2

Assume my application contains two activity, A and B. Both are limited to portrait in AndroidManifest.

Activity A started Activity B. In Activity B, there is a button, which calls finish() when clicked.

The problem is... When I hold the device vertically(portrait) and click the button, the calling sequence is

B.onStop();
B.onDestory();
A.onStart();

However, when I hold the device horizontally(landscape), the sequence becomes

B.onStop();
B.onDestory();
A.onCreate();
A.onStart();

I do NOT want the A.onCreate()!!!

I tried pressing the Back button. A.onCreate() is not called. So... simulating the Back button is somehow the solution

I have tried the followings, all of them called A.onCreate()..

finish();

.

onBackPressed()

.

dispatchKeyEvent(new KeyEvent (KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_BACK));
dispatchKeyEvent(new KeyEvent (KeyEvent.ACTION_UP, KeyEvent.KEYCODE_BACK));

EDITED

I have to say again. Both activities are limited to portrait in AndroidManifest. onConfigurationChanged is never called.

Static variable is not accepted. Since this will cause other problem..

Peter Lo
  • 110
  • 1
  • 7
  • Adding `android:configChanges="orientation"` in Activity A can skip the `A.onCreate()`. But I expect a better solution. – Peter Lo May 08 '13 at 06:44
  • when orientation changes the activity is destroyed and recreated. If you don't want to activity to restart check the Handling the Configuration Change Yourself at http://developer.android.com/guide/topics/resources/runtime-changes.html#HandlingTheChange – Raghunandan May 08 '13 at 06:44
  • 1
    Check out this :http://stackoverflow.com/questions/8814551/how-to-stop-activity-recreation-on-screen-orientation – andy May 08 '13 at 06:52
  • If you're filling some fields on onCreate event, you can define a static Boolean variable and make it true after first calling onCreate event, then you can check it every time if the variable equals to true, don't fill fields. – Behzad May 08 '13 at 07:12

3 Answers3

3

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"
Lazy Ninja
  • 22,342
  • 9
  • 83
  • 103
  • I am using the same solution, as mentioned in my own comment to the question. Your statement is correct, and works. Just wonder if there is any better solution. I don't want to add this line to all activities in Manifest. – Peter Lo May 08 '13 at 08:24
  • @PeterLo, sorry I missed that. It depends why you don't want the onCreate() method to be called again. There should be some work around. – Lazy Ninja May 08 '13 at 08:54
0

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.

Alexander Kulyakhtin
  • 47,782
  • 38
  • 107
  • 158
0

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:

@Override
protected void onConfigurationChanged(Configuration newConfig) {
    newConfig.orientation = Configuration.ORIENTATION_PORTRAIT;
    super.onConfigurationChanged(newConfig);
}
xiang-ge
  • 1
  • 1