3

I have a background Service which starts an activity,

Intent i = new Intent(this, MyActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);

After destroying this Activity and restart it over the "long press home key menu", this Activity starts again. But I want to start the main activity instead. How could I realise this?

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Stefan
  • 53
  • 1
  • 4

2 Answers2

4

Could you explain in more detail? If I understand your problem try setting the FLAG_ACTIVITY_NO_HISTORY.

Alternatively a manual solution would be to check the FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY on the intent in MyActivity and launch to the main activity if you see this flag set. The following code should do that:

if ((getIntent().getFlags() & FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY) > 0) {
   activity.startActivity(new Intent(context , MainActivity.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));    
}
Nic Strong
  • 6,532
  • 4
  • 35
  • 50
  • I start the activity form the service to show a notification (I can't use the build in notifications). If the user launches the app again, he shouldn't see the notification activity. He should see the main activity. It works fine if the app is restartet from the app menu or if the main activity wasn't killed by pressing the back button. If the app is restartet from the "long press home key menu" (is there any better name?) and the main activity was previously killed by pressing the back button, then the notification Activity is startet again. noHistory won't work :-(. Thank you! – Stefan Feb 24 '11 at 08:46
  • I understand now. There is probably a better solution but see my edit for a solution. – Nic Strong Feb 24 '11 at 09:01
  • It seems to work, but I don't think this is the best way ;-) . It would be great to see a better solution. But thank you for this workaround! – Stefan Feb 24 '11 at 09:18
0

The problem is like you started the activity from service-->Notification came-->user launch the app agian-->No notification-->main activity came on foreground Now if the application is started from the "long press home key menu" main activity is starting and showing the Notification.

so one Clear resolution is make Main activity as "Exclude From recent = true" and "No History = true;" user will not be able to see your activity in the "long press home key menu"

Dinesh Prajapati
  • 9,274
  • 5
  • 30
  • 47
  • This is a workaround. But it would be nicer to include it in history and start the right activity. Thank you! – Stefan Feb 24 '11 at 09:07
  • This is the parameters which is provided by the AndroidManifest and we are using it to make the application work in our way which is right thing only :) – Dinesh Prajapati Feb 24 '11 at 09:12
  • Sure, but not very comfortable for the user ;-). But thank you for showing me this possible solution! – Stefan Feb 24 '11 at 09:24
  • Use the preference to decide which activity needs to launch when your activity has been launched. – Dinesh Prajapati Feb 24 '11 at 12:04