0

Scenario - I dont have my application running in the background. I have a receiver implemented for ACTION_USER_PRESENT. In this receiver i start a activity whose manifest settings are :

<activity android:name=".activity.MyActivityB"
    android:excludeFromRecents="true"
    android:noHistory="true"
    android:screenOrientation="portrait"/>

This is because there is banner in it for which i credit points to the user and i dont want the user to be able to launch it again and again for the same banner .

I finish the activity on pause because I want to close it as soon the user navigates away from it.

But using a Nexus device, when I press the recents button then the activity is shown in the recents and on again pressing it the activity is recreated .

but what should happen is When the activity was in the foreground and i press recents button , then the activity should finish because i had called this :

@Override
protected void onPause() {
System.out.println("MyActivityB onpause called ");
    super.onPause();
    bannerShown = false;
    if(!isFinishing())
        finish();
}

But the activity doesnot gets finished probably and recreated when i again press the recents button.

this is not in the samsung devices but only in the Nexus devices with Android 6 & 7 that have recent apps button (i.e the square one).

Sid
  • 14,176
  • 7
  • 40
  • 48
beginner
  • 383
  • 3
  • 5
  • 19
  • That's because the Activity is still active on Nexus devices (onPause() is not called). The installed Launcher on these devices is the matter. In other words you can't recognize if the user opens the recents menu. But when you switch your application from recents menu it will be called, because the activity is now in background. Also when the app will be closed by the button in the middle the activity goes in background. – Larcado Nov 24 '16 at 11:33
  • I checked the logs the on pause and onWindowFocusChanged both gets called when the user clicks the recents button but still the activity is not finishing even after having finish(); in it – beginner Nov 24 '16 at 11:38

1 Answers1

0

You can use savedInstanceState:

https://developer.android.com/training/basics/activity-lifecycle/recreating.html

It's used for restore a Activity after some delay and recreate Objects, but they must be Serializable. You can use it here for saving your activity state by boolean.

private static final String ACTIVITY_STATE = "activityState";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Check whether it's firstly opened or recreated
    if (savedInstanceState != null) {
        if(savedInstanceState.getBoolean(ACTIVITY_STATE)) {
            // recreated => force close
            finish();
        }
    } else {
        // first opend

    }
    ...
}

@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
    // Saves that it was open already
    savedInstanceState.putBoolean(ACTIVITY_STATE, true);
    super.onSaveInstanceState(savedInstanceState);
}
Larcado
  • 225
  • 2
  • 10
  • its not working . when i press the recents button again all the values whether in intent or in variable are saved automatically. And this method is called but not setting anything. still the bundle is null – beginner Nov 24 '16 at 12:43