开发者

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

开发者 https://www.devze.com 2023-04-12 12:20 出处:网络
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 th

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 i开发者_JAVA百科s spinning?Any help would be appreciated.


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);         
        }
    }


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


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);
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号