What's the Best Way to Share Data Between Activities

How do I pass data between Activities in Android application?

The easiest way to do this would be to pass the session id to the signout activity in the Intent you're using to start the activity:

Intent intent = new Intent(getBaseContext(), SignoutActivity.class);
intent.putExtra("EXTRA_SESSION_ID", sessionId);
startActivity(intent);

Access that intent on next activity:

String sessionId = getIntent().getStringExtra("EXTRA_SESSION_ID");

The docs for Intents has more information (look at the section titled "Extras").

How to share data between fragments and activities in android

For between activities you can putExtra values such as below:

Intent intent = new Intent (this, newActivity.class);
intent.putExtra("someKey", someValue);
intent.putExtra(bundle);
startActivity(intent);

To get it inside your activity:

getIntent().getExtra("someKey");

For moving values between fragments i'd suggest using bundles:

//Where mainlayout is the top level id of your xml layout and R.id.viewProfile is the id of the action within your navigation xml.
NavController navController = Navigation.findNavController(getActivity(), R.id.mainlayout);
Bundle bundle = new Bundle();
bundle.putString("uid",snapshot.getKey());
navController.navigate(R.id.viewProfile,bundle);

to retrieve this value within your fragment:

String game = getArguments().getString("game");

Hopefully that helps.

How to share data between multiple activities?

Yes, Intents are the way to pass information between activities, but as you will soon discover, they are not meant to hold whole objects of data and to keep some kind of persistence, trying to implement your data passing will become your nightmare.

Take a look for some persistence frameworks( let it be some SQLite wrappers or I personally like realm.io as they provide really cool features like live auto-updating objects, it's built upon their c++ engine and it's very easy to use )... Once you store your data in some kind of data layer, you only pass identifications for your objects through intents and query for them in the receiving activity...

That way, you keep your model persistent across the entire application, don't have the trouble to propagate changes of the object back to the source activity and lose the hell related with Parcelables ( with is a way to serialize your object to store it in intent )

Happy coding!

What is the best way to share data throughout all activites?

To pass (or persist) data between application's activities, you can either use:

  1. SharedPreferences:

Shared Preferences allows activities and applications to keep preferences, in the form of key-value pairs similar to a Map that will persist even when the user closes the application.

Android stores Shared Preferences settings as XML file in shared_prefs folder under DATA/data/{application package} directory. The DATA folder can be obtained by calling Environment.getDataDirectory().

Link to SharedPreferences example tutorial.


  1. Intent.putExtra(...)

Whenever you need data from an activity to be in another activity, you can pass data between then while starting the activities. Intents in android offer this convenient way to pass data between activities using Extras.

Link to Intent pass data through extras tutorial.

Most secure way of sending data between activities in Android

Putting the data in the Intent would be the least secure, insofar as that data is being sent by IPC from your process to a core OS process, then back to your process via another IPC invocation. On modern versions of Android, I know of no security issues with this — other apps cannot spy on that IPC or otherwise get at that data. However, all else being equal, keeping the sensitive data within your process at all times would be more secure than passing that data outside of your process.

The cited "Persist objects (sqlite, share preferences, file, etc.)" is not a way of passing data between components. It is a way of persisting data for long-term use. Now, most apps need such persistence, and so you may have persistence for other reasons and simply leverage it (e.g., A triggers writing data; E reads in that data). From an efficiency standpoint, though, disk I/O is slow, and so you do it when you need persistence, not data passing.
Typically, though, with persistence, we maintain some sort of in-memory cache, to minimize that disk I/O. That involves static fields and WeakReferences, the other option cited in that Stack Overflow answer.

Off the cuff, and knowing nothing about your app:

  • If this sensitive data should be persisted, then just persist it, use a cache, and have E get the data from the cache or persistent store

  • If this sensitive data should not be persisted, but it is large and it can be acquired again (e.g., via network I/O), cache it using a static field and a WeakReference, so you do not tie up heap space indefinitely

  • If this sensitive data should not be persisted, and either is not large or cannot readily be acquired again, cache it using a static field, so it sticks around as long as your process does

Another way that you can exchange data between activities

It is usual to pass data between activities using extras within the intent. Such data persists for the lifetime of the receiving activity (when finished with, the garbage collector can free up the memory).

Or you can store values using SharedPreferences. These will persist between sessions and are stored as key/value pairs in a file (so don't impact memory use as such).

Or you can hold values in static fields (as you are doing here) which persist for the lifetime of your application session. However there is a significant risk with this in that the garbage collector cannot free up memory that is referenced by such fields unless you set them to null when you no longer need the reference. You should never store a reference to an activity/context/view in a static field since you'll leak the entire activity which can amout to a significant amount of memory usage.

http://android-developers.blogspot.fr/2009/01/avoiding-memory-leaks.html

You can pass a class instance within an intent if it is Serializable, e.g.:

Intent intent = new Intent(this, whatever.class);
Bundle b = new Bundle();
b.putSerializable("data", my_object);
intent.putExtras(b);
startActivity(intent);

And in the receiving activity, cast it back to whatever class your object is:

Bundle b = getIntent().getExtras();
my_object = (whatever class is it) b.getSerializable("data");

Many java classes implement Serializable and it is very simple to make your own classes serializable too.

What is the best way to pass data across multiple activities

It looks like a wizard pattern to me. I'd consider the following:

  • If your data is small, using Parcelable sounds like a good idea. It will be executed on the main thread so if you have a lot of data to parcel, that will block the UI.
  • I'd also consider implementing those 3 screens as fragments, you can have that data saved in the activity. You will still have to parcel it for configuration changes (rotation of the device for example)
  • You can store the data in a singleton object that won't get removed across configuration changes.


Related Topics



Leave a reply



Submit