1

I have and android application in which I am initializing a third party library in the onCreate() of the main activity. Now when there is any runtime error in initialization of the library, My application will crash and at that point I want to show an AlertDialogue to the user saying "some error message" in the dialogue box. The problem is that when the application crashes, my alert dialogue is showing up but it is dismissed along with the application. I want it to be persisted even when the application crashes. Below is the code of alert dialogue which I used.

AlertDialog.Builder builder = new AlertDialog.Builder(context);
                    builder.setMessage("Error!")
                           .setCancelable(true)
                           .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                               public void onClick(DialogInterface dialog, int id) {
                                    //do things
                               }
                           });
                    AlertDialog alert = builder.create();
                    alert.show();

Can anyone guide me in how to make that AlertDialogue independent of the activity, so that it doesn't require the app window. If not possible, then what other widgets, I can use or customize?

kaddy
  • 109
  • 11

3 Answers3

0

If you want your app to keep running, avoid crashing.

If you want special handling for issues in a library init, catch the relevant exceptions and keep the app running in a state that shows the dialog and then does something else, such as finish()ing the activity that had a problem in its init.

laalto
  • 150,114
  • 66
  • 286
  • 303
0

May be it's possible to achieve something like that.

There is a library that allows you to show activity when your app crashes. Basically it launches new activity in another process.

At the same time you can show activity as a dialog by using Theme.AppCompat.Dialog for your activity as a theme.

Perhaps combining these two approaches will get what you want.

P.S. Or by using old fashioned try-catch as some people suggest.

infernno
  • 41
  • 3
0

To avoid the 3rd party library from taking down your app, wrap its API calls in a try/catch block, e.g.:

try
{
   BuggyLibrary.init();   // example
}
catch (Exception e)
{
   AlertDialog.Builder builder = new AlertDialog.Builder(context);
   builder.setMessage("Error!")
          .setCancelable(true)
          .setPositiveButton("OK", new DialogInterface.OnClickListener() {
              public void onClick(DialogInterface dialog, int id) {
                    //do things
              }
   });
   AlertDialog alert = builder.create();
   alert.show();
}

You'll need to consider the other implications of this library not initializing ... if your app can't continue functioning without it you'll need to gracefully finish() your Activity when the dialog is dismissed.

CSmith
  • 13,318
  • 3
  • 39
  • 42