开发者

FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS excludes whole application, not only the Activity

开发者 https://www.devze.com 2023-04-13 09:13 出处:网络
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 No

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


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 ...


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>
0

精彩评论

暂无评论...
验证码 换一张
取 消