0

I have an ActionMode in my fragment that I want only to run in the portrait mode; so I called actionMode.finish() in the onConfigurationChanged() fragment callback to stop the ActionMode if the orientation in the landscape:

override fun onConfigurationChanged(newConfig: Configuration) {
    super.onConfigurationChanged(newConfig)
    if (orientation == Configuration.ORIENTATION_LANDSCAPE)
        actionMode.finish()
}

This does work but Android studio warns me that "Calling finish() within onConfigurationChanged() can lead to redraws".

Is there a better way to finish the ActionMode in landscape without having to worry about that redrawing?

1 Answers1

0

For example you have two activities: MainActivity and SecondActivity.

Attributes in Manifest should be:

<activity
        android:name=".MainActivity"
        android:screenOrientation="fullSensor"
        android:configChanges="orientation|screenSize"
...
<activity
        android:name=".SecondActivity"
        android:screenOrientation="fullSensor"
...

MainActivity starts SecondActivity when screen is rotated into LANDSCAPE:

@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.