简体   繁体   中英

How to share data between multiple activities?

This will be my first project in android studio. I have a some java and C/C++ experience. I still am a total beginner though when it comes to program development. I have never developed an android app.

This app will be used to track a workout program. User will be required to enter his 1 rep maxes for each of the following; bench, squat, deadlift. I want the app to use these inputs to base the workout program on them. This will involve multiple activities sharing and manipulating this data.

What is the best way to take the user input, save it (meaning store it somewhere and left unchanged even when the app terminates), and have multiple activities access it to display? The app will only ask the user for input once. If the user wants to change input, it can be done in settings.

When you call Intent to jump from one activity to other activity then you can put some data or values which you need for second activity or any further use. You have to do same for all activity call and lastly you can use it where you required data.

intentObject.putExtra("KEY", value);

Bundle bundle = getIntent().getExtras();

int value = bundle.getInt("KEY");
or
String value = bundle.getString("KEY");
or
DataObject dataObject = bundle.getString("KEY");

You can put data object and get data object also using Gson.

Try this by using INTENTS

On First Activity

Intent intent = new Intent(Activity_FROM.this, Activity_TO.class);

intent.putExtra("KEY1", value);

intent.putExtra("KEY2", "a value");

startActivity(intent);

On second activity

Bundle bundle = getIntent().getExtras();

int value = bundle.getInt("KEY1");

String value2 = bundle.getString("KEY2");

To have it more clearer, you better to understand basic concepts of android framework and terminologies. When its done, you can easily understand the process flow.

Visit www.vogella.com

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!

Intents are one way to pass Data from one activity to another.However you need to pass it every time you change from one to other... Some other way more simple and effective is to use a singleton : a class where you can write into and access in any activity by creating an instance for it. just take a look at this https://gist.github.com/Akayh/5566992

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM