Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conditional restart on Activity onConfigurationChanged

I want to make an activity that allows orientation changes on some condition, but not otherwise. More exactly I want to prevent restarting the activity when a background thread is busy.

I have put the configChanges attribute on the activity manifest, and onConfigurationChanged is called when the orientation changes. However I want to allow the app to change the orientation when allowed.

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);

    if (orientationChangeAllowed) {
        // how do I restart this activity?
    } else {
        // don't do anything
    }
}
like image 388
Randy Sugianto 'Yuku' Avatar asked Mar 19 '10 07:03

Randy Sugianto 'Yuku'


1 Answers

if it's allowed call setRequestedOrientation(), when it's not allowed, do nothing.

As a tip: You can use onRetainNonConfigurationInstance() and getLastNonConfigurationInstance() and return (an object containing) the AsyncThread. This way the Activity will change orientation when the user wants to. Take note though: You shouldn't leak a reference to a Context (may it be a reference to your Activity or a Drawable, ...).

like image 101
MrSnowflake Avatar answered Sep 30 '22 09:09

MrSnowflake