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?
Related
I have one recyclerview so the task is to get the data from another activity and set it to the main activity's recyclerview and when we press on particular row of list so, it get back to the editable mode and edittext fill with old data and replace it with new data and again show update data in recyclerview.
Yes, you can do this by using the startActivityForResult() from list item adapter and at that time you can pass the data through it.
Follow the steps:
1. Implement Main Activities recyclerview
2. Get the data from another activity and show into Main Activities recyclerview.
3. Now, On List item click start the previous activity with startActivityForResult() and pass the full list or the specific list item object as per your list complexity.
4. On The Launched screen get the data from intent and show the filled data to the user.
5. As soon as user makes any change to the data and clicks on submit then finish the activity and you should handle the onActivityResult on the previous screen to update the recycler's view data.
Also, you can store the data in Shared Preferences and while launching the new screen you can get the same data from shared preferences.
There are three major way to communicate component of android we can say pass data, which is bellow
Using BroadcastReceiver: using broadcast receiver you can transfer data between tow activity or activity or service, bellow is the link which will help you
https://developer.android.com/guide/components/broadcasts
Using static variable: You can declare the static variable and set value when you want to use, Like if I am declaring static variable A in Activity1 so I can change or access value of variable A from any of activity using Activity1.A.
Using Intent when starting activity: when we starting activity we can send data using intent put extra.
You can use broadcast, SharedPreferance and Static variables in 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.
I have created an app with two activities as the activities with two separate lists.
Let's call them: Activity1 and Activity2.
With the home screen of the app called the MainActivity.
As soon as the app is launched, the MainActivity is created and notifications appear in the notification bar based on the list item's statuses.
I've had a look at the lifecycle of Android but I can't understand and figure out where to load the lists and when to save the lists. Also, what is the best way to save large list data in Android to internal storage? Json? Csv? File type basically? :)
For example, would I load both lists when the MainActivity is first created, onCreate(). And then save the list of(for example) Activity1 when the user navigates away from Activity1, onPause()?
To reiterate, this is what I need help on:
I've had a look at the lifecycle of Android but I can't understand and figure out where to load the lists and when to save the lists. Also, what is the best way to save large list data in Android to internal storage? Json? Csv? File type basically? :)
Thank you for any help! :)
As you may have already known, onCreate will be call when Activity is instantiated, so all initialization, including populating the Activity with list data loaded from a file or shared preference. In terms of when to save the list data, it totally depends on how you want to modify the data, as data only needs to be saved when it's changed from its original state.
Of course, you can also load data every time onResume is called, and save data again when onPause is called, just to make sure data is synchronized if you have multiple applications modifying the same list data.
There are many different ways to save list data. I would recommend to use SharedPrefernce as it will save you some troubles. Please read this for how to save ArrayList to SharedPreference. If you don't like this, you can always save the data to a file with some standard format (JSON, XML, etc.) and parse the data when reading from and writing to the file.
A good entry point to load a data is in "OnResume". It is called every time your activity will be presented to the user.
Saving your data can be placed in "OnPause".
"OnCreate" is only called when the activity will be created, which will happen only once for your MainActivity.
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 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.