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.
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 :
- Programatically enabling/disabling screen rotations in Android
- 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 ..
- How to handle screen orientation change when progress dialog and background thread active?
- Handling progress dialogs and orientation changes
- Threads and Progress Dialogs in Android Screen Orientation Rotations
- Android Persistent ProgressDialog Done Right
************** Edited 2 *****************
And some time you looses data when orientation changes. So you can try these tutorials
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);
}
}
© 2022 - 2024 — McMap. All rights reserved.