开发者

Handling Orientation changes with Context and AlertDialog/AsyncTask

开发者 https://www.devze.com 2023-01-09 21:35 出处:网络
I have an activity that loads an AlertDialog like this. progressDialog = new ProgressDialog( MyActivity.this );

I have an activity that loads an AlertDialog like this.

        progressDialog = new ProgressDialog( MyActivity.this );
        progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        progressDialog.setMessage("Doing Something");
        progressDialog.setCancelable(false);
        progressDialog.show();

I also have another class that extends AsyncTask

    private class MyBackgroundTask extends AsyncTask<Void, Void, Boolean> {

        @Override
        protected Boolean doInBackground(Void... params) {

            ...do stuff
        }

        @Override
        protected void onPostExecute(Boolean result) {

            progressDialog.dismiss();
        }

        @Override
        protected void onProgressUpd开发者_开发问答ate(Void... values) {

        }

    }

If the orientation of the screen changes I get an error about trying to attach to a view that does not exist. This is because the Activity has been destroyed and the context has gone away.

So I added this to onDestroy() and it fixed it.

    @Override
    public void onDestroy() {

        if(progressDialog!=null)
            if(progressDialog.isShowing())
                progressDialog.cancel();

        if(background!=null)
            background.cancel(true);
    }

But this means that I always have to keep a reference to the Dialog and AsyncTask

So I cant do this for example:

new MyAsyncTask().execute();

In case the orientation changes, equally I can't do this:

    new AlertDialog.Builder(this)
    .setIcon(android.R.drawable.ic_dialog_alert)
    .setTitle( "some title" )
    .setMessage("some message")
    .setPositiveButton(R.string.ok, null)
    .show();

This does not seem right to me and there must be a better way, I know you can use getApplicationContext() but in some situations this is not viable.

How do I manage this properly?


i have pretty much the same issue, i think the way to handle this is not so simple, you have two options:

  1. make the activity handle the configuration change itself so you wont have the activity killed, but then you will have to call the dialog's invalidate or requestLayout i think.
  2. Make your own Progress Dialog (inherit from AlertDialog or Dialog classes, and have an inner method that saves it's current state and use it if the Dialog is on in saveInstanceState. and restore it when needed.

Your only remaining problem is the async task. If you start an async task in an activity that dies, i have no idea what it meas for the task, basically to avoid it you either need a thread that starts in the Application onCreate and dies in it's onDestroy that does those things for you so it's Activity independent or you need to use a Service with sticky Broadcasts so even if your activity is not listening for the broadcast because of orientation change it will get that broadcast once it's back on and listening. This way you are no bound to hold a instance of your Activity that will be nulled or in an invalid state for your service to handle.

0

精彩评论

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