Home > Net >  Why does isFinishing() behaves differently in entry point Activity (with android.intent.action.MAIN)
Why does isFinishing() behaves differently in entry point Activity (with android.intent.action.MAIN)

Time:08-01

I have 2 types of Activity -

  1. One acts as the entry point of the Application, with android.intent.action.MAIN.
  2. Another activity is launched by the entry point activity.

They looks as the following

// Act as entry point of the application, with android.intent.action.MAIN
public class MainActivity extends AppCompatActivity {

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

        Log.i(TAG, "MainActivity isFinishing() : "   this.isFinishing());
    }
}

// Launched by MainActivity.
public class MainActivity2 extends AppCompatActivity {

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

        Log.i(TAG, "MainActivity2 isFinishing() : "   this.isFinishing());
    }
}

I notice that this.isFinishing() behaves differently in both Activity, when I tested via

  1. Back button press
  2. Home button press

Here's the observation

MainActivity (Entry point Activity)

  1. When back button pressed, isFinishing() returns false.
  2. When home button pressed, isFinishing() returns false.

MainActivity2 (Launched by MainActivity)

  1. When back button pressed, isFinishing() returns true.
  2. When home button pressed, isFinishing() returns false.

I want to differentiate between back button press, and home button press by using isFinishing() method. Now, I realize I can't do that in entry point Activity, because both scenarios returns the same false.

May I know, why does isFinishing() behaves differently in entry point Activity (with android.intent.action.MAIN) and non entry point Activity?

Thanks.

CodePudding user response:

This is expected on all Android 12 and higher devices as per the Behavior changes for all apps in Android 12:

Android 12 changes the default handling of the system Back press on launcher activities that are at the root of their tasks. In previous versions, the system would finish these activities on Back press. In Android 12, the system now moves the activity and its task to the background instead of finishing the activity. The new behavior matches the current behavior when navigating out of an app using the Home button or gesture.

Therefore there is no way to tell the difference between the system back button and the home button on the root activity - they purposefully both put the activity in the exact same state in the exact same way. You shouldn't be trying to distinguish between the two as they are not meant to be treated any differently (i.e., you shouldn't clear any state or make any changes for one that you wouldn't also do for the other).

This only applies to the root activity of your task stack, which is why your other activities still show as isFinishing() when the back button is pressed.

  • Related