Using dialog from background crashes app

how to handle app crash in android
android crash log analysis
android app restarts automatically after a crash
application crash reasons
android studio app crashes on startup
app crash android
android crash reporting
how to crash android app manually

I have a bunch of AsyncTasks which fetch data. They update the progress as they are executing by displaying dialog boxes. Sometimes this causes the app to crash when the app is in the background (not visible). My code called in below. What checks can I do to keep the app from crashing without re-factoring the dialog box entirely?

These are declared in DownloadAsyncTask and passed in on constructor

protected ProgressDialog mDialog;
protected String mDialogMessage = "Loading Data";
protected ProgressBar progBar;

doInBackground() {
    if ( mProgressBar != null && mProgressBar.getVisibility() == View.INVISIBLE) {
        mUIThreadHandler.post(new Runnable() {
            @Override
            public void run() {
                mProgressBar.setVisibility(View.VISIBLE);
            }
        });
    } else if ( mDialog != null && !mDialog.isShowing() ) {
        mUIThreadHandler.post(new Runnable() {
            @Override
            public void run() {
                mDialog.setMessage(mDialogMessage);
                mDialog.show();
            }
        });
    }
}

and then onPostExecute():

@Override
protected void onPostExecute(DefaultHandler handler) {
    super.onPostExecute(handler);
    if ( progBar != null && progBar.getVisibility() == View.VISIBLE ) {
        progBar.setVisibility(View.INVISIBLE);
    } else if ( mDialog != null && mDialog.isShowing() ) {
        try {
            mDialog.dismiss();
        } catch (Exception e) {
        }   
    }   
}

UPDATED CRASH LOG:

12-20 15:19:15.802: E/AndroidRuntime(9529): android.view.WindowManager$BadTokenException: Unable to add window -- token android.os.BinderProxy@41a37908 is not valid; is your activity running? 12-20 15:19:15.802: E/AndroidRuntime(9529): at android.view.ViewRootImpl.setView(ViewRootImpl.java:567) 12-20 15:19:15.802: E/AndroidRuntime(9529): at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:246) 12-20 15:19:15.802: E/AndroidRuntime(9529): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69) 12-20 15:19:15.802: E/AndroidRuntime(9529): at android.app.Dialog.show(Dialog.java:281) 12-20 15:19:15.802: E/AndroidRuntime(9529): at com.*.DownloadAsyncTask$2.run(DownloadAsyncTask.java:119) 12-20 15:19:15.802: E/AndroidRuntime(9529): at android.os.Handler.handleCallback(Handler.java:725) 12-20 15:19:15.802: E/AndroidRuntime(9529): at android.os.Handler.dispatchMessage(Handler.java:92) 12-20 15:19:15.802: E/AndroidRuntime(9529): at android.os.Looper.loop(Looper.java:137) 12-20 15:19:15.802: E/AndroidRuntime(9529): at android.app.ActivityThread.main(ActivityThread.java:5039) 12-20 15:19:15.802: E/AndroidRuntime(9529): at java.lang.reflect.Method.invokeNative(Native Method) 12-20 15:19:15.802: E/AndroidRuntime(9529): at java.lang.reflect.Method.invoke(Method.java:511) 12-20 15:19:15.802: E/AndroidRuntime(9529): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run

You are touching the UI from the background thread. Android technically says "do not update", but it is best to read that as "do not interact with the UI on background threads". Your if statement essentially says "if (...) is true about the UI, post this task to be executed on the UI thread at some time in the future". By the time your Runnable executes on the UI thread, the UI's state may have changed. It is probably better to encapsulate the entire logic and ship that to UI thread to be executed. You could do that by combining the two Runnables you have, and shipping in a single post, but AsyncTask was designed so you would not need to deal with handlers directly. If you call publishProgress() in doInBackGround it will post a call on the UI thread to the onProgressUpdate() member. Therefore, the easier thing to do is combine the code in your Runnables and move to the onProgressUpdate() method.

Something like --

   void onProgressUpdate() {
            if (mProgressBar != null && mProgressBar.getVisibility() == View.INVISIBLE) {
                mProgressBar.setVisibility(View.VISIBLE);
            } else if (mDialog != null && !mDialog.isShowing()) {
                mDialog.setMessage(mDialogMessage);
                mDialog.show();
            }
        }

Android App crash with dialog, You are trying to run a UI thread in a background thread (inside your AsyncTask), what you can do is create a global dialog in your AsyncTask class and show it� An app that is written using native-code languages crashes if there’s an unhandled signal, such as SIGSEGV, during its execution. When an app crashes, Android terminates the app's process and displays a dialog to let the user know that the app has stopped, as shown in figure 1. Figure 1. An app crash on an Android device

Use runOnUiThread(runnable) to avoid crash: this essentially runs the code segment in the run method on the UI thread. wrap all the UI operations you would want to perform in the run method as show below.

so the code would go something like:

yourActivity.runOnUiThread(new Runnable() {

            @Override
            public void run() {
                // whatever UI operations..
                ..
                mDialog.setMessage(mDialogMessage);
                mDialog.show();
            }

});

Hide your crashes gracefully (and still report them), Since our app is meant to be used by golfers, any crash that happens during confronts them with that hideous system crash dialog; something that in a background component, as a service, or in the Application class. Remember that UI elements may only be accessed from the UI thread. Use the UI thread to access UI elements before launching the background work and/or use CoreDispatcher.RunAsync or CoreDispatcher.RunIdleAsync on the background thread. An example of work that can be performed on a background thread is the calculating of computer AI in a game.

you should check that activity is not finished.

use this:

if (!isFinishing()) {
    mDialog.show();
    }

check this page for more description:

http://dimitar.me/android-displaying-dialogs-from-background-threads/

Detect and diagnose crashes, Android will display the ANR dialog for a particular application when it work on a background thread and communicating back with the UI. This is pretty simple: if you’re using an app, and it suddenly closes for no reason, the app crashed. If you’re using an app and it becomes unresponsive, but you can still access other apps, the app has crashed. If you’re trying to launch an app and it keeps disappearing, the app is repeatedly crashing.

Keeping your app responsive, Android library that allows launching a custom activity when your app crashes, instead of showing the hated "Unfortunately, X has stopped" dialog. launch the default system error when the app is in background. CaocConfig. at android.app.Dialog.show(Dialog.java:239) at android.app.Activity.showDialog(Activity.java:2488) … at android.os.Handler.dispatchMessage(Handler.java:99) … I only got a couple of these exceptions from thousands of installs, so I knew that was not anything that happens regularly or that it was easy to replicate.

Ereza/CustomActivityOnCrash: Android library that allows , Tricks when suppressing the system crash dialog. user when the app is in the background, so let's suppress it only for background crashes. While neither of the articles were using WPF, extensive testing showed that using ApplicationContexts and Application.Run instead of ShowDialog() made WPF (and WinForms) work far better. The NotifyIcon issue took a number of days to resolve as I battled with unresponsiveness when clicking on the ContextMenu to Exit.

Suppress the Android Crash Dialog – Nightly Nexus, [macOS only] InDesign crashes when you try to quit the application in Transparent background in PNG files renders into black when used in InDesign. file with missing and modified links, the text in the dialog that appears� I am using Holo.Theme.Dialog.Light (tried Holo.Theme.Dialog too) to display my activity as a dialog. This works OK on Android 4.0+, but crashes on earlier versions. The crash is the following: java

Comments
  • Please post a stack trace showing the exception and its source, so that we can better advise you of what you are doing wrong.
  • It does not happen very often. I did save the log file somewhere ...
  • Line 119 is: @Override public void run() { mDialog.setMessage(mDialogMessage); mDialog.show(); }
  • Line 119 is inside doInBackground()
  • Ok. This looks like a better approach.
  • can you connect that approach to an AsyncTask which is producing updates of status?
  • yes, we can! AsyncTask basically is a task that is carried in a bg thread. So inside onPostExecute, we can use this approach i thought!