John Moore
John Moore

Reputation: 332

How to get ACRA to stop app when exception is not in main activity?

I want my app to stop when ACRA detects and reports an uncaught exception - normal ACRA behavior. This is not working when the exception happens in an activity that is not the main activity.

After fighting this in my actual app, I created a very simple app that uses ACRA, and allows me to force an exception in either the main activity, or a second one that can be started from the first. I am testing with a MinSDK of 26. The app is in Java and has only enough code to generate this test.

An exception in the first activity produces the desired result (as determined from logcat and the screen):

  1. On button touch, main activity generates an exception
  2. ACRA catches the exception:
  3. ACRA starts another application process
  4. the first process ends
  5. the second process does ACRA processing
  6. the second process ends

However, running again, but with the exception in the second activity (with the first still on the task stack), results in:

  1. On button touch, main activity starts second activity
  2. On button touch, second activity generates an exception
  3. ACRA starts another application process
  4. First process ends
  5. ACRA processes in the second process (also using this code base)
  6. meanwhile Android starts yet a third process - this is the problem
  7. The second process finishes
  8. The third process displays the main activity screen, and waits

What seems to be happening is that Android is detecting that there is an activity left on the stack, and is starting a new process to bring that one forward.

But, ACRA should be stopping this.

Is this an ACRA problem? In any case, ideas on how to prevent it?

*NOTE:

  1. In my tests, I am using touch events on a bottom navigation bar to generate a divide by zero exception.
  2. The order of when ACRA finishes and the third process starts is indeterminate, as far as I can tell. It is also probably irrelevant.
  3. Test results are from watching the screen, and logcat. Important evens are logged using the Log class.

Below is a snippet from the main activity that shows the button processing, and the exception generation. The second activity is similar.

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        final Context context = this.getApplicationContext();
        setContentView(R.layout.activity_main);
        Log.i(MyApp.TAG, "MainActivity.onCreate() - pid:"+android.os.Process.myPid());
        mTextMessage = findViewById(R.id.message);
        BottomNavigationView navigation = findViewById(R.id.navigation);
        navigation.setOnNavigationItemSelectedListener((item) -> {
            switch (item.getItemId()) {
                case R.id.navigation_home:
                    int i = 0;
                    Log.i(MyApp.TAG, "Throwing exception in main activity");
                    Log.i(MyApp.TAG, "This shouldn't show up" + 3 / i);
                    return true;
                case R.id.navigation_dashboard:
                    Log.i(MyApp.TAG, "Starting Activity2");
                    startActivity(new Intent(context, Activity2.class));
                    return true;
                case R.id.navigation_notifications:
                    mTextMessage.setText(R.string.title_notifications);
                    return true;
            }
            return false;
        });

Upvotes: 1

Views: 136

Answers (1)

F43nd1r
F43nd1r

Reputation: 7749

This is a bug in ACRA, fixed by this PR, which will be included in the next release (5.3.0).

Upvotes: 1

Related Questions