6

I have a Notification which starts an Activity. After a long press on home button and selecting my app, I want to start my main Activity again, and not this Activity started by the Notification. I tried with FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS, but this removed my whole application from the recents, and that's not what I want to achieve. How can I have my app in the recents, but have the main Activity started?

Regards

Binabik
  • 1,013
  • 11
  • 24

2 Answers2

14

Okay, I found the solution to my problem. I started an Activity from a Notification with FLAG_ACTIVITY_NEW_TASK. But it seems to me that this Activity only gets started in an own task if affinity is different from the default affinity. So I had to add a different affinity in the manifest.

And it seems that FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS does not (as documented) exlucde the Activity from the recents, rather it excludes the whole task (not the whole application) in which the Activity gets started from the recents. And as I hadn't set a different affinity the Activity which I wanted to exclude was started in the same task (although I had set FLAG_ACTIVITY_NEW_TASK) and so my whole application (as it was running in only one task) was excluded from the recents.

Now I've set a different affinity for the Activity that gets started from the Notification and I start it with FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS. When I leave this Activity and long-press the HOME button I can choose my app and the default task is started or brought to the front.

If it's wrong what I mentioned above, feel free to clear it up ...

Binabik
  • 1,013
  • 11
  • 24
  • Thanks, this is similar to a problem I'm encountering - how did you change the affinity of the task? – Ed_ Sep 05 '13 at 12:56
  • 3
    For anyone who finds this in future, just answered my own question: in the manifest set `android:taskAffinity="com.yourpackage.YourActivity"` – Ed_ Sep 05 '13 at 13:03
  • will the activity get destroyed on it's own if say the home button is pressed? because now the notification activity can never be navigated back to – Kushan Jun 02 '17 at 18:26
1

It's not clear to me what you want.

"How can I have my app in the recents, but have the main Activity started?"

If you want to always start one activity using the long home-press, you can define your activity as singleTask in the manifest.

That way, when you select the shortcut in the long press HOME, it will always show the MAIN, singleTask activity. I say this because I used this behavior before once. ;-)

I believe that by using this you can still start an activity from the notification normally, using, say, Intents.

In the activity tag:

        android:launchMode="singleTask"
        >

        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
davidcesarino
  • 16,160
  • 16
  • 68
  • 109
  • I think you understood what I want but launchMode="singleTask" didn't solve my problem. When I select the shortcut of my app after a long-home-press, it doesn't always show my MAIN, it shows the last Activity that I used. – Binabik Oct 14 '11 at 14:13
  • After a long press on home I want to get started Activity SplashScreen ... I removed launchMode. – Binabik Oct 15 '11 at 17:22
  • I mean your whole manifest. You can edit the question if you want... I say that because if you read this: http://developer.android.com/guide/topics/fundamentals/tasks-and-back-stack.html#TaskLaunchModes You'll see that singleTask always start a new task and is placed at the root of this task. However, if one instance of it already exists, then the intent goes to it (onNewIntent). Also, because ActivityManager calls the activity with FLAG_ACTIVITY_NEW_TASK|FLAG_ACTIVITY_RESET_IF_NEEDED (... continues ...) – davidcesarino Oct 16 '11 at 05:09
  • (see http://stackoverflow.com/questions/2417468/android-bug-in-launchmode-singletask-activity-stack-not-preserved), the top stack is cleared and the singleTask act. Shouldn't be a surprise since singleTask activities can only exist once. So in all it is placed into the foreground. However, there are cases (I don't understand fully) that intent-filters and Intents in code can change this behavior. It would be good to post the whole manifest and also how your activities are called (I mean, which Intent codes were used). – davidcesarino Oct 16 '11 at 05:12
  • ... or you can use the intent in the notification to start the activity in a new task (or define affinity for it in the manifest). There are other options, depending on what you want to achieve. And that's why I said I didn't understand your problem *completely*. But of course, I could be wrong... – davidcesarino Oct 16 '11 at 05:28