I have an activity. When I change the orientation, onCreate is called as we know. A new activity is created. But I want to continue with my old activity, is there a way to restore the old one and continue with it?
Use this in your activity tag
android:configChanges="orientation"
Related
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
I am having some problems with an activity of my Android aplication.
I have discovered that my app enters twice in 'onCreateView()' method of this activity when orientation changes, and it is causing me problems. When I click the corresponding button and this activity starts is all ok, but whithin this activity when the orientation changes I have some problems because enters twice, i.e. activity restarts twice ...
... Does anyone know the behaviour of an activity when orientation changes? Why does this happen?
Thanks so much.
Since onCreateView() is not part of the Activity lifecycle, but rather is part of the Fragment, I would assume you have a fragment somewhere in your activity.
Also I would assume you add this fragment manually using FragmentManager and FragmentTransaction. Since Android keeps all the added fragments between orientation changes, it is quite possible that you don't check if your Fragment is already present in this activity, so you add it again which causes additional onCreateView to be called.
If this is the case - you need to check if your savedInstance is null within onCreate. If it is - you add fragment. Otherwise activity is getting restored, so fragment is already there.
A lot of assumptions for the answer, I know. But I'm still mastering my telepathy skills :)
When orientation changes onDestroy() is called, followed by onCreate().
For more : http://developer.android.com/guide/topics/resources/runtime-changes.html
Try to add this in AndroidMainfest.xml in your activity:
android:configChanges="orientation|screenSize|keyboardHidden"
This means not to recreate the activity when orientation changes.
I would like to ask if the following behaviour is possible and, if it is, how I can achieve it. I need this behaviour for a very specific reason.
I have two activities, I will call them Activity A and Activity B. Now let's look at this example:
From activity A, in portrait orientation, I launch activity B (which has a fixed landscape orientation). The user, as the activity B is displayed in landscape, obviously rotates the device. The activity B is finished and activity A is resumed. The user keeps the device in landscape orientation so the orientation of the activity A will immediately be changed just after is resumed.
What I want is avoid that the orientation of the activity A changes when the user goes back to it. I want to resume the activity A in the last orientation it had and only allow orientation changes after that.
Locking the orientation is not an option as I want to allow the user to change it anytime.
The android:configChanges="orientation|screenSize" solution is neither an option as I want my activity to be re-created after an orientation change.
Do I have an option to achieve what I want?
Thanks!
EDIT: I forgot to say that saving the current orientation before the activity A is destroyed and restoring it when it is created again is not an option too. Activity A should NOT be re-created after activity B finishes.
EDIT 2: I was thinking now and I realized that I could rewrite the question from another point of view.
I would like to avoid the re-creation of the activity A when it is resumed after activity B finishes.
Afaik, when the device orientation change occurs, a message is sent to ActivityThread (with value RELAUNCH_ACTIVITY). That causes the activity A to be re-created with the new screen orientation.
I want to avoid the activity A relaunch as that would solve my problems and would keep the last orientation it had.
I don't think it is possible to do this as you've described. When the user returns from the landscape activity to your previous activity you would need to lock the orientation to portrait (in order to ignore the landscape orientation that the phone is now in). Now, if the user rotates the phone, you won't see the rotation and you won't be able to change the orientation. Since you can't really tell whether the user just rotated the phone or if the rotation is due to returning from a landscape-only activity, you can't tell these 2 conditions apart.
However, assuming that you can somehow differentiate between these 2 cases, try the following:
If you don't want Android to recreate your activity on an orientation change (even only in certain circumstances), then you will need to tell Android that you want to handle orientation changes by yourself by setting
android:configChanges="orientation|screenSize"
in the manifest.
Now you'll need to implement onConfigurationChanged() to get the behaviour that you want. In the case where your activity has launched another activity with fixed orientation, you'll want to save the current screen orientation before launching that activity. When the user returns to your activity Android won't destroy it and create a new instance it will just call onConfigurationChanged(). When you get the onConfigurationChanged() call you'll need to check if the user just rotated the device or if this is the result of returning from an orientation-locked activity. In the case where this is the return after launching another activity you can then lock the screen orientation to the previous orientation by calling setRequestedOrientation(). In the case where the user just rotated the phone, you can then either:
Recreate the activity by calling Activity.recreate() Note: this only works if you are running on API level 11 or higher
Recreate the activity by calling your own method that basically does everything necessary to recreate the Views (call setContentView()) and reset your member variables as if the activity were recreated
Recreate the activity by launching your activity again: startActivity(new Intent(this, this.class) and then calling finish() to get rid of the current instance.
You, probably could store your activity's current orientation in shared preferences in onStop() and restore it in onResume()
I have set setDisplayHomeAsUpEnabled to true and when home is pressed I want the user to go back to the very first Activity.
But in case FirstActivity already is created I don't want to recreate it. I'm currently adding FLAG_ACTIVITY_CLEAR_TOP to the intent that starts FirstActivity. Are there other flags I need to add (or use another flag altogether)
to get the desired behaviour of only creating the Activity if it doesn't exist or is the flag I have sufficient?
you can use singleTop launchmode and override onNewIntent method (this will be called if activity is relaunched instead of new one )
you can't assume that root activity is still alive, In Android 4.0 and greater devices have the developer option called don't keep activities, if it's enabled when you leave from one activity to another the parent activity automatically killed by the system.
My app seems to behave perfectly except I do not understand one thing. When I press the home button and return to my app, the previous state is restored (automatically). When I press the home button and then the phone goes to sleep in the home screen (or any other view except my own) and I return to my app, the previous state is restored (automatically).
Now, the kicker is that when the phone goes to sleep with my activity on top, it does NOT automatically restore the previous state but seems to call onCreate() because the starting initialization occurs and my app behaves as if it just started.
I am able to save the instance and recall the stored state using saveInstanceState() so ultimately the problem is solvable, but my question is:
Why does Android call onCreate() if it went to sleep from my activity but not from another?
And if it does not call onCreate() what is happening and why do I have to bother with saveInstanceState() since in every instance (barring low memory) my app returns the user to their last "placemark" EXCEPT when the phone goes to sleep from my activity.
Check whether your activity has locked the orientation to portrait or landscape, if you have locked the orientation, it will call onCreate() as it changes the orientation before it sleeps and when it wakeup.
you can solve this by adding android:configChanges="orientation" in to activity tag in the manifest.
<activity
android:name=".MainActivity"
android:screenOrientation="portrait"
android:configChanges="orientation"
/>
When I am having issues with the life cycle methods what I would do is add onCreate,onPause,onResume etc etc in all my activities with a log.D("in onPause()") inside to trace down exactly which method is being called at what time.
Android would only call onCreate if it previously destroyed your activity, in which case onDestroy would have been called. The situations where your activity state was preserved without onCreate being called were simply ones where your activity was never destroyed, it was left running the whole time.
Here is an overview of when, and how, to save/restore your instance state.