简体   繁体   中英

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

我有一个注册屏幕,用户输入所有注册详细信息,当用户点击“注册”按钮时,我显示进度条(进度条开始旋转),然后将用户带到主屏幕。如果用户旋转当进度条旋转时,进度条被取消。我希望方向不会从potrait变为横向,反之亦然,当进度条可见时(意味着它正在旋转)。进度条如何停止方向改变旋转?任何帮助将不胜感激。

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM