My Android Application has only one activity called MainActivity. When I press the back-button and run my application again, the previous activity will be destroyed. Now I want to know that is it possible to save the exact previous activity?
Thank you
Pressing the back key kills your current activity. A user would not expect it to be saved as there is no way to go "Forward" .
You can save the important parts of your activity in SharedPreferences or a database when you press back and restore it to its previous state when the activity next starts which would give the impression that it was "saved"
Activities are created in a stack, when you create A from B then B is now at the top of the stack, when you press the back button you are telling android that the top level of the stack (current activity) is no longer needed and it gets removed.
You will have to save state yourself and restore it if that's what you need to do when B is recreated.
one way you can use sharedpreferences for saving state of TextView,EditView ...etc.and resigning again when activity recreated.
Thanks.maybe helpful :-)
Related
I know that in Android, if stuff is idle for a while, the operating system will devour things to free up memory.
So if I have a first Activity, and I invoke a second Activity by using an Intent, and then invoke a third Activity using yet another Intent, I can use the back button to go back to the previous Activities.
But let's say I stay on the third Activity, and let the phone idle for a while until the OS decides to devour my app for memory. If I open the app again, will I have lost the stack I have formed from my Intents? Will I still be in the third Activity with the ability to press Back and go to Activity 2, then Activity 1?
The OS will handle how long a particular Activity stays "active" in memory. However, the "stack" shouldn't change, regardless of whether an activity is "active" or not. This is where the Bundle comes in handy and the methods: "onSaveInstanceState()" and "onRestoreInstanceState()".
Implementing these methods properly is the difference of the activity reappearing on the screen in an empty state vs. with its previous state maintained.
Some documentation on Recreating an Activity
The backstack stays intact for the task in hand. It will always stay in task and whenever the user presses the back button, it will go through the back stack like popping the last item. However, not all the activities in the stack are in the foreground. Usually, only the last item put into the back stack (the top of the stack) is in the foreground and if there are multiple apps/tasks open this may not even be true. Here is a great diagram to show this.
Now, lets say a user opens a task with a couple of activities in its back stack. The top activity is in the foreground and is running normally, but to preserve memory the other activities were destroyed. So now when the user presses the back button, the task knows what activity was in the back stack and knows it is now destroyed. So, it will recreate it following the Activities lifecycle and any data that was in it will be lost. One way to preserve it (mentioned by original answer) is using the onSaveInstanceState() and onRestoreInstanceState(), which save things in a bundle that Android preserves for the user so data can be saved. All of this information can be found in the docs. To answer your question more clearly, yes you will be in the activity you think you would be in, but you can treat it as a new instance of that activity and to recover the data from before to display it in the same way, you should use bundle and implement the methods aforementioned.
I've two activities, A and B.
My app goes from A to B. (A -> B).
When I'm on B and press the back button (hardware back button) the state and UI of A is restored successfully (onResume() is being called).
The problem is, when I press the home button (Actionbar arrow) the previous Activity A calls onCreate() so its state and UI won't be restored as with back button press.
Why is this happening? How can I solve it?
It seems you create a new instance of Activity A when you hit the up button on the ActionBar, instead of going back to the already existing Activity A.
You should override the listener that is invoked when you press the button and call finish() on Activity B instead. So the behavior will be the same as pressing the device's back button.
http://developer.android.com/training/basics/activity-lifecycle/recreating.html
Google has a great discussion on the Developers website. Anytime you leave the app you must save information needed to recreate the last state of Activity A because the system can remove your app from the cache at anytime after it closes. So save important db keys etc... into a file on the system with those keys needed to reload the activity. This will also handle orientation changes for you. The important lifecycle methods are onPostResume to restore state and onStop and onDestroy to save state.
Read about activity lifecycle too.
I have activities in sequence as Activity A calling Activity B,When I am on Activity B I press Home button and start another Application (for example Map). If i stay on this second application for a long time say 5-10 minutes and then press Home Button again and choose to Start my application again, then Activity B is started again and works fine. But when i Press Back key - I return to my Activity A again (which is also correct) but it shows a Blank Screen. Ideally in correct version it should show me Acitivty A with the XML data is ListView form.
Alternatively, in the above description, when the other Map Application is started and if the user stay on it only for 1-2 minutes then the above problem doesnt not occur.
Can anyone suggest a solution on the same.
Is it that i need to check in Activity B whether Activity A is still there in Activity Stack (How do i do the same) and If its not in my Activity stack then re-create it.
I tried doing alwaysRetainTaskstate in my Android manifest file for Activity A. But it doesnt work at all
You don't have to do any of that, Android takes care of the technicalities for you - it will recreate your Activity A.
You just need to remember to save A's state when B is opened (take a look at Activity.onSaveInstanceState). When A is restarted, your saved state will be passed to onCreate.
You should really read about Activity Lifecycle
This should help.
I have two buttons on my application. One button starts a new Game (lets say solitair).
When ever this button is pressed it will always start a new game
My problem is that I would like a second button to go back to the game that has already started if the user clicks back.
How do I go about recalling that activity that has already been created
Thanks
I assume that the game is from a third party. As I see it you don't even need second button. Just click the first one again and you will get back to the game.
If the game intercepts Back it may intentionally destroy it's state so there is no way to jump back to the place that you were before hitting Back.
You have to save the state of the activity
onSaveInstanceState() and onRestoreInstanceState() are used to handle the activity state
onSaveInstanceState() method gets called for an activity when user leaves the activity. Note this method is only called when activity is present in the History state and user can come back to the activity.
onRestoreInstanceState() restores the state of the views.
see the following link
How to manage activity state
You have to read manual about activity stack
Your application can has more than one task and more than one activity back stack
My question is about restoring complex activity related data when coming back to the activity using the "back" button".
Activity A has a ListView which is connected to ArrayAdapter serving as its data source - this happens in onCreate().
By default, if I move to activity B and press "back" to get back to activity A, does my list stay intact with all the data or do I just get visual "copy" of the screen but the data is lost?
What can I do when more than activities are involved? Let's say activity A starts activity B which starts activity C and then I press "back" twice to get to A. How do I ensure the integrity of the A's data when it gets back to the foreground? PrefsManager does not seem to handle complex object very intuitively.
Thanks, Rob
Activity A has a ListView which is
connected to ArrayAdapter serving as
its data source
Note that an ArrayAdapter is not a persistent store.
By default, if I move to activity B
and press "back" to get back to
activity A, does my list stay intact
with all the data or do I just get
visual "copy" of the screen but the
data is lost?
That depends.
Typically, Activity A has not gone anywhere. However, if the user leaves the app for an extended period, Android may destroy Activity A, leaving a placeholder in the activity stack, to free up memory. If, later, the user presses BACK, Android will re-create Activity A. You need to hang onto your data in this case, either by using a persistent store (file, database, etc.) or by using onSaveInstanceState() and onRestoreInstanceState().
You can find more in the Activity Lifecycle section of the Activity class description.
Unless you actively finish() Activity A when you start Activity B, its data and state will all be present when you return to it, regardless of how many activities you stack up.