Android: Avoid Calling Oncreate() When Back From Another Activity
Solution 1:
It happens because you have changed the orientation at some stage when the application has started, imo. On orientation change the activity is destroyed then recreated. To avoid it handle the orientation. Handle the orientation change by yourself by adding the following line in the manifest file , under activity.
<activity android:name=".Activity_name"android:configChanges="orientation|keyboardHidden|screenSize"
Solution 2:
If you want not to call some code in onCreate
when the orientation changes you can do as follows:
in your activity override onRetainLastNonConfigurationInstance()
and make it return Boolean.TRUE
.
In your onCreate
check Boolean.TRUE.equals(getLastNonConfigurationInstance())
and, if yes, this means your onCreate has been called because (and only becase) the orientation has been changed.
Solution 3:
in your condition,Set
<activity android:name=".Activity_name"android:configChanges="orientation|keyboardHidden|screenSize"
can avoid Activity reCreate new instance,but some device will cause black screen and onCreate will still be called , if you want to prevent this condition,you can override onConfigurationChanged() method,and do this:
@OverrideprotectedvoidonConfigurationChanged(Configuration newConfig) {
newConfig.orientation = Configuration.ORIENTATION_PORTRAIT;
super.onConfigurationChanged(newConfig);
}
Post a Comment for "Android: Avoid Calling Oncreate() When Back From Another Activity"