简体   繁体   English

替代Intent.FLAG_ACTIVITY_CLEAR_TASK

[英]Alternative to Intent.FLAG_ACTIVITY_CLEAR_TASK

I have two apps App-B launches App-A. 我有两个应用程序App-B推出App-A。 If the user starts App B from inside App AI call finish on App-A so I have no problem. 如果用户从App-A内的App AI内部启动App B,那么我没有问题。

If the user goes straight to App B from the Application drawer or long pressing home button then I implement the below which clears the task in App A first before applying all the extras. 如果用户从应用程序抽屉直接进入应用程序B或长按主页按钮,则执行下面的操作,在应用所有附加功能之前先清除应用程序A中的任务。 This has the desired affect but only works on API 11. On lower APIs the new task in APP-A will not change and extras putExtra will have no effect. 这具有所需的效果,但仅适用于API 11.在较低的API上,APP-A中的新任务不会更改,并且额外的putExtra将不起作用。 Any alternative to FLAG_ACTIVITY_CLEAR_TASK ? FLAG_ACTIVITY_CLEAR_TASK替代方案? for API <=10? 对于API <= 10?

        Intent i = new Intent("com.App-A");
    i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);

Thanks 谢谢

Jason 贾森

The new IntentCompat should've helped on that, but apparently the flag is ignored for API lower than 11. 新的IntentCompat应该对此有所帮助,但显然对于低于11的API会忽略该标志。

To use IntentCompat do following: 要使用IntentCompat,请执行以下操作:

intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
IntentCompat.FLAG_ACTIVITY_CLEAR_TASK);

this will work correctly 这将正常工作

i.addFlag(Intent.FLAG_ACTIVITY_NO_HISTORY | 
               Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);

The best documentation I've found for these Intent flags is here: http://blog.akquinet.de/2010/04/15/android-activites-and-tasks-series-intent-flags/ 我在这里找到了这些Intent标志的最佳文档: http//blog.akquinet.de/2010/04/15/android-activites-and-tasks-series-intent-flags/

I don't understand what you are trying to do, but have you tried FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET ? 我不明白你想做什么,但你尝试过FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET吗?

I may be wrong in understanding what you are asking, but is it that when you launch B, you want A to be killed? 我理解你在问什么可能是错的,但是当你发布B时,你想要A被杀?

In A, add this to the activity tag in the manifest: 在A中,将其添加到清单中的activity标记:

android:noHistory=true

This will cause the activity to be removed from memory as soon as it loses focus. 这将导致活动失去焦点后立即从内存中删除。

I'm still having a lot of trouble understanding the problem but would like to help you fix it. 我仍然很难理解这个问题,但是想帮你修复它。 Since comments only allow 600 characters and don't format well, I'm going to create an answer instead, because I'm sure that together we can get this resolved. 由于注释只允许600个字符并且格式不正确,我将创建一个答案,因为我确信我们可以一起解决这个问题。

I would like to be able to reproduce your problem. 我希望能够重现你的问题。 To do that I've created 2 applications: AppA and AppB . 为此,我创建了2个应用程序: AppAAppB AppA has a single activity called ActivityA and AppB has a single activity called ActivityB . AppA有一个名为ActivityA ,AppB有一个名为ActivityB Both ActivityA and ActivityB use android:launchMode="singleTask" . ActivityAActivityB使用android:launchMode="singleTask"

ActivityA has a button on it that launches AppB and finishes, like this: ActivityA有一个按钮,可以启动AppB并完成,如下所示:

    Intent intent = new Intent("de.sharpmind.example.AppB");
    intent.putExtra("extra", "launched from AppA");
    startActivity(intent);
    finish();

ActivityB has a button on it that launches AppA like this: ActivityB有一个按钮,可以像这样启动AppA:

    Intent intent = new Intent("de.sharpmind.example.AppA");
    intent.putExtra("extra", "launched from AppB");
    startActivity(intent);

This all works as I expect it to. 这一切都像我期望的那样有效。 AppA and AppB run in different tasks. AppA和AppB运行在不同的任务中。 The "extra" is properly seen in the onCreate() methods of each application. 在每个应用程序的onCreate()方法中都可以正确地看到“额外”。

So, can you please tell me more about your problem. 那么,请你告诉我更多关于你的问题的信息。 How can I reproduce exactly your problem? 我怎样才能准确再现您的问题? You wrote: 你写了:

On lower APIs the new task in APP-A will not change and extras putExtra will have no effect. 在较低的API上,APP-A中的新任务不会更改,并且额外的putExtra将不起作用。

What do you mean by that? 你是什​​么意思? Where are you putting extras and where are you getting them and what do you expect to happen? 你在哪里放置额外的东西,你在哪里得到它们,你期望发生什么?

Also, what is the launchMode of your AppB ? 另外,你的launchModeAppB什么?

Also, when you have this problem, are there other activities in the task stack for AppA? 此外,当您遇到此问题时,AppA的任务堆栈中是否还有其他活动?

Please provide more info, either in your original question or here as comments. 请在原始问题或此处作为评论提供更多信息。

Using FLAG_ACTIVITY_CLEAR_TASK clears the back stack. 使用FLAG_ACTIVITY_CLEAR_TASK清除后台堆栈。 If I'm understanding correctly, this is the behavior you want. 如果我理解正确,这就是你想要的行为。

Using singleInstance instead of singleTask in your manifest will do this. 在清单中使用singleInstance而不是singleTask将执行此操作。

In the comments you said that it must be singleTask . 在评论中你说它必须是singleTask I'm assuming this is because you need the back stack in certain circumstances. 我假设这是因为在某些情况下你需要后筹码。

Since launchMode can't be changed programaticaly and FLAG_ACTIVITY_CLEAR_TASK is not availble for API <=10, you may have to create two identical activities. 由于launchMode不能programaticaly改变和FLAG_ACTIVITY_CLEAR_TASK不availble的用于API <= 10,则可能必须创建两个相同的活动。

One with launchMode=singleTask and one with launchMode=singleInstance . 一个是launchMode=singleTask ,另一个是launchMode=singleInstance

Add this to the one using singleInstance to get a clear stack when launched from the app drawer: 将此添加到使用singleInstance那个,以便在从应用程序抽屉启动时获得清除堆栈:

 <category android:name="android.intent.category.LAUNCHER" />

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Intent.FLAG_ACTIVITY_CLEAR_TOP 不起作用 - Intent.FLAG_ACTIVITY_CLEAR_TOP is not working SherlockActivity中的Intent.FLAG_ACTIVITY_CLEAR_TOP无法达到目的 - Intent.FLAG_ACTIVITY_CLEAR_TOP in SherlockActivity not serving the purpose 为什么同时使用Intent.FLAG_ACTIVITY_NEW_TASK和Intent.FLAG_ACTIVITY_SINGLE_TOP? - Why use both Intent.FLAG_ACTIVITY_NEW_TASK and Intent.FLAG_ACTIVITY_SINGLE_TOP? Android Intent.FLAG_ACTIVITY_CLEAR_TOP是否清除SharedPreferences? - Android Intent.FLAG_ACTIVITY_CLEAR_TOP clears SharedPreferences? 按意图发送信息并使用FLAG_ACTIVITY_CLEAR_TOP - Sending information IN Intent and using FLAG_ACTIVITY_CLEAR_TOP 你如何使用Intent.FLAG_ACTIVITY_CLEAR_TOP清除活动堆栈? - How do you use Intent.FLAG_ACTIVITY_CLEAR_TOP to clear the Activity Stack? 无法使用Intent。FLAG_ACTIVITY_NEW_TASK - Cannot Use Intent.FLAG_ACTIVITY_NEW_TASK 使用标志Intent.FLAG_ACTIVITY_NEW_TASK从onPostExecute()方法启动意图不起作用 - Starting intent from onPostExecute() method with a flag Intent.FLAG_ACTIVITY_NEW_TASK is not working Android Studio - 如何修复使用 flag_activity_clear_task 后后退按钮仍然有效 | flag_activity_new_task? - Android Studio - How to fix back button still working after using flag_activity_clear_task | flag_activity_new_task? 使用 Intent.createChooser 并出现错误:从 Activity 上下文外部调用 startActivity() 需要 FLAG_ACTIVITY_NEW_TASK 标志 - Using Intent.createChooser and getting error: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM