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.
Related
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.
I've a big questionaire to fill, so I've decide to split it in two activities/screens. My question is:
- Should I send the data from activity1 to activity2 (startActivityForResult) and insert all the data on sqlite with an insert on activity2?
- Should I make an insert on activity1 and an update on activity2 since I know the row _id?
The option 1 is better to ensure the user fills all the questionaire, right?
Have you already considered:
Using shared preferences in activity 1, read it from activity 2, then insert; or,
Using a static array or map, which both activities can access?
I don't know what your security needs are but either two options seem so much simpler, especially number 2.
First option is better, because with this you will never have incomplete information in database (what happens if user exits from app when he finishes with first screen?) and you only will make a unique call to insert data.
I am starting an activity with a Serializeable extra. This extra contains a List of a custom object holding a bunch of types, mostly Strings. I read in the data from the assets folder of my project, and parse it with GSON(the data is JSON). This file is ~108KB in size.
For the life of my application, all data is being passed around as intent extras. This is very convenient as i don't have to worry about re-loading the data from the assets folder again, app shutdown recovery is all taken care of, and i don't need to manage an SQLite database(versioning, queries, etc).
Problem:
I find that passing these extras around can become quite slow(starting an activity with ALL the data takes maybe 1.5 seconds or more). I can't seem to show any "loading" dialog either, as it seems to be a blocking call to start an activity and attach extras.
Question(s):
Should I avoid passing these extras around like I have described? Is the best option using an SQLite database to interact with this data? What suggestions do you have to avoid a lot of the fuss of SQLite databases/global static variables for accessing my application's data?
Slapping my JSON data into data model classes and passing them as intent extras is easy and nice(seemingly), and i don't want to give it up!
I had the same problem and followed the solution you already depicted. I used an SQLite DB to store the information, then passed only unique ids as intent extra and displayed a "loading..." progress dialog on the called activity while I accessed the DB.
Prolly using SQLite is the way to go.
Why don't you load the data to the Application class, onCreate of the Application class, and then access from every activity there?
Usually we pass value in android from one activity to another activity by Intent function.Is there is any another way is possible to pass the values from one activity to another activity with out Intent function
Yes. Create a class that extends android.app.Application, then in the Manifest put this class as the name of your application
<application android:name=".ClassNameOfTheClassYouCreated"...>
...activities etc here...
</application>
Now, that class that you created is your application. Its lifespan is all the way as long as the application is running, it holds the activities stack and you can add some custom fields to hold your values. To get the Application instance from an Activity, hit this.getApplication().
Edit regarding fields values being reset (in response to the commend by #hackbod): Using static fields on singletons is the other way to hold global values, but I find the proposed one more elegant. As of the case you mention with resetting values, that can also happen in other contexts (like incoming phone call, orientation change), which raises the need to sanitize (or check for existence, call it whatever you like) values before using them. Always!
Information flow can be realized in various way, all you need is a way to save and retrieve your data.
You can save and then get your data which you want it to flow via static class, file storage, database, or even remote storage by using network.
Intent is the better way. Android creates it, so use it. :)
From http://developer.android.com/guide/topics/providers/content-providers.html :
Content providers store and retrieve data and make it accessible to all applications. They're the only way to share data across applications; there's no common storage area that all Android packages can access.
If you want to make your own data public, you have two options: You can create your own content provider (a ContentProvider subclass) or you can add the data to an existing provider — if there's one that controls the same type of data and you have permission to write to it.
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.