OnBackPressed () Best Practice / Performance

I usually override onBackPressed()as follows:

@Override
public void onBackPressed() {
    super.onBackPressed();
    Intent intent = new Intent(getApplicationContext(), HomeActivity.class);
    startActivity(intent);
    finish();
}

Only now I saw that when I press the "Back" button with this code , I see a white activity for 0.5 seconds during the transition.

Testing a bit I found that if I use this code, the problem will not be:

@Override
public void onBackPressed() {
    Intent intent = new Intent(getApplicationContext(), HomeActivity.class);
    startActivity(intent);
    finish();
    super.onBackPressed();
}

What is the difference between these two codes? If I use the second, is everything all right? Cause of a memory problem? thank

+4
source share
1 answer

super.onBackPressedjust causes the finish line. It is not necessary if you call, complete yourself. Just delete the line.

, , , , , , , . .

+2

Source: https://habr.com/ru/post/1668269/


All Articles