How to Share Same Data Between Multiple Activities

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!

How to share same data between multiple activities

One way to do it is to extend Application, and then call getApplication in your activities. This site has some examples.

Passing Data Between Multiple Activities

When calling from B to C, add to your intent:

myIntent.putExtras(getIntent().getExtras());

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 the next activity:

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

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

How to transfer data from multiple activities on AndroidStudio?

You can do this. In your Opciones activity, declare a static String variable. Store EditText data into string and you can reference the string from the second activity.

Third Activity

public static String claveCorrecta = "";
claveCorrecta = et2.getText.toString();

Now in Second Activity you can reference the String by

Opciones.claveCorrecta

send data from one activity to multiple activity

I recommend considering Mediator Pattern or an interface which will be implemented by your receiving activity classes;

how can i share viewModel between Activities?

You can't share a ViewModel across Activities. That's specifically one of the downsides of using multiple activities as per the Single Activity talk.



Related Topics



Leave a reply



Submit