My activity restarts when I rotate my phone.
How can I fix this problem?
add
android:configChanges="orientation"
to your Activity in AndroidManifest.xml.
Save your non-view instance state in onSaveInstanceState or in onRetainNonConfigurationState and restore state in onCreate. Save "shared document-like data" in onPause() by writing to a database and restoring "shared document-like data" in onResume().
Also see the docs.
There are various posts on this, but for Android Version 3.2 and newer you should add android:configChanges="orientation|screenSize" to the activity in your AndroidManifest.xml, as the screen size changes between landscape and portrait orientation.
Related
On rotating screen, it seems activity is getting restarted, it loses all it's state. I checked android developer guide.Handling Runtime Changes. It has mention about how to handle screen orientation and using onSaveInstanceState() before it destroys your activity and restore the state during onCreate() or onRestoreInstanceState().
I am answering to my question this is how it worked for me, if anyone is facing same problem, then edit your AndroidManifest.xml on
android/app/src/main and
local-cli/generator-android/templates/src/app/src/main
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:configChanges="keyboard|keyboardHidden|orientation|screenSize">
Here is a detailed explanation of how to save/retrieve activity instance in Android
https://web.archive.org/web/20160308105435/http://www.intertech.com/blog/saving-and-retrieving-android-instance-state-part-1/
I am launching camera intent from my android application using this answer (I need user to be able to choose image from camera or from gallery)
But it seems that my whole app is recreated - activity's onCreate is called, and one of the classes, that is a singleton, has toString() value, that differs from the value before camera launch.
Here is also my activity description from android manifest:
<activity
android:name="xxx"
android:theme="#style/NoActionBarTheme"
android:screenOrientation="portrait"
android:configChanges="orientation|keyboardHidden|screenSize"
android:label="#string/app_name">
</activity>
Any ideas what is going wrong?
Make sure uncheck "Dont Keep Activities" from Developer Options in Settings.
Also check your manifest and code if your activity has correct launchMode.
On some devices(i observed samsung s3), when launch camera app, device orientation is changed to landscape & again to portrait when select captured photo to return to our app. Our activity is re-created again & we end up as just started activity.
In this case, you have to implement following pair of callbacks for activity. OnSaveInstanceState() & OnRestoreInstaceState().
You can save member variables & restore them back when come back to your activity from camera app.
e.g. imagePath (location you give for saving camera picture) can be saved & restored.
I have developed an application and it contains web view.
My issue
When my phone's orientation changes from portrait to landscape the whole application loads again and the web view reloads showing the first page of website.
So I am getting confused about the screen orientation or saving the data during that phase, so how do I fix it...
Yes, in Android the Activity is destroyed and recreated when your change the screen orientation. Thus you need to restore your applications state on recreation. On way is to use the callback method onSaveInstanceState() which you can use to save the state in a Bundle.
The Activities are explained here: http://developer.android.com/guide/components/activities.html and I suggest you take a look on that page for examples and more detailed instructions.
The default behaviour is to restart the activity when a configuration change happens (such as orientation change).
To override this you need to tell the system you'll handle orientation change yourself by adding this to your manifest file in your <activity> element:
android:configChanges="keyboardHidden|orientation|screenSize"
You may also want to override onConfigurationChanged which will be called when such a change happens.
See http://developer.android.com/guide/topics/manifest/activity-element.html#config
The Android Activity lifecycle clearly indicates this behaviour .
What happens
Whenever you start an Activty is gets created(after onStart() method) the
onCreate(Bundle onSavedInstance)
The variable onSavedInstance mentioned above initially recieves null as nothing is saved.
But during the orientation the whole layout hierarchy has to adjust according to the new mode from an existing mode(from portrait->landscape or vice-versa).This change may remove the previous data that you had in your activity.So to avoid such a problem(loosing data), there is a method
onConfigurationChange(Bundle saveSomething)
This method will be called for you to handle some configuration changes into this,
The default implementation will save you some data like some text in an editText.
Note This method as far the specs goes should be used to save some trivial data.
Example
Suppose you had applied a background colour to the activty layout and now you rotated it default implementation won't save it but onConfigurationChange if you want you can save it like this
saveSomething.putInt("color",1);
Inside onCreate
protected void onCreate(Bundle onSavedInstance){
if(onSavedInstance!=null){
int color=onSavedInstance.getInt("color");
if(color==1){
setBackgroundColor(Color.BLACK);
}
}
}
Add the following line inside the activity element of your manifest file you will be handling the changes in configuration
android:configChanges="keyboardHidden|orientation|screenSize"
As I understand it, if you insert 'android:configChanges="orientation"' into the activity in the manifest, the activity will not be destroyed and recreated on an orientation change. To test this, I created a dead-simple app which does NOTHING. Then I inserted 'android:configChanges="orientation"' in the manifest. Then I added the following method:
#Override
public void onConfigurationChanged(Configuration newConfig) {
Log.v(TAG,"onConfigurationChanged:");
super.onConfigurationChanged(newConfig);
}
However, I'm still seeing onCreate() being called. The activity is still being recreated.
As if that weren't strange enough, I don't see onConfigurationChanged() being called when I put the emulator into landscape mode (Ctrl-F11). It's only called when I go back to portrait mode. Shouldn't it be called both ways? Isn't the configuration (orientation) being changed when I go into landscape as well as portrait modes? This makes no sense.
Anyway, this whole activity and orientation thing is driving me crazy.
Your screen size changes from 1200x800 to 800x1200 (for instance).
Since API 13, this will also raise a screenSize config change. The fix is:
android:configChanges="keyboardHidden|oritentation|screenSize"
However, I'm still seeing onCreate() being called. The activity is still being recreated.
The emulator emulates a device with a side-slider keyboard. The android:configChanges value that matches your - would be keyboardHidden, generally used in conjunction with orientation to handle non-keyboard devices (e.g., android:configChanges="keyboardHidden|orientation").
That being said, android:configChanges is not recommended in most cases. Use dynamic fragments and setRetainInstance(true), or use onSaveInstanceState() and onRetainNonConfigurationInstance() to allow the activity to be destroyed and recreated.
When ever i change in to another orientation the Activity onCreate() method is called again?
how ever i used
android:configChanges="orientation|keyboardHidden"
but it prevent to change another orientation.how to block on calling the OnCreate() again?
Thanks in advance...
Whenever the orientation changes, the application starts over in the onResume() method.
If you do not want it to change orientation when the user flips the phone, you need to set this in your manifest file:
<activity android:name="YourActivity"
android:screenOrientation="portrait" />
Or landscape if you wish.
If you do want your application to continue where it left of before the user changed orientation on the phone and you do want your application to change orientation too, you need to keep track of the state of the application and set these values in your onResume()-method.
This is a little dirty, but you could simply set a flag-style boolean for the activity. First time through, set the "already ran onCreate" flag to true and use that to avoid running the code multiple times.
Not very proper, but should work.