Riko
Riko

Reputation: 1

Rapid activity switching crashes app

I'm having an issue with my app that when I switch rapidly between activities I'm able to crash the app. I'm not sure as to what's causing the crash. Here is the code used to switch activities. This code is copied into each activity (Map, List, and Rally).

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.map:
            Intent intentMap = new Intent(this, Map.class);
            startActivity(intentMap);
            finish();
            return (true);
        case R.id.list:
            Intent intentList = new Intent(this, List.class);
            startActivity(intentList);
            finish();
            return (true);
        case R.id.rally:
            Intent intentRally = new Intent(this, Rally.class);
            startActivity(intentRally);
            finish();
            return (true);
    }
    return (super.onOptionsItemSelected(item));
}

The crash is immediate when it occurs and I've tried using the debugger to break on all exceptions but it exits without throwing any. I'm using android studio and I'm testing on an S6 Edge that is running in nougat.

How can I go about finding the reason for these crashes?

Edit:

So I've finally gotten an exception to break. It turns out I'm having an OutOfMemoryError. I've been playing around with the Android Monitor. Obviously, I assumed I might have a memory leak but when I dump the java heap and use the analyzer tasks window it shows that I have no leaks. Whenever I double click the Initiate GC button the apps allocated memory goes down to a normal amount. Again, the allocated memory stays at a normal level unless I switch between classes quickly. I'm at a complete loss on why anything is happening. I've already tried using the answers thus far and they seem to make no impact on the problem.

Upvotes: 0

Views: 68

Answers (1)

Ebin Joy
Ebin Joy

Reputation: 3189

please specify the current context of activity when creating new intent for example if you are currently on MainActivity then

Intent intentMap = new Intent(MainActivity.this, Map.class); startActivity(intentMap);

Upvotes: 1

Related Questions