Sharing An Object Between Tabs (different Activities)
Solution 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.
Solution 2:
Data:
import android.app.Application;
publicclassDataextendsApplication {
privateintblupp=0;
publicvoidsetBlupp(finalint bla) {
blupp = bla;
}
publicintgetBlupp() {
return blupp;
}
}
Setting the data in the oncreate() method of one activity:
finalDatamyData= ((Data) getApplicationContext());
myData.setBlupp(12);
Getting it in the oncreate() method of another:
finalDatamyData= ((Data) getApplicationContext());
finalinttest= 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.
Solution 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 :
publicclassAppContext {
publicStringusername=null;
//////////////////// below the singleton implementation//////////////////privatestaticfinalAppContextinstance=newAppContext();
// Private constructor prevents instantiation from other classesprivateAppContext() { }
publicstatic 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
Solution 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
Solution 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.
Post a Comment for "Sharing An Object Between Tabs (different Activities)"