I have a a simple Java bean that gets filled in my second activity.
When I return back to my first activity(main activity) I want to have access to this data. But so far I have no idea how to do it.
Can anyone provide me with a code example of how to pass beans between Activities?
Kind regards
Shared Preferences if you have string data
http://developer.android.com/guide/topics/data/data-storage.html#pref
Singleton class if you have objects to pass around.
If you want guaranteed persistance use database.
Related
I want to instantiate an object in my MainActivity class that will be used and altered by my other activities.
The object is a custom data type.
Would it best best to declare the object as public static in MainActivity and then just use MainActivity.object throughout my application? Or would it be better to just pass the object around using intents?
The main thing I'm worried about using static is that I've seen people saying you use it when you care about privacy but I'm not too sure what that means so.. hoping to get some input.
Thanks
If you need one object on all app, you can store this object in Application class
Currently im storing all of my users information and there friends information inside a class i have created called userInfoCore that extends Application so i can store the values in the Context. When my app crashes it gets rid of al those values and my users are forced to relogin, so i would like to store them in SharedPreferences to be grabbed again in the onCreate of my MainActivity.
I know how to store them, thats not the issue. The issue is i dont want to overcrowd my code with repetitive code and put the storing methods in all the onDestroy's of all my Activities, and i cant #Override onDestroy in my userInfoCore class because its not an Activity i imagine?
Some insite would be great. Thank!
EDIT:
Ive found out that this line in the android manifest is causing my Application Context data to be destroyed even when the user presses the home screen. android:launchMode="singleInstance"
My thoughts are YES i could store them in the onPause or do what #CommonsWare suggested. However like i said, i dont want to have to do all of that. If i can find the root of the cause of the issue... which i have. (The singleInstance in manifest) then i would be much happier.
Some insite would be great.
Update your persistent store when the data changes. A custom Application subclass, like any singleton, should only be treated as a cache or other transient spot for data. If you care about the values, persist them, at the point when the data is changed.
This is a good argument for using the Model-View-Presenter (or Model-View-Controller) pattern. By separating your Model (domain data and procedures) from your View and Presenter (Layout and Activity respectively) you only have to write the Store logic once.
Then you have two choices: either do as #CommonsWare suggests, have the model write itself whenever it changes, or add a simple call in each Application's onPause (onDestroy is too late! It may never get called.) to the model to tell it to save itself.
Note: the model can accept a context as a construction parameter for use in finding a shared preferences, or it can create it's own named preferences using the PreferenceManager
I have an android project where i have different objects that one or more of my activities need to acess now i was thinking of creating a subclass of Application however under the documentation of Application it states the following:
There is normally no need to subclass Application. In most situation, static singletons can provide the same functionality in a more modular way. If your singleton needs a global context (for example to register broadcast receivers), the function to retrieve it can be given a Context which internally uses Context.getApplicationContext() when first constructing the singleton.
My question is fairly simple is it best pratice to use a static singleton class to contain all of your objects ? or am i right to assume that extending application is a better option?
To answer your question I would use a singleton container to access these objects, initialize that class with a context by application context (there are very big chances you will need a Context); but then you will see it's kind of hard to maintain these and the singleton container.
To solve this object graph issue, I would use some IoC: RoboJuice, AndroidAnnotations or Dagger are really cool and they provide much more. Each of them handles this issue different, but you don't have to worry about that.
I hope it helps!
Can I use Android's sharedpreferences to pass a Java object between different activities?
"Bundles" are probably the best way to go:
http://bimbim.in/post/2010/09/27/Android-Passing-object-from-one-activity-to-another.aspx
Intent and Bundle are two classes to transfer object's from one
activity to another activity. You can create bundle object and put
them into intent
If you want to pass an "object" in your Bundle, then the object must implement "Parcelable":
How to send objects through bundle
http://developer.android.com/reference/android/os/Parcelable.html
Yet another alternative is to use global state:
http://developer.android.com/reference/android/app/Application.html
No. For that purpose I suggest creating an implementation of the Application class as a singleton and keeping a reference to your object there.
Edit after reading comments:
Keep in mind though that a singleton has many drawbacks and should be used only if the other solutions are insufficient.
In my main Activity I have object with some data in it. I want to delete all this data through PreferenceActivity using method (wipe()) that does this job. How can I do it?
Inside Preferences.java there is a OnPreferenceClickListener which, when activated, expected to use method in object that I need to access.
Thanks in advance.
Just make this object as static.
Then you can access to this object by class name.
Activity.counters.wipe();
But, keeping data in activities is a bad practice in android development