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

3

14

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.

Akin answered 13/10, 2011 at 5:4 Comment(2)
I think for this activity you should define portrait mode in your manifest file - android:screenOrientation=[""portrait"], so only for this activity your screen orientation doesn't change. also specifies <activity android:name=".Activity_name" android:configChanges="orientation|keyboardHidden">Unapproachable
I cant fix the orientation because it should support both landscape and potrait.I want it to be stopped only when progress bar is visible.Akin
F
14

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);         
        }
    }
Frogfish answered 13/10, 2011 at 5:11 Comment(9)
If i have 2 diff layout for potrait and landscape mode,how can i set the appropriate layout when orientation changes.Because when we make the activity as android:configChanges="orientation|keyboardHidden",it won't automatically change the layout on orientation change.Akin
you can put that landscape xml file in res/layout-land folder and then it will automatically set that file when orientation changes, no need to write any code for that to detect the orientation in your activity file..Ries
In normal scenario it will automatically set the file when orientation changes,but when we make it as android:configChanges="orientation|keyboardHidden", it doent change automatically.Akin
Yep,But i cannot fix a particular orientation.It should support both potrait and landscape.Akin
@Akin Please come to casual chat room so that we can discuss further on your issue...I am trying something else for your issue.Frogfish
@Lalit:It wont work because when progress dialog is spinning and wen u rotate,u are once again setting the content view which will result in resetting the view which means all the data entered in edit text would be gone and progress bar will be dismissed.Akin
let us continue this discussion in chatAkin
@LalitPoptani: i also trid this way and it works well,but as we are define setcontent view ,it doesn't maintain data ahich are generated runtime.Belshazzar
It does the job but in my application, it also stops the other images (events) from working :(Saltern
A
9

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
Aeronautics answered 13/10, 2011 at 5:53 Comment(0)
M
2

use setRequestOrientation method in Activity class.

Inherit ProgressDialog class, and override methods.
Example code is below.

    class myDialog extends ProgressDialog
{
    Activity mActivity;
    public myDialog(Activity context) {
        super(context);
        this.mActivity = context;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mActivity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);
    }

    @Override
    public void dismiss() {            
        super.dismiss();
        mActivity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
    }
}
Metathesis answered 13/10, 2011 at 6:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.