1
Intent goMain = new Intent(getBaseContext(),MainActivity.class);

goMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
goMain.putExtras(extras);
getApplicationContext().startActivity(goMain);

What I'm trying to do is, sending data from a separate IntentService o one and only MainActivity. What happens and which i would not prefer it to happen is bringing a new activity instead of the current running one. And additionally, the current running activity does not show the data sent from the intentservice. When I try removing FLAG_ACTIVITY_NEW_TASK I get "Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag" error. Can anyone help me with this? Thanks in advance.

David Wasser
  • 93,459
  • 16
  • 209
  • 274
gokturk
  • 116
  • 2
  • 13

1 Answers1

2

Using Intent.FLAG_ACTIVITY_CLEAR_TASK is causing your problem. If you want to use the existing instance of the Activity, use this set of flags:

goMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
            Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);

Setting Intent.FLAG_ACTIVITY_CLEAR_TOP will remove any other activities from the task stack that are on top of MainActivity.

Setting Intent.FLAG_ACTIVITY_SINGLE_TOP will ensure that the existing instance of MainActivity will be used and a new one won't be created.

MainActivity.onNewIntent() will be called with the Intent you use.

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