A better way to handle Activity restart on orientation change?

481 views Asked by At

There is an activity which should not exist when a user leaves it. That's why it has finish() method in the onStop.

@Override
protected void onStop() {
    super.onStop();
    finish();
}

However, this makes it restart each time the screen orientation changes. At the moment I handle this via Manifest Activity tag android:configChanges="orientation" and overriding method onConfigurationChanged

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    setContentView(R.layout.someactivity);
    doAllInitializations();;
}

Is there a better way to handle such situations?

1

There are 1 answers

1
Vineet Shukla On BEST ANSWER

You can stop this orientation by adding android:configChanges="orientation|keyboardHidden" in your activity tag in the manifest file.