1

Here's the scenario:

  1. User uses Activity#1 to schedule an alarm (using the the android AlarmManager service), and goes back to home screen. Activity#1 remains in stack.

  2. AlarmReciever's onReceived() is called as planned:

    @Override
    public void onReceive(Context context, Intent intent){
    
       Intent intent = new Intent(context ,Activity2.class);
    
    intent.setFlags(
            Intent.FLAG_ACTIVITY_NEW_TASK |
            Intent.FLAG_ACTIVITY_NO_HISTORY
                    );
    
    context.startActivity(intent);
    }
    
  3. Activity#2 (Activity2) starts successfully.

    public class Activity2 extends Activity {
      @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    
        setContentView(R.layout.activity_layout_2);
    
        getWindow().addFlags(
                WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
                        WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD |
                        WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON |
                        WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON |
                        WindowManager.LayoutParams.FLAG_FULLSCREEN
        );
    
    }
    
    @Override
    protected void onResume() {
        super.onResume();
    
        findViewById(R.id.dismiss_button).setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        finish();
    
                    }
                }
        );
    
    }
    

    }

  4. User clicks a button (R.id.dismiss_button) to finish() Activity#2.

  5. System moves back to homescreen as expected, yet Activity#2 remains on stack (it appears when the user clicks on Recent Button)

Please notice that in addition to setting Intent.FLAG_ACTIVITY_NO_HISTORY flag for the intent, the flag was also specified in the AndroidManifest.xml (and I tried also each of them separably):

   <activity
        android:name=".Activity2"
        android:label="@string/app_name"
        android:configChanges="orientation|keyboardHidden"
        android:launchMode= "singleInstance"
        android:noHistory="true"
        >
    </activity>

Also, the flag Intent.FLAG_ACTIVITY_NEW_TASK is required as the Activity#2 being started from an AlarmReceiver rather than another activity.

1 Answers1

4

The NO_HISTORY flag just prevents this Activity from appearing in the task stack once the Activity is no longer visible. In your case, if you were to press HOME when Activity2 is shown, you would see that Android calls finish() and onDestroy() on that activity right away (which isn't the usual behaviour when a task is put into the background).

To do what you want, you need to add

    android:excludeFromRecents="true"

to the manifest entry for your Activity. This tells Android not to put your task in the list of recent tasks.

David Wasser
  • 93,459
  • 16
  • 209
  • 274