Moving value to activity not being accessed immediately - android

Is it possible to pass a value in a putExtra or Bundle to be accessed later?
For example, in a standard putExtra you load up the intent with the values you want to pass over and then in the activity specified in the intent, you retrieve those values. Is it possible to store the values in the putExtra and retrieve them in another activity that is NOT the one specified in the intent?
For context, I am using google sign in for authentication. I want to store the user's account in a putExtra and use it later in the app, but not in the activity that follows from the sign on.

Use Application or MultiDexApplication class to keep a SharedPreference that holds your data in String format
Use SharedPreference at you Activity and follow similar procedure.
This link might give you some mental picture.

Related

Pass value to non-sequential activity

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.

Android passing values to activities that have not yet started

I am running into an issue with passing values to activities. I have a Title activity that launches Generating activity. Generating activity launches Play activity. Suppose I want to pass some value such as a boolean to Play activity from Title activity.
Would I have to pass it first to Generating activity and then make Generating activity pass the value to Play? Or can I pass it via putExtra() bypassing Generating activity even if Title activity does not launch Play activity directly?
An intent is a message containing data that will launch an activity. The data is only available to the target activity.
So to answer your questions:
No, you cannot pass intent data from TileActivity to PlayActivity. You must pass it to GeneratingActivity, who in turn would then pass it to PlayActivity.
There are alternatives to using intents such as:
Static/Global data - Use this with care. Because of how Android manages your process, it can be dangerous to use this approach.
SharedPreferences - This is a mechanism for persisting data. Your first activity could save the data in SharedPreferences, and the third activity could read it from SharePreferences. Because of the dangers with approach #1 above, many people take this approach in Android.
You can create a public class with a static variable :
public class Global {
public static boolean play = false;
}
You just need to call Global.play = true in your Title Activity and check the value of Global.play in your Play Activity (or other Activities based on your need)

Passing data through multiple activities in android

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.

How to save an intent's data

I have the following situation: I need to save data from an Intent's putExtra call into another Activity. I am able to display the putExtra data, however I cannot save it. I am using startActivity to start the activity that I need to save the data in. I am attempting to store it by adding the data to an ArrayList, however whenever I change Activities the data is lost. Also it only stores the last value. How can I store the sent data in the second Activity?
You can store it in the Application Context as described here:
Static way to get 'Context' on Android?

how to save data in next activity

hi to all
I what to know how to save data in next activity
Use Intent to pass data from one Activity to another Activity.
But these Intents may carry native data types like String, int, float, double etc.
If you want your own Object to be passed, you would have to implement Serializable interface.
An odd way is to use a static Object in a class which is globally accessible.
you should explain clearly about your requirement. As i have understood, i think you want to access data which belongs to one activity in the other activity.
Refer the link below, you will get a clear idea.
http://developer.android.com/resources/faq/framework.html#3
u can use intents, bundles, or any other static variables to holds data from one activity to another

Categories

Resources