Search code examples
androidorientationrestartoncreateactivity-finish

A finished Activity re-created after a coincident orientation change


I don't know if this behavior is normal because I suppose a call to finish() would stop the activity from being re-created.

Here is an excerpt of the activity class:

public class MyActivity extends FragmentActivity {

    private RetainFragment retainFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        ......

        retainFragment = getSupportFragmentManager().findFragmentByTag("RetainFragment");
        if(retainFragment == null) {
            retainFragment = new RetainFragment();
            getSupportFragmentManager().beginTransaction().add(retainFragment, "RetainFragment").commitAllowingStateLoss();
        }
        if(retainFragment.isFinish) {
            Log.v("MyActivity", "isFinish == true");
            this.finish();
        }
    }

    // a on-click event handler for a finish button
    public void onFinishClicked(View view) {
         retainFragment.isFinish = true;
         this.finish();
    }

    private class RetainFragment extends Fragment {
        private boolean isFinish = false;

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            this.setRetainInstance(true);
        }
    }

}

The activity is persistent(I store the other persistent variables in the RetainFragment too but I didn't show them) and is closed only after the user has clicked the finish button, at which point onFinishClicked(View) would be called, and the activity should be finished by the system. I don't anticipate it to be recreated by the system after a coincident screen rotation. This happens rarely and I handle it by calling finish() again within the onCreate() method. However, it looks pretty ugly here because a finished activity is supposed to be dead forever and now I have to explicitly handle it. :( Is there any misunderstanding of the activity lifecycle or the retain fragment on my part?


Solution

  • This is a standard Android behavior. Take a look at this paragraph of Android documentation.

    Unless you specify otherwise, a configuration change (such as a change in screen orientation, language, input devices, etc) will cause your current activity to be destroyed, going through the normal activity lifecycle process of onPause(), onStop(), and onDestroy() as appropriate. If the activity had been in the foreground or visible to the user, once onDestroy() is called in that instance then a new instance of the activity will be created, with whatever savedInstanceState the previous instance had generated from onSaveInstanceState(Bundle).

    To avoid this, you can set android:configChanges attribute in its manifest. For any types of configuration changes you say that you handle there, you will receive a call to your current activity's onConfigurationChanged(Configuration) method instead of being restarted. If a configuration change involves any that you do not handle, however, the activity will still be restarted and onConfigurationChanged(Configuration) will not be called.