Application Crashes on orientation change while showing Progress Bar in AsyncTask

房东的猫 提交于 2019-12-06 17:45:30

Try this for your activity. Although it is not very clean. It can help

<activity android:configChanges="orientation|screenSize" android:name=".activity.MyActivity" ></activity>

Well what about "locking" the screen-orientation while your AsyncTask is running?!

For example:

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);

right before you get the JSON file from the server and

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);

after your AsynTask is executed.

My best guess from the limited information available as of now:

When you rotate your screen it goes through the normal activity lifecycle process of onPause(), onStop(), and onDestroy() as appropriate, so you have to dismiss the progressDialog in one of those methods

How i did it:

@Override
protected void onPause()
{
    super.onPause();
    if (mProgressDialog != null)
        mProgressDialog.dismiss();
}

Try this small hack I put together... run it and observe the logcat information as you try various combinations of screen changes and pressing the hardware back key. Then try the tweak mentioned in the comments below and tinker some more. This lets you avoid android:configChanges="orientation|screenSize" which isn't preferable.

This should build as is with a minimal manifest and layout.xml file. Leave comments if I mucked it up somehow.

// ********************************************************************
// *** AsynchHack - allows for multiple screen orientation changes, ***
// ***    multiple asynch tasks are started / stopped,progress      ***
// ***    dialogs handled gracefully (no memory leaks / etc).       ***
// ***                                                              ***
// ***    Remove the wrapping comments in the onCreate() function   ***
// ***    to restrict processing to a single asynch instantiation   ***
// ***    screen rotation will dismiss the progress meter, but not  ***
// ***    the task                                                  ***
// ***                                                              ***
// ***    View the logcat to understand the timing of events and    ***
// ***    their interactions                                        ***
// ***                                                              ***
// ********************************************************************

package com.example.AsynchHack;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import java.util.List;

// *** Main activity

public class AsynchHack extends Activity {

    final static String debugClass = "AsynchHack";
    ProgressDialog mProgressDialog;

    // *** standard onCreate()

    public void onCreate(Bundle aSavedInstanceState) {

        super.onCreate(aSavedInstanceState);
        Log.d(debugClass, "onCreate(" + this + ")");
        setContentView(R.layout.layout);

        // As-is, this allows for multiple starts of asynch tasks
        //if (aSavedInstanceState == null) {
        mProgressDialog = new ProgressDialog(this);
        (new AsynchStartup()).execute();
        //}

    }

    // *** demo Asynch task

    private class AsynchStartup extends AsyncTask<Void, Void, Void> {

        protected void onPreExecute() {
            Log.d(debugClass,
                  "--- AsynchStartup() onPreExecute  (" + this + ")");
            mProgressDialog.setMessage("Loading please wait..");
            mProgressDialog.show();
        }

        protected Void doInBackground(Void... aInput) {
            Log.d(debugClass,
                  "--- AsynchStartup() doInBackground(" +
                  this + ") *starts*");

            // simulate lengthy processing
            try {
                Thread.sleep(5000);
            }
            catch (InterruptedException aException) { }

            Log.d(debugClass,
                  "--- AsynchStartup() doInBackground(" +
                  this + ") *finishes*");
            return null;
        }

        protected void onProgressUpdate(Void aProgress){
        }

        protected void onPostExecute(Void aOutput) {
            Log.d(debugClass,
                  "--- AsynchStartup() onPostExecute (" + this + ")");

            if (mProgressDialog != null) {
                Log.d(debugClass,
                      "--- AsynchStartup() onPostExecute " +
                      "(Dismissing progress meter)");
                mProgressDialog.dismiss();
                mProgressDialog = null;
            }
            else {
                Log.d(debugClass,
                      "--- AsynchStartup() onPostExecute " +
                      "(Not required to dismiss progress meter)");
            }
        }

    }

    // *** standard onDestroy()

    public void onDestroy() {

        super.onDestroy();
        Log.d(debugClass, "onDestroy(" + this + ")");

        if (mProgressDialog != null) {
            Log.d(debugClass,
                 "onDestroy(Dismissing progress meter)");
            mProgressDialog.dismiss();
            mProgressDialog = null;
        }
        else {
            Log.d(debugClass,
                  "onDestroy(Not required to dismiss progress meter)");
        }

    }

}

Try using this

  dialog.setCancelable(false);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!