Here's the scenario:
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.AlarmReciever
'sonReceived()
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); }
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(); } } ); }
}
User clicks a button (
R.id.dismiss_button
) tofinish()
Activity#2.- 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.