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.
Related
Can anybody please tell why we need to serializable object for passing one activity to another activity in android? Android is following Java syntax. In java we can pass object to another class without serializable.
Thanks
In ordinary java programs passing parameters(Object type), is kind of create a new handler to the object and giving to another method (In regular words passing the reference by value).
But when it comes in android, passing object references from activity to activity, where their states have to be persisted, is a serious headache.
One way you can do is create a static object in the first activity and access from the second, though this seems to be a easiest way, there is no guarantee that the system maintains the activity in the memory. Therefore the second activity may loose the object reference.
Other way, and the mostly recommended way is serializing(Kind of flatten the object) the object and pass with the intent as extra. In android there are two ways to serialize.
Implement the java's serializable interface
Implement the android's parcelable interface
However, on the android, there is a serious performance hit that comes with using serializable, the solution is using parcelable.
You can find a pretty good tutorial and explanation on android parcelable implementation here.
We need to understand following concepts before getting to the answer:
Android uses Binder for inter-process process. It is required even for simple app because the OS and the apps run in different processes.
Marshalling:
A procedure for converting higher level application data structures into parcels for purpose of embedding into Binder transaction
Unmarshalling
A procedure for reconstructing higher-level application data-structures from parcels received though binder transactions.
You can consider Intents as higher level abstraction of Binder
Based on the documentation following is the way how intent communication occurs:
Activity A creates an Intent with an action description and passes
it to startActivity().
The Android System searches all apps for an intent filter that
matches the intent. When a match is found,
the system starts the matching activity (Activity B) by invoking
its onCreate() method and passing it the Intent.
Why Parcelable or Serializable
IPC (Inter Process Communication) requires data in Intent to be Marshalled and unMarshalled. Binder provides built-in support for marshalling many common data-types. However when we define custom object, it would impact this process and the final object received might be corrupted during the process.
When you define custom object, you need to be responsible for providing this marshalling and unmarshalling which is achieved through Parcelable and Serializable (Since comparison between these two would be another topic I won't discuss much here). Both of these provide mechanisms to perform marshalling and unmarshalling. This is the reason why you need to use Parcelable or Serializable.
Using Parcelable you write the custom code for marshalling and unmarshalling the object thereby you gain complete control over the process.
Serializable is a marker interface, which implies the user cannot marshall the data according to their requirements and its done on JVM, which doesn't give any control at your side.
Disclaimer: Description above is my understanding for the rationale behind the need for serialization based on some
documentation
There are basically two questions in your question, so let's decouple it.
Why marshall in a Parcelable instead of passing an object reference directly?
It's obvious faster and more memory efficient to reference objects rather than marshall/unmarshall them. So you shouldn't use Parcelable when you can pass the object directly.
However, there are situations where you may not have access to the object reference.
in Intent because the process that handles the Intent may not be the process that emitted the Intent (it's an inter-process communication)
in Activity lifecycle, for instance in onRestoreState(), because the whole app may have been killed by memkiller when the user wants to resume it.
everywhere else where Android frameworks requires
In IPC, why use Parcelable rather than Serializable like Java does?
That's only a performance optimization.
If We want to pass object from Activity to to Another Activity . We need to save the passing state.
//to pass :
intent.putExtra("MyClass", obj);
// to retrieve object in second Activity
getIntent().getSerializableExtra("MyClass");
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
I am trying to pass an object from one activity to another. But from the methods I've seen, it seems like you always grab that object in the onCreate method, which makes sense. But since onCreate is protected, I'm unable to reference that object in other methods in that activity.
Is there a better way to do this? I'm still getting the hang of all this Android stuff.
use intent for this that is the best way for doing this:-
intent=new Intent(this,yourotheractivty.class);
intent.putExtra("object",object);
startActivity(intent);
get this in your activity's oncreate using the get intent if you are creating a custom object you better use parceable
Using an Intent is one common method of sending an Object around, but it sounds to me like you are looking for something like a generalized observer pattern.
I would create class that holds references to subscriber objects and what callbacks they would like receive. Upon sending a callback and associated data to that class, have the subscriber objects registered to that callback receive the associated data.
Existing solutions:
If you're using Guava it has one built in (https://code.google.com/p/guava-libraries/wiki/EventBusExplained), otherwise take a look at Otto (http://square.github.io/otto/) or EventBus (https://github.com/greenrobot/EventBus).
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
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.