Reputation: 17
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.
Upvotes: 0
Views: 1990
Reputation: 1922
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.
Upvotes: 4
Reputation: 17
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
Upvotes: -1
Reputation: 2487
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!
Upvotes: 1
Reputation: 71
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
Upvotes: 1