开发者

Prevent onCreateDialog being called when process is killed then relaunched

I have an Activity with ProgressDialog being updated by an AsyncTask. I am using onCreateDialog to setup the dialog. The AsyncTask is writing to the SDCard. During normal scenarious (rotation, going to background, etc.) I have no issues.

The problem is that the dialog gets recreated if the process gets killed. Thus, I end up with a "newly" opened activity and a dialog that is not supposed to be shown at all because there is no AsyncTask set up to update it.

For example in case when SD card gets ejecter then the Reaper comes and kills the process (no onDestroy, noPause, noResume has been called by the framework). When, however, the application is resumed (for example from the recently used applications) there is no clue that there is no AsyncTask and I am forced to show the dialog. I cannot return null in onCreateDialog, because the app will crash.

How can I prevent a dialog to be recreated after the process is killed?

Example:

- Activity gets shown
    - onCreateDialog/onPrepareDialog show a progress dialog
      - AsyncTask gets started exporting to SD card
=> SD card gets unmounted
- Process is killed
- User selects the application from task switched
- Activity gets created as new
=> Android calls onCreateDialog/onPrepareDialog with the previously shown dialog ID

By the time the activity is recreated as new there is no AsyncTask, there is even no SD card. Still, Android insist that I show a dialog.

H开发者_运维技巧ow can I prevent onCreate/PrepareDialog methods to be called during the recreate? Or the only choice is to bring up an error dialog instead.


you can make check over SD card if it is inserted or ejected thru

String state = Environment.getExternalStorageState();

and over it's return value you can control your dialog...


OnCreateDialog method is like onCreate method which will be called only once in your activity. You cannot control your dialog using this method. Instead your activity should also have the following piece of code which will be called each time Dialog is shown. So you have to handle things inside this method instead.

    @Override
protected void onPrepareDialog(int id, Dialog dialog) {
    super.onPrepareDialog(id, dialog);
      }
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜