Saving the instance of an Activity to reuse it - android

I have an Activity A with a button on it which when clicked navigates to Activity B. Now from this Activity B when the back button is pressed i get back to Activity A which is quite obvious. But once again when i click on the button on Activity A a new instance of Activity B is launched.
My query: Is there a way so that when i press the back button on Activity B it's instance is stored and so when i again click the button on Activity A the saved instance of B is launched instead of a new one.
Hoping for a solution..

if you don't want to pass data.. or just want to go back on Previous activity
finish() in onClick

http://developer.android.com/reference/android/app/Activity.html
In addition, the method onSaveInstanceState(Bundle) is called before placing the activity in such a background state, allowing you to save away any dynamic instance state in your activity into the given Bundle, to be later received in onCreate(Bundle) if the activity needs to be re-created.

To think that it won't be easy to deal with it. The activity in the android can be store but you cannot call the android system to using that instance of your when you navigate to the activity B.
Read this link http://developer.android.com/reference/android/app/Activity.html
it give all information that you can do with it but not all as you want.
When you navigate from activity B to activity A the android itself has been store your activity B's instance temporarily in the memory and that instance will destroy completely anytime later. If you navigate it from A to B again, then the instance of Activity B will be reuse again if it still exist in the memory otherwise it recreate the activity again. In any case the activity B is reuse or recreate, the method onCreate is always call (all method in Lifecycle like onResume ...etc) then all data of your activity B is now new data that's not your old data before you navigate it to the activity A. This is what android system doing so that you cannot launch activity B without calling those method.
if you want the activity B look like the same as before navigate to activity A you should probably saving the necessary data in the method onPause() on the activity B then set those data back to the component of activity B when it call onCreate(). The method onSaveInstanceState(Bundle) should not be use in this case because the android os version which is before honeycomb (3.0) is not invoked that method.
This could be difficult to deal if you are working with online data or listview, webview ...etc and also save data by your own it could the risk to your application itself.
Anyway I hope this could be some help to you.

Related

Return to previous Activity without deleting current state - Android

I wrote an Android app with several Activities and a Main Activity. When I go from the Main Activity to lets say Activity B, I want to "pause" the Main Activity, when the back button is pressed it should go back and reactivate the Main Activity instead of calling onCreate().
This shall work with all Activities, so if I click on a button to start Activity B, it shall also reactivate the old Activity B instead of creating a new state with onCreate().
How can I realize this?
PS: I already tried it with parcelable, but this do only work, if I close the application or something unexpected happens.
It's always possible that the first activity may be destroyed when you start another activity. It will be recreated when you go back to it. Every app should be written with this possibility in mind. To make sure your activities can handle being destroyed and recreated, turn on the "don't keep activities" developer option.
It is by default in Android. If you Start Activity B from Activity A then activity A goes in stopped state. Below methods will be called of Activity A
onPause()
onStop()
When you tap on Back key on Activity B. Below methods of Activity A will be called.
onRestart()
onStart()
onResume()
For reactivating Activity B, you can set Activity B launch mode as singleInstance. This will make sure that only single instance of Activity B will be created. onCreate will be not be called again. onNewIntent will be called when that activity is reactivated.
Refer: http://developer.android.com/guide/topics/manifest/activity-element.html#lmode
Activities live in stack order. Each activity has its life cycle. When you close an activity (Activity B in your example) it eventually reaches onDestroy() method and removed from the stack order. It is by default in Android and there's nothing you can do about it.
What you can do is rewrite the onStop() method in Activity B and save some activity data (like text in EditText, for example) in SharedPreferences - read here. Then in your onCreate() method you can call for SharedPreferences, check if it's not empty and restore the text in the EditText by pulling appropriate value by key.

Android how to save and restore state of Activity when calling finish()?

In Android, from Activity A, if I start new Activity B, Android will automatically save all the state of Activity A. So when I click back or call finish() from Activity B, Activity A will be restored to the state when I start Activity B (for example: position of a ScrollView in Activity A, or the value of ProgressBar). In Activity A I have a ProgressBar showing progress of some task, then if I come back from Activity B, the ProgressBar is restored and keep running.
Now what I want is, from Activity A, I will call finish(). How can I save all the state of Activity A just like Android did (include the ProgressBar as I describe above), so when I start Activity A again, I can restore everything like before calling finish(), and my ProgressBar keep running?
Can I push Activity A to something like stack in Android and then pop it out? Anyone know how to achieve this?
Do not call finish on clicking the back button.
Instead call the method "moveTaskToBack(false)". This will 'minimize' your application.
Next time you open the application the application opens with the previous state.
The whole point of finish() is to say that your Activity is "done and should be closed". If you aren't done, just don't call finish().
When you go from Activity A to Activity B, Android does not kill Activity A. Activity A still lives in the background. It can however be killed at any moment if Android needs the memory.
To save the state of an activity you have to do it manually.
Here's a similar question with a good answer: Saving Android Activity state using Save Instance State
If you want to finish your activity and then start it again just to load some new configurations, You can call activity's recreate() method instead of calling finish(). Calling recreate() will call onSaveInstanceState() and onRestoreInstanceState(), helping you to restore your previous configurations..
You can look at this answer for more detail : How do I restart an Android Activity

Who gets the Activity result when an Activity is re-constructed on configuration change?

As I understand it, if I don't explicitly tell Android that I want to handle a configuration change in an Activity, it will get torn down and re-created on, say, an orientation change.
Let's say that I have object O that's an instance of some class MyCustomClass in Activity A. When the user presses a button in Activity A's layout, we call O.startActivityB. That method will start Activity B. When Activity B gets re-created on an orientation change, I can tell (some flag was set in another or something), so Activity B immediately runs this:
setResult(RESULT_CANCELED);
finish();
return;
Who is Activity B's parent activity at this point? Who gets the result code?
If you started Activity B from Activity A, the 'parent' is Activity A. The fact that an activity gets recreated for some reason doesn't change the activity stack in any way. BTW, if you want to receive a result, you need to use startActivityForResult(), not startActvity().

How to save instance of an activity?

I am new going for the android.I am working on an app in which I have two activities let say A and B.
In activity A I have a list view of some items.I have a button in activity A which takes me to activity B.In activity B I have a seek-bar.
I am using the seek-bar to filter the result of activity A.
I have two buttons in activity B cancel and filter.
After adjusting seek-bar if user clicking filter button than it takes user to activity A and showing filter results.
User can play between activity A and activity B.
Now three different scenario are there for coming back from activity B to A.
By pressing filter button
By pressing cancel button
By pressing phone's back button
After adjusting seek-bar if user pressing filter button then activity A is re ordering and showing filter results. Here I want to save the instance of activity B. so that from activity A if user again going in activity B then I can show the previous state of activity B.(I am able to do this)
In second scenario if user adjusting the seek-bar again and pressing cancel button then Activity A is reordering.Here I do not want to save the instance of activity B and if user again going in activity B from activity A then I want to show the previous state of activity B.(I am not getting how to do this ??)
In Third scenario if user adjusting the seek-bar again and pressing phone's back button then Activity A is reordering but now if user again going in activity B from activity A then activity B is restarting that I do not want, here also I want to show the previous state of activity B.(I am not getting how to do this also ??)
I am stuck with this problem.
Thank you so much in advance.
You must consider each of the cases and manage the activity lifecycle accordingly. You did this just fine, the problem is how to manage it. So, the first step is to study this:
activity and lifecycles
After understanding how the lifecycle of activities is handled by the android os you're on your way: Manage the life of your second activity so that state is mantained by overriding the onPause method (which is called when your activity is no longer in the front of the application) or finish the activity if you don't want to save the state (effectively calling the onStop method.
I would solve this in this way:
in activity A i call the activity B with a startActivityForResult. This way, when the activity B is finished, it's state is not mantained. So, call finish() inside activity B to return to activity A without saving it's state.
When wanting to save the state of activity b, call the activity A so that the onPause method gets called and the state saved.
Hope this clarifies it for you.

Task, Back Button, and onSaveInstanceSate method

Hello I am just a little confused after reading these materials on Tasks and Back Stack, Android Developer Guide:
It says:
When Activity A starts Activity B, Activity A is stopped, but the system retains its state (such as scroll position and text entered into forms). If the user presses the Back button while in Activity B, Activity A resumes with its state restored.
While in the APIs reference for onSaveInstanceState() method, it says:
One example of when onPause() and onStop() is called and not this method is when a user navigates back from activity B to activity A: there is no need to call onSaveInstanceState(Bundle) on B because that particular instance will never be restored, so the system avoids calling it.
The above two situations seem identical to me ("press the Back button to Activity A" and "navigate back from Activity B to Activity A"). But I don't understand why while the former says Activity A resumes with its previous state restored while the latter says the particular instance of Activity B will never be restored. Any explanations?
Thanks in advance!
I think the first one is saying that A will be saved so it can be restored, and the second one is saying that B will not be saved because it can't be restored.

Categories

Resources