Conditional restart on Activity onConfigurationChanged
Asked Answered
C

1

5

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
    }
}
Comprehend answered 19/3, 2010 at 7:28 Comment(0)
S
7

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, ...).

Stylistic answered 19/3, 2010 at 10:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.