Android how to prevent restart the activity on orientation change.

Android how to prevent restart the activity on orientation change.

For manual orientation change:
<activity
    android:name=".ActivityName"
    android:configChanges="orientation"/>

public class ActivityName extends Activity {
    ...
    onStart() {
        setRequestedOrientation(orientation);
    }
    onConfigurationChanged(Configuration newConfig){
        // do stuff for orientation change.
    }
    onClick() {
        setRequestedOrientation(orientation);
    }
}

For auto-orientation detect:
<activity
    android:name=".ActivityName"
    android:configChanges="orientation"/>


public class ActivityName extends Activity {
    ...
    onConfigurationChanged(Configuration newConfig){
        // do stuff for orientation change.
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *