Sharing an object between Tabs (different activiti

2019-08-02 06:28发布

问题:

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

回答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.



回答2:

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.



回答3:

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



回答4:

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



回答5:

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.