Android restore UI state on home button pressed? - android

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.

Related

Activity Lifecyle Difference in Device's Back Button vs Actionbar's Back Button

I'm currently learning the Activity Lifecyle. I noticed the following:
I have two Activitys, A and B.
When I open Activity B from Activity A, A gets stopped and B gets created and started.
When I press the Back Button on my device, B gets destroyed and A get restarted.
But when I use the Back / Up Botton of the Actionbar instead, B gets destroyed, A gets destroyed and then onCreate() is called.
Why gets A destroyed instead of restarted, when using the Up Botton in the ActionBar?
I hope my question is clear, if not please comment.
When you press the BACK button, this calls onBackPressed() in the current Activity. The default behaviour of that method (if not overridden in the Activity) is to call finish() on the Activity. This finishes the Activity and resumes the Activity which is underneath it.
The UP button is calling startActivity() with an Intent that is built like this:
Intent intent = new Intent(this, TargetActivityForUpButton.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
This code will remove all activities in the stack back to, and including, TargetActivityForUpButton. It then creates a new instance of TargetActivityForUpButton and launches that Actvity (you will see onCreate(), onStart(), onResume() called on the Activity.
See also the section "Navigate up to parent activity" in https://developer.android.com/training/implementing-navigation/ancestral
The device's back button is actually taking you back (to the previous activity). The action bar back button works similarly to an "Up" button (within the hierarchy of your app). This is why the action bar's back button won't take you outside of the app, whereas the device's back button will carry on taking you back, even outside of the app. The action bar exists within your app so it follows the activity's lifecycle methods and starts from scratch each time you go back, whereas the device is restarting from where it stopped.
EDIT:
The Back button appears in the system navigation bar and is used to navigate, in reverse chronological order, through the history of screens the user has recently worked with. It is generally based on the temporal relationships between screens, rather than the app's hierarchy.
(Read more here)

singleInstance launch mode: why is onCreate called after activity is brought to front?

I launch main activity, change some settings in UI, press back button and then reopen the activity. onCreate() is called again and activity is back to default state. Why is this?
I would expect only onResume() to be called since I have this in Manifest:
android:launchMode="singleInstance"
Try using moveTaskToBack(true); in onBackPressed() to make your current activity hide instead of destroying, so you can reopen it again
#Override
public void onBackPresses(){
moveTaskToBack(true);
}
I launch main activity, change some settings in UI, press back button and then reopen the activity. onCreate() is called again and activity is back to default state. Why is this?
This is expected behavior of activity life cycle. When you press back button then activity get destroyed, it will start from onCreate method.
This is because when you press back, it destroyes the activity.
I think that what you want is to keep the state when you come back to this activity, not really to have one instance of it. So you should save the information you need and then restore them in onCreate.
Single instance only means (if I remember it well) that if you launch multiple activities without finishing them, the ones that have singleInstance will go back to front when you will call startActivity() and will not call onCreate. But this means you can't have this behavior by pressing back.
Maybe you can override onBackPressed and start another activity (the one you should go back to) instead of calling super.onBackPressed(). this should do what you want but if think it will be kind of difficult to manage.
By the way remember that Activities might be killed by the system itself, so you should not rely on the fact that your activity as not been killed

Is it possible to save previous activity?

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 :-)

help calling an Intent / activity that has already been created

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

Different state storage behavior for "Back" vs "Home"

I am still learning the ins and outs of Android development. I am playing around with the Notepad tutorial application to try and get different behavior.
Right now, I want to have the application do the following in the NoteEdit activity:
1) If the Back button is pressed, current state is ignored; basically, it's like an implicit cancel, and you are taken back to the list.
2) If the Home button is pressed, it takes you to the home page as normal. However, if you open the application again, it should go back into the NoteEdit activity in the same state as when you left (IE, if you were partway through an edit, for example).
I removed the "saveState" stuff from onPause, because I don't want to store to the DB unless "Confirm" is pressed (instead, I moved the call to saveState to the confirm button). By doing this, hitting "Back" basically throws out your changes, which is what I want. However, going Home and coming back also throws out your changes, though it does remain in the NoteEdit activity. Both "Back" and "Home" cause the onPause message to trigger, and both cause onResume to trigger (either from clicking on the item in the "Back" case, or by going back into the app in the "Home" case).
Is there a way to have these two events handle saving the state differently? Is it possible to have the Home button store the state (temporarily), while not having the Back button do it?
Thanks in advance!
You need to define an onSaveInstanceState method, but instead of saving to the DB (as in the Notepad sample), save your Activity's state to the Bundle. You then need to recover from the saved state in your onCreate when the passed in Bundle is non-null.

Categories

Resources