Android – how do I exit (only) the current activity if uncaughtexceptionhandler occurs?

I did some coding to test the specific use case of thread. Uncaughtexceptionhandler (described below)

First, I implemented baseactivity

public class BaseActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
            @Override
            public void uncaughtException(Thread paramThread, Throwable paramThrowable) {
                Log.e("CRASH_REPORT", "Activity crashed!");
                System.exit(0);
            }
        });
    }
}

By extending baseactivity, any uncapped exceptions will eventually be caught by the handler. System. Exit (0) will terminate the VM belonging to the application

Now I use this hierarchy to create 2 activities (both of which extend baseactivity)

In parentactivity, I have only one button, which will start the subactivity (code omitted)

In subactivity. Oncreate (...), I deliberately inject an exception to trigger uncaughtexception (...)

public class subactivity extends BaseActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        int crash = 1 / 0;
    }
}

When the subactivity is executed, it will trigger uncaughtexception (...) and stop the application (obviously there is no well-known app stop dialog box)

What I want to know is whether the triggered activity (in this case, a subactivity) can be terminated, and whether the app will roll back (sort) to the previous state (parentactivity)?

Any suggestions are appreciated. Thank you

resolvent:

After some research, I believe there is no way to return to the previous activity, because the main thread has stopped, so uncaughtexception (...) is triggered

Here, I will list my views on the problem of "countermeasures"

1. Declare each activity in a different process (not recommended)

In the manifest file, add Android: process = dedicated under each activity tag_ process_ Name. This will make each activity run in its own process, so as to ensure that the crash of one process will not affect another process. However, this is not recommended

<activity
    android:name=".ParentActivity"
    android:process="::parent_process" />

2. Forcibly stop the application (system. Exit (code)) and provide a callback, where each activity can define its own processing

Create baseactivity using the oncrashed (...) callback

public class BaseActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
            @Override
            public void uncaughtException(Thread paramThread, Throwable paramThrowable) {
                Log.e("CRASH_REPORT", "Activity crashed!");
                onCrashed(thread, throwable);
                System.exit(0);
            }
        });
    }

    protected void onCrashed(Thread thread, Throwable throwable) {
        // space for rent
    }
}

All activities extended from baseactivity can decide what they want to do when they crash. An example is to schedule the application to start again

public class subactivity extends BaseActivity
    @Override
    protected void onCrashed(Thread thread, Throwable throwable) {
        Intent i = new Intent(this, ParentActivity.class);
        PendingIntent pi = PendingIntent.getActivity(this, 0, i, 0);
        AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        am.set(AlarmManager.RTC, System.currentTimeMillis() + 100, pi);
    }

Alternatively, we can choose to implement uncaughtexception (...) at the application level, and the application will decide what to do next (for example, restart the current activity)

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>