How to Handle Screen Orientation Change When Progress Dialog and Background Thread Active

How to handle screen orientation change when progress dialog and background thread active?

When you switch orientations, Android will create a new View. You're probably getting crashes because your background thread is trying to change the state on the old one. (It may also be having trouble because your background thread isn't on the UI thread)

I'd suggest making that mHandler volatile and updating it when the orientation changes.

Background task, progress dialog, orientation change - is there any 100% working solution?

Step #1: Make your AsyncTask a static nested class, or an entirely separate class, just not an inner (non-static nested) class.

Step #2: Have the AsyncTask hold onto the Activity via a data member, set via the constructor and a setter.

Step #3: When creating the AsyncTask, supply the current Activity to the constructor.

Step #4: In onRetainNonConfigurationInstance(), return the AsyncTask, after detaching it from the original, now-going-away activity.

Step #5: In onCreate(), if getLastNonConfigurationInstance() is not null, cast it to your AsyncTask class and call your setter to associate your new activity with the task.

Step #6: Do not refer to the activity data member from doInBackground().

If you follow the above recipe, it will all work. onProgressUpdate() and onPostExecute() are suspended between the start of onRetainNonConfigurationInstance() and the end of the subsequent onCreate().

Here is a sample project demonstrating the technique.

Another approach is to ditch the AsyncTask and move your work into an IntentService. This is particularly useful if the work to be done may be long and should go on regardless of what the user does in terms of activities (e.g., downloading a large file). You can use an ordered broadcast Intent to either have the activity respond to the work being done (if it is still in the foreground) or raise a Notification to let the user know if the work has been done. Here is a blog post with more on this pattern.

application crashes on orientation change while using progressDialog with threading

Check out this blog:

http://blog.doityourselfandroid.com/2010/11/14/handling-progress-dialogs-and-screen-orientation-changes/

There are also other similar questions here on stackoverflow:
How to handle screen orientation change when progress dialog and background thread active?

I have experienced, if using the first option, that the emulator has a bug which makes it call onCreate() twice - causing a crash, but it shouldn't be an issue on a real device.

How to prevent ProgressDialog to dismissing on screen rotation change in Android?

I end up to use DialogFragment and it works.

public class MainActivity extends Activity{



public void prepareViews(int ID, boolean state){
switch(ID){
case USERNAME_TEXTBOX:
LoginUsernameTextBox.setEnabled(state);
break;
case PASSWORD_TEXTBOX:
LoginPasswordTextBox.setEnabled(state);
break;
case LOGIN_BUTTON:
LoginButton.setEnabled(state);
break;
case LOGIN_PROGRESSBAR:
if(state == true){
LoginProgressBar.setVisibility(View.VISIBLE);
LoginProgressBar.setIndeterminate(true); }
else{
LoginProgressBar.setVisibility(View.GONE);
}
break;
case CONNECTING_DIALOG:
if(state == true){
showDialog();
}
break;
}
}

public void showDialog() {
FragmentManager fragmentManager = getFragmentManager();
ProgressDialogFragment newFragment = new ProgressDialogFragment();
newFragment.show(fragmentManager, "Dialog");
}

public static class ProgressDialogFragment extends DialogFragment {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final ProgressDialog progressDialog = ProgressDialog.show(getActivity(), "", "Connecting to "
+ DeviceName.subSequence(0, DeviceName.length() - 17));
return progressDialog;
}
}
}

Activity orientation change while in the background

Use Below Code in Manifest:

android:configChanges="keyboardHidden|orientation|screenSize" 
android:screenOrientation="portrait"

Edited:
Sorry there is no way to control the rotation animation. This is done way outside of your app, deep in the window manager where it takes a screenshot of the current screen, resizes and rebuilds the UI behind it, and then runs a built-in animation to transition from the original screenshot to the new rebuilt UI. There is no way to modify this behavior when the screen rotation changes

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

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


Related Topics



Leave a reply



Submit