How to fix warning: "Calling ActionMode finish() in onConfigurationChanged() can lead to redraws".
There are two activities: MainActivity and SecondActivity.
MainActivity
should be displayed in the portrait orientationSecondActivity
should be displayed in the landscape orientation
We need such attributes in Manifest:
<activity
android:name=".MainActivity"
android:screenOrientation="fullSensor"
android:configChanges="orientation|screenSize"
...
<activity
android:name=".SecondActivity"
android:screenOrientation="fullSensor"
...
MainActivity
starts SecondActivity
when the screen is rotated into LANDSCAPE
:
import static android.content.res.Configuration.ORIENTATION_PORTRAIT;
...
@Override
public void onConfigurationChanged(@NonNull Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if (ORIENTATION_LANDSCAPE == newConfig.orientation) {
startActivity(new Intent(this, SecondActivity.class));
}
}
SecondActivity
checks on create if current orientation is LANDSCAPE
, otherwise it finishes:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (ORIENTATION_PORTRAIT == getResources().getConfiguration().orientation) {
finish();
return;
}
setContentView(R.layout.second_activity);
...
}
When attribute configChanges="orientation|screenSize"
is not present - activity will be recreated on each screen rotation, otherwise - method onConfigurationChanged
is called.
- Start in portrait orientation:
MainActivity.onCreate()
MainActivity.onStart()
MainActivity.onResume()
- Rotate into landscape orientation:
MainActivity.onConfigurationChanged()
MainActivity.onPause()
SecondActivity.onCreate()
SecondActivity.onStart()
SecondActivity.onResume()
MainActivity.onStop()
- Rotate back into portrait orientation:
SecondActivity.onPause()
SecondActivity.onStop()
SecondActivity.onDestroy()
SecondActivity.onCreate()
MainActivity.onConfigurationChanged()
MainActivity.onRestart()
MainActivity.onStart()
MainActivity.onResume()
SecondActivity.onDestroy()
- Start in landscape orientation:
MainActivity.onCreate()
MainActivity.onStart()
MainActivity.onResume()
MainActivity.onPause()
SecondActivity.onCreate()
SecondActivity.onStart()
SecondActivity.onResume()
MainActivity.onStop()
- Rotate into portrait orientation:
SecondActivity.onPause()
SecondActivity.onStop()
SecondActivity.onDestroy()
SecondActivity.onCreate()
MainActivity.onConfigurationChanged()
MainActivity.onRestart()
MainActivity.onStart()
MainActivity.onResume()
SecondActivity.onDestroy()
- Rotate back into landscape orientation:
MainActivity.onConfigurationChanged()
MainActivity.onPause()
SecondActivity.onCreate()
SecondActivity.onStart()
SecondActivity.onResume()
MainActivity.onStop()
- Start in portrait orientation.
- Minimize application:
MainActivity.onPause()
MainActivity.onStop()
- Rotate into landscape orientation.
- Restore application:
MainActivity.onConfigurationChanged()
MainActivity.onRestart()
MainActivity.onStart()
MainActivity.onResume()
MainActivity.onPause()
SecondActivity.onCreate()
SecondActivity.onStart()
SecondActivity.onResume()
MainActivity.onStop()
- Start in landscape orientation.
- Minimize application:
SecondActivity.onPause()
SecondActivity.onStop()
- Restore application:
SecondActivity.onDestroy()
SecondActivity.onCreate()
MainActivity.onConfigurationChanged()
MainActivity.onRestart()
MainActivity.onStart()
MainActivity.onResume()
SecondActivity.onDestroy()