Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to stop changing the orientation when a progress bar is spinning in android

I have a Registration screen where user enters all the registration details and when user clicks on the "Register" button i am showing the progress bar(Progress bar begins to spin) and then takes the user to the home screen.If the user rotates the phone when progress bar is spinning, the progress bar gets dismissed.I want the orientation not to change from potrait to landscape and vice versa,when a progress bar is visible(meaning it is spinning).How to stop the orientation change when progress bar is spinning?Any help would be appreciated.

like image 314
androidGuy Avatar asked Oct 13 '11 05:10

androidGuy


People also ask

How do I stop Android from restarting activity when changing orientations?

If you want the activity to not restart during screen orientation change, you can use the below AndroidManifest. xml. Please note the activity android:configChanges=”orientation|screenSize” attribute. This attribute makes the activity not restart when change screen orientation.

How do I restrict orientation on Android?

Add android:screenOrientation="portrait" to the activity you want to disable landscape mode in.

How do I turn off auto Rotate on Android?

Short guide: Tap the Settings icon to open the Settings app. Scroll down and tap Accessibility. Scroll down to Interaction controls and tap Auto-rotate screen to turn it off.


2 Answers

This is happening because when screen orientation rotates the Activity gets re-started. In this case you can add configChanges attribute in your tag in the AndroidManifest file to stop the re-creation of the Activity.

<activity android:name=".Activity_name"
          android:configChanges="orientation|keyboardHidden">

By, this your orientation can change will the progress bar is working and also it won't stop though the orientation changes.

UPDATE

@Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        if(newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE){
            setContentView(R.layout.login_landscape);
        }
        else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
            setContentView(R.layout.login);         
        }
    }
like image 78
Lalit Poptani Avatar answered Nov 09 '22 15:11

Lalit Poptani


As explained here, calling

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);

and then

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);

really works like charm... on real devices !

Don't think that it's broken when testing on the emulator, the ctrl+F11 shortcut ALWAYS change the screen orientation, without emulating sensors moves.

Some other references :

  1. Programatically enabling/disabling screen rotations in Android
  2. Android child activity and orientation lock

And here is Activityinfo

***************** Edited ********************************

And I think you are not able to handle progressbar on screen rotation. Am I right?

So you need not to stop the rotation for this solution. You should handle the progress bar. Here are some tutorials, which are useful for this purpose ..

  1. How to handle screen orientation change when progress dialog and background thread active?
  2. Handling progress dialogs and orientation changes
  3. Threads and Progress Dialogs in Android Screen Orientation Rotations
  4. Android Persistent ProgressDialog Done Right

************** Edited 2 *****************

And some time you looses data when orientation changes. So you can try these tutorials

  1. Handling Runtime Changes
  2. Faster Screen Orientation Change
  3. Handling Orientation Change in Android
like image 30
Pankaj Kumar Avatar answered Nov 09 '22 14:11

Pankaj Kumar