简体   繁体   中英

Back button from activity 2 closes down the app instead of going to activity 1 in Android 7

Pressing back button from the second activity returned to the first activity before without problems. I then updated to Android 7.
Then the whole app closed when pressing back button from the second activity.

I know that there are threads about this here and I have checked them all. Basically, they say that finish() should be avoided from the first activity.
I don't call finish() , so that is the problem here. It is difficult to solve, because it works like it should when I launch the app from Android studio.
It returns to the first activity from second. The problem occurs when the app is started by pressing its icon (not from Android studio).
Pressing back from the second activity closes down the whole app. How can I solve this? Here is some of my code:

Activity 1:

Intent glIntent = new Intent("astral.worldstriall.GLActivity");
glIntent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);

The below code should be used in 2nd activity so that when u press back button it terminates the current activity(2nd activity) and goes back to previous activity

@Override
public void onBackPressed() {
    super.onBackPressed();
    finish();
}

I think you just misused the Intent constructor. According to the documentation, you used this constructor Intent(String action) . The one that you atually want should be this one Intent(Context packageContext, Class<?> cls) .

In the first activity (therefore this being the instance of your first activity), you should write:

Intent glIntent = new Intent(this, astral.worldstriall.GLActivity.class);
glIntent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(glIntent);

For going first to second activity...

  Intent i=new Intent(FirstActivity.this,SecondActivity.class);
            startActivity(i);

Use below code for going in previous activity...

  @Override
public void onBackPressed() {
    super.onBackPressed();

    Intent i=new Intent(SecondActivity.this, FirstActivity.class);
    startActivity(i);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM