简体   繁体   中英

Sharing an object between Tabs (different activities)

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 :)

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

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.

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.

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM