The first Activity is destroyed when pressing Back button from SecondActivity

479 views Asked by At

I'm implementing an Android app playing online Video and get the error. For the simplicity, I made an sample app here. There are 2 activities: MainActivity and SecondActivity.

MainActivity

public class MainActivity extends Activity {
    public static String TAG = "MainActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.e(TAG, "onCreate");
        setContentView(R.layout.activity_main);

        ((Button)findViewById(R.id.btn)).setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent intent = new Intent(getApplicationContext(), SecondActivity.class);
                startActivity(intent);
            }
        });
    }

    @Override
    protected void onStart() {
        // TODO Auto-generated method stub
        super.onStart();
        Log.e(TAG, "onStart");
    }

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        Log.e(TAG, "onResume");
    }

    @Override
    protected void onStop() {
        // TODO Auto-generated method stub
        super.onStop();
        Log.e(TAG, "onStop");
    }

    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        Log.e(TAG, "onDestroy");
    }
}

SecondActivity

public class SecondActivity extends Activity {
    public static String TAG = "SecondActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
    }

    @Override
    protected void onStart() {
        // TODO Auto-generated method stub
        super.onStart();
        Log.e(TAG, "onStart");
    }

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        Log.e(TAG, "onResume");
    }

    @Override
    protected void onStop() {
        // TODO Auto-generated method stub
        super.onStop();
        Log.e(TAG, "onStop");
    }

    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        Log.e(TAG, "onDestroy");
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        // TODO Auto-generated method stub
        super.onConfigurationChanged(newConfig);
        Log.e(TAG, "onConfigurationChanged");
    }
}

AndroidManifiest.xml    

    <activity
        android:name="com.example.androidtest.MainActivity"
        android:screenOrientation="portrait"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name="com.example.androidtest.SecondActivity"
        android:label="@string/title_activity_second" 
        android:configChanges="orientation|keyboardHidden|screenSize"
        android:screenOrientation="portrait">
    </activity>

In MainActivity has a button to start SecondActivity. My scenario is putting the device in landscape mode and click button to start SecondActivity(so that SecondActivity is auto rotated to landscape mode). Then pressing Back button immediately to return back MainActivity. But the MainActivity is destroyed. Here is my log:

12-11 22:22:20.940: E/MainActivity(400): onCreate
12-11 22:22:20.960: E/MainActivity(400): onStart
12-11 22:22:20.960: E/MainActivity(400): onResume
12-11 22:22:24.524: E/SecondActivity(400): onStart
12-11 22:22:24.524: E/SecondActivity(400): onResume
12-11 22:22:24.544: E/SecondActivity(400): onConfigurationChanged
12-11 22:22:24.634: E/MainActivity(400): onStop
12-11 22:22:24.634: E/MainActivity(400): onDestroy
12-11 22:22:34.133: E/SecondActivity(400): onStop
12-11 22:22:34.133: E/SecondActivity(400): onDestroy

The MainActivity is destroyed and the current activity is SecondActivity. If I press Back one more, the SeconActivity is destroyed. Anyone can explain me why MainActivity is destroyed or this is error of Android OS. Thanks in advance.
P/S: For this case happens, putting the device in landscape, then clicking button to start SecondActivity and press Back immediately.
The OTHER cases works fine.

1

There are 1 answers

2
HannahMitt On

Your MainActivity is being recreated on rotation. To keep it from being recreated when the device rotates you'll need the same configChanges as the SecondActivity: android:configChanges="orientation|keyboardHidden|screenSize"