0

My app has many dialogs. When I change the orientation of the device, the dialog disappears along with the content entered, because activity restart is triggered on orientation change. I have two choices: save the content of every dialog in variables and when activity restarts I reopen the one that was opened and refill it. This creates a LOT of boilerplate code and it also gets confusing a bit with many dialogs. My other solution is to use android:configChanges="orientation" in the manifest file, which will prevent activity restart. (My layout doesn't change on different orientations so it's okay) But many people suggested that this is a bad practice, as it prevents the activity from restarting on other configuration changes.

Is there a way to prevent activity restart only on orientation change and keep it on other configuration changes?

Nitesh Verma
  • 2,460
  • 4
  • 20
  • 36
Nicolas
  • 6,611
  • 3
  • 29
  • 73
  • https://developer.android.com/guide/topics/manifest/activity-element.html#config it may help you to take the decision – Shahab Rauf Oct 25 '16 at 12:12
  • Do you want your app to work in both `landscape` and `portrait` mode? If no, you can just set `android:screenOrientation='landscape'` on each activity tag in manifest and you are done. – Nitesh Verma Oct 25 '16 at 12:21

4 Answers4

1

From configChanges documentation:
Specify one or more configuration changes that the activity will handle itself. If not specified, the activity will be restarted if any of these configuration changes happen in the system.

So it shouldn't have impact on other configuration changes except orientation if you set android:configChanges="orientation|screenSize".

dzikovskyy
  • 5,027
  • 3
  • 32
  • 43
  • Documentation says I should set `android:configChanges="orientation|screenSize"` will that impact anything else than orientation? – Nicolas Oct 25 '16 at 17:03
  • You're right, `screenSize` should also be added because it also changes when a device switches between portrait and landscape orientations. (Updated my answer). This will not impact anything else. – dzikovskyy Oct 25 '16 at 17:31
  • My toolbar items used to adjust to available room but now they don't. How can I recreate the actionbar so it updates? – Nicolas Oct 26 '16 at 00:31
  • It's because now activity is not recreated on screen rotation. Check out this solution for dialogs http://stackoverflow.com/a/15729764/3185707. – dzikovskyy Oct 26 '16 at 09:47
  • Can I just recreate the toolbar and not the rest? – Nicolas Oct 26 '16 at 11:10
  • Not sure if it's possible as activity is not recreated. – dzikovskyy Oct 26 '16 at 15:40
1

check this https://stackoverflow.com/a/15444485/1577428

Dialog disappears because in the rotation it restarts the activity so that dialog instance has to be saved and bought back after the rotation this is the best practice so far

Community
  • 1
  • 1
Sajidh Zahir
  • 526
  • 4
  • 13
1

You can use configChanges, it's not a bad practice, but use it as per requirement. Check this SO post.

Community
  • 1
  • 1
pratiti-systematix
  • 792
  • 11
  • 28
  • This answer suggest adding this could prevent the app from restarting correctly. Is that the case with the orientation|screenSize parameters? – Nicolas Oct 25 '16 at 17:00
1

if you are not okey with above answer which seems not working for you. You can create one manager suppose DialogManager

public class DialogManager {

private AlertDialog  mAlertDialog;

public void setAlertDialog(AlertDialog alertDialog) {
    mAlertDialog = alertDialog;
}

public AlertDialog getAlertDialog() {
    return mAlertDialog;
}

}

and the instance is created in application class

public class SampleApplication extends Application {

private DialogManager mDialogManager;
private static SampleApplication mSampleApplication;

public SampleApplication() {
    super();
    mSampleApplication = this;
}
@Override
public void onCreate() {
    super.onCreate();
    mDialogManager = new DialogManager();
}

public DialogManager getDialogManager() {
    return mDialogManager;
}

public static SampleApplication getApplication() {
    return mSampleApplication;
}

}

so whenever you change screen orientation you can set dialog instance in DialogManager , as application creates it's instance only once.

please let me know if these solution not work for you.

Jitesh Mohite
  • 31,138
  • 12
  • 157
  • 147