I have application in which I have three activities. First and second activity have listview. I need selected values from the first and second activity in order to display some data in the third activity. Which is the best way to do this? Is it better to pass Intent from first to second activity, then from second to third activity or to use SharedPrefenreces?
This depends on what you need. If you need the data to be persistent in the event that your app is destroyed, then you will probably want to save the data in SharedPreferences (will also depend on what type of data since SharedPrefs aren't limited to what they can store) or a DB.
If you aren't worried about it being persistent then passing the data through Intent.putExtra() is fine. You can also create a Bundle and pass the Bundle through the Intent and add the data as you go.
You also have the option of creating a separate class which implements Serializable. Then you can create and pass the class Object through your Intents. Here is a post about doing that
Yes this can be achieved in many ways using:
Intents
Shared Preferences
Sqlite
Sqlite: You can use Sqlite to store data if the data is huge in first and second activity and retrieve the data in the third
activity (But if data is less this is not a feasible solution)
Shared Preferences: Here the data you have saved in the shared-Preferences is done in a xml file, so even if the app is
stopped the data is retained. This is helpful to save the password
and login details etc
Intents: My opinion Passing data with intents is much better option in your case because size of the data passed between your
first, second to third activity is less.
Finalizing :: Just put the data in bundle in first activity. Next get the data(Bundle) in second activity and add the data in second activity in addition to the data in the bundle received from first activity and pass that final bundle from second activity to the third activity.
In my opinion, it would be easiest to create an ArrayManager class, in which your variables are static. This way you can create methods to select and resize your arrays in the ArrayManager class. The only case in which this is bad is if you must hold references to Views, Contexts, or Activities (this will create a memory leak). In that case, codeMagic's solution is ideal.
In your case, if you need to pass same data from first activity to second activity then third activity & your data is small then I suggest use Shared Preferences.
If data passed from first activity to second activity is not same data of second activity to third activity then simply use intent.
There is a tricks. If your second activity always need data from first activity to operate then when you want to go back third activity to second activity then you will get stuck because from third activity you can not get first activity result. In that case saving data i mean Shared Preferences or Sqlite is best.
All above those scenario you need to find your own what is best to use for your app.
Related
I'm new to Android development and was unable to find how to pass values to an activity that is not the next screen that the user will see. If I have activities in this order: 1>2>3 connected by buttons, how do I pass a value from 1 to 3? I'm only familiar with using intents on button clicks, which would require me to pass the value to screen 2 and then to screen 3.
Thanks
There are several ways to do that:
1) From Activity1 you pass data though Intent. In Activity2 you retrieve data and pass it within new Intent to Acitivity3.
2) Save data in SharedPreferences in Activity1 and retrieve them in Activity3.
3) Save data in SQLite database which is default for Android. This is a good option if you need to save/query data not once, but many times.
4) Save in Google Cloud or use other services.
You can see here a full bunch of store options in Android. Without performing those actions, storing/retrieving data though the Intent is the only one way.
Note: Make a static variables is such a bad practice, unless it is just constant values or other utilities.
If the chain pass becomes too complicated then you should persist it. My suggestion is using Preferences to save the value. Later you do an existence check in the retrieving Activity, load it, and if needed clear it. This is the method commonly used when storing app Settings values.
If your values become more complicated in structure and more numerous then you'd need to consider database.
I am noob in Android. Suppose I have two Activity in my android app.
Activity A and B, currently I am on Activity A then on click on one button I am now Activity B. Here From Activity B I want to update some view with updated data that is in Activity A. I got updated data in Activity B so Should I use here LocalBroadCastReceiver for this or anything ?? so that When I press back then app should show me updated data in Activity A
Should we use our custom callback here to update the UI view data of Activity A from Activity B ??
No, you shouldn't use BroadcastReceiver for that. The method depends on the size of data you want to transfer from Activity B to Activity A. If it's quite small, you can start Activity B with startActivityForResult and then get data back at the onActivityResult method (in Activity B you should use setResult once you are done). If the size of data is quite big, it's better to use DB for storing it instead of keeping it in memory.
okay , there are multiple ways to do it :-
you can use a static variable to store you data that too of application level ( might not be a good approach ) check the value of vairable in onResume() and set it to the view.
you can use sharedpreferences if your data is not that large , store the data in Activity B and fetch it on onResume() method of activity A.
as #nikis has told you
if your data is too large store it in db.
I dont think broadcastreceiver fits right in your scenario !!
I have 5 Activities in my App. Each Activity having some data. I need all previous Activity data in last Activity. All previous Activity is a kind of Form. User fill the data and move to next Activity and last Activity displays all previous Activity's data.
There are several ways to do this.
Use of Intents and save the data like putString(...):
Which isn't really nice because you have to temporarilly save the data of each Intent in the Activity and move them further with the next Intent.
Use a class for your data and pass it through the Activities per Intent:
This way you pass the instance of the class to the Activities to write/read your data. But this class needs the Parcelable / Serializable interface
Use of a static class:
You can access this class in every Activity and write/read like you want.
Use a Bundle with the Intent:
Save all your data in a Bundle and pass it to the Activities.
Think about your design and use Fragments:
Have one Activity with 5 Fragments. Every Fragment saves/reads the data from the Activity.
Best approach would be to use a Bundle.
Option 1:
If you want to add data for every activity and carryforward it to next activity then it's better to pass data with bundle.
Bundles are better in a scenario, when data is comparatively small and where you just want to pass data to next activity and don't want to store the data for future.
Option 2:
You can use Sharedprefrences. Better to use when data is to be stored for future.
Option 3:
If you have more records and you want it to be in a structured manner, and to be stored for future then Sqlitedatabase is obvious.
Above to options are in case if you want to use your data to use very frequently and it is not structured.
You can use Intent
FirstActivity
Intent intent = new Intent(FirstActivity.this,SecondActivity.class);
intent.putExtra("key_value", string);
startActivity(intent);
SecondActivity
String text = getIntent().getStringExtra("key_value");
Or if you need the data to stay longer, and you can reuse it after the application is killed, you can use SharedPrefrences
Place the data in some Activity
SharedPreferences.Editor editor = getPreferences(MODE_PRIVATE).edit();
editor.putString("ValueOne", "SomeValue" );
editor.commit();
Retrieve data from shared preference in any Activity
SharedPreferences prefs = getPreferences(MODE_PRIVATE);
String prefString = prefs.getString("ValueOne", null);
I don't know how to solve my problems. It goes like this:
In main activtiy I make my own objects and added them to list(arrayList) and after that when I go to another activity I would like to:
to send list of list og my own ojbects, I know you make it with: intent.putExtra but you don't have type list myObject
when I pass the list of lists of objects I think it makes new istance of these datas, but I would like to have one istance all the tame and I would like them to read and manipulate on first instance.
More explanation in my main activity I make objects and they are ready for all other activityes and all other activitys can read and write my list od lists. Only one activity is active in a time.
And I am also intetrested in when I manupilated the data in some activity and I would like to go to main menu and pick other activity how to send from that first activity to menu activity and pass them to next activity to process.
Would you help me, please. Best regards Robert
I suggest you implement an application object, and have the objects that you're referring to live in the application object rather than an activity object. There can be only application object associated with any particular app, and it gets created before any Activity objects, and is independent of whatever activity objects are created/destroyed during the lifetime of your app. Hence you don't need to worry about sharing your objects between activities, because they're all available globally in the application object.
I have an Activity which is an OpenGL view. I also have an xml layout to use for preferences. Until now, to show the preference menu, I just brought it to front by setContentView(). And the same to get back to the OpenGL view.
But is this a case where I should give the preference menu its own Activity?
I guess this would make a few things much easier. For example, the back button would just work, opposed to now where I have to code it or it will just exits the application.
And if this is a good idea, how do I pass data both ways? I have a class that store all preferences. Can I send it to the Activity and back again? Or is the best way to store the preferences in a sqlite database and then use it for passing data?
I find it easier to segregate menus and such into separate activities (unless you are using dialogs etc..) As far as storing data you can do it a number of ways:
Database
StoredPreferences
Intent extras with putExtra/Bundle
Creating an application subclass and storing preferences there
Each have their merit. 4 is pretty easy as you just have to state the application class name in your manifest then call: MyAppClass app = (MyAppClass)getApplicationContext(); and you can then use any variables in MyAppClass via app. 2 is also straightforward.
You already pointed out the main difference: history management.
You can pass data to Activity via Intents putExtra()/getExtra():
Create an Intend and add custom data via Intent.putExtra(..)
Start the new Activity: startActivityForResult(intent).
Inside new Activity you can get extra data with intent.getXyzExtra() (where xyz is type).
When new Activity is done just call setResult(int, resultIntent). Again you can add extra data as described in 1.
Call finish() to end the activity.
In original Activity method onActivityResult will be called. Again extract data from Intent as described in 3.