1

I am trying to come up with the best solution for a problem: I am developping an app with 3 tabs. I get xml data from a rest service and parse it into an object (there is only one request). The 3 tabs now display different parts fo this data. I was thinking about splitting the app into different activities to make the code more readable. How do I share the date between the activities? I know this question was posed a million times but I still cannot come up with a solution.

  1. application object needs to derive from the Application class, but my main activity is already derived from the TabActivity class. use a different main class und then start my tab class with an intent?

  2. A HashMap of WeakReferences to Objects. Seems a waste of memory but would be possible.

  3. Put all the code into one activity and be done with it.

Thanks for any help :)

Redfox
  • 1,024
  • 1
  • 11
  • 28
  • You should pick an answer even if it's your own one ! – AsTeR Nov 15 '11 at 13:51
  • I am waiting for the timeout to exspire in order to be able to pick my answer. Using your code I get an error that the type "singleton" is unknown. Eclipse could not find any Imports for that Class. But thanks nonetheless for your effort. (please stop plenking [using uneccessary white spaces before punctuation marks]) I got the idea I just did not know, how to code it. Unfortunately I could not understand your suggestion. – Redfox Nov 16 '11 at 14:02
  • I've forgot to replace some words after my copy paste sorry if you wanna try again. I'm still thinking this approach is best. – AsTeR Nov 17 '11 at 16:13

5 Answers5

1

In all my applications I'm using a Context class (called through a Singleton) that keep all application level informations and data that have any reason to be shared through the different activities.

By the way, this introduce a model level (in the MVC sense of it) in your application, in software design this part should be used to keep data that represent user's data and the application's state.

Singleton example :

public class AppContext {

    public String username = null;

    //////////////////
    // below the singleton implementation
    //////////////////

    private static final AppContext instance = new AppContext();

    // Private constructor prevents instantiation from other classes
    private AppContext() { }

    public static AppContext getInstance() {
        return instance;
    }
}

When you got your data from the web (here username) :

AppContext.getInstance().username = receivedUsername;

To get it in one of your activity :

myLabel.setText(AppContext.getInstance().username);

PS1 : extending application for satisfying such a purpose doesn't seem to be a good thing to me. Extending Application class is supposed to extend the normal application behavior, no to be a mean of storing common data.

PS2 : your weak reference map could be added in the Context object to structure your data

AsTeR
  • 7,247
  • 14
  • 60
  • 99
  • A singleton would use the application object? – Redfox Nov 14 '11 at 15:25
  • No a singleton class just written to be accessible everywhere in your application. I think touching the application object is a bad idea for your problem – AsTeR Nov 14 '11 at 15:31
  • Check at the wikipedia page if you don't know how to do a Singleton in java. – AsTeR Nov 14 '11 at 15:35
  • I know the idea behind a singelton but I don't know the correct way to use it in android. I using the application class is "not to be a mean of storing common data" then what is? :) – Redfox Nov 14 '11 at 15:55
  • The correct way of using your singleton is just to adding it attributes (this is a basic data object). For example if you get a username from your rest service just : AppContext.getInstance().username = receivedUsername; to retrieve it somewhere else AppContext.getInstance().username Is that clear enough ? -> I updated my answer with the code – AsTeR Nov 14 '11 at 16:09
  • Thank you ver much for our effort. I will try it out as soon as I tried out thies approach: http://stackoverflow.com/questions/708012/android-how-to-declare-global-variables – Redfox Nov 14 '11 at 16:28
  • Ah yes, no I got it. Thanks for your persistence. This way I can drop all the getter and setter methods. This is the better approach indeed. Thank you again – Redfox Nov 18 '11 at 09:17
  • But using this approach without any getter and setter the variables have to be public, which is quite ugly. :( – Redfox Nov 18 '11 at 09:33
  • It depends on how you plan to use it. I drop the approach about putting getter and setter everywhere. Eclipse Java offer quite easy refactoring solution when I need to plug something somewhere, in this case the Context class is just a basic mean to share data inside of your application, for some need you can put method in it or design another property access policy. – AsTeR Nov 18 '11 at 10:56
1

The ideal solution is that a the activity notifies a service which starts and handles the rest request, saves the result somewhere such as a sqlite-db, then the service notifies the activity that the transaction is done so it can query for the data.

But you only have one request and I don't think you'd bother doing all those mentioned above, so I'd go for number 3.

josephus
  • 8,284
  • 1
  • 37
  • 57
1

Data:

import android.app.Application;
public class Data extends Application {

private int blupp = 0;

public void setBlupp(final int bla) {
    blupp = bla;
}

public int getBlupp() {
    return blupp;
}
}

Setting the data in the oncreate() method of one activity:

final Data myData = ((Data) getApplicationContext());
myData.setBlupp(12);

Getting it in the oncreate() method of another:

final Data myData = ((Data) getApplicationContext());
final int test = myData.getBlupp();

In the android manifest:

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:name="Data">

The Class Data must be put there. That was rather simple. Formatting is a bit messed in this forum. I don't quite get it with the code format. :( Thanks for all the help.

Redfox
  • 1,024
  • 1
  • 11
  • 28
0

Try creating the object with

public static Object....

this static object can be used for all your classes, u can access the object bye ClassName.objectName

Maulik J
  • 2,745
  • 19
  • 22
0

Depending on the data, there are a number of ways of doing this. How big is the data? If it is text-only, and not insanely huge, it might not be a problem to keep it in memory throughout the application lifetime.

If you split your application into different activities, you have two choices:

  • Passing the data between the activities as intent extras. Lets say that the activity displayed by default fetches the data. When you want to see other parts of the data, you can bundle the data you need into the intent and retrieve it in your newly created activity by using getIntent().getExtras().
  • Keeping the data in a singleton or as an instance variable of an Application subclass. I would advise against using a singleton as having objects living on their own without references to other objects makes your code more prone to memory leaks. Keeping data in your Application class is a better approach in my opinion.

As stated, the right solution depends on what your data looks like. If all of your activities display different parts of the data, I would probably keep it in my Application subclass. However, if your application is structured in a similar way as a contact list (one activity for displaying the contacts, and one activity for detailed information about a contact), I would probably have my data in the main activity and pass just the necessary details to my other activity.

Bendik
  • 928
  • 11
  • 23
  • Thank for your answer. It is much a contact list. A personal tab with a few values and two lists containing a dozend lines with 3-4 entries each. But many values to parse aroung with the intent, imho. I'll try to create a Application subclass that starts my TabActivity subclass. – Redfox Nov 14 '11 at 16:05