如何关闭任务中的所有活动而不清除整个后台堆?

时间:2017-08-30 19:27:47

标签: android android-activity task

假设我有活动:

首先-----------------开放

A - > B - > C - > D - > E - > ˚F

我希望在F打开时调用活动C,D,E,将A,B留在后堆中,这样当用户从F返回时他们会到达B,依此类推。

我已经看到大量清除后台堆栈或从后台堆栈中拉出一个活动并清除其余部分的示例,但每次我尝试关闭一个任务时,我最终会得到一个空的后台堆栈并且后退按钮掉落在应用程序之外。我希望有关此特定情况的更多信息,以及最佳实践是什么(C,D,E,F是具有API副作用的工作流程,一旦离开就不应该重新输入,但是从应用程序中掉出来在背压下也不是我想要的。)

3 个答案:

答案 0 :(得分:1)

一个选项是使用B中的startActivityForResult(),因此将打开C.然后使用FLAG_ACTIVITY_FORWARD_RESULT打开下一个活动,直至到达F.

在F中覆盖onBackPressed()以致电setResult(),以便用户返回B.如果您不再次致电startActivityForResult(),则用户将永远无法再次访问{ {1}} C到F。

您可以在我的网站上找到使用Activity的详细示例 answer to another SO post

答案 1 :(得分:0)

The best workaround I could find was flagging the final task with FLAG_ACTIVITY_TASK_ON_HOME which just happens to be the task that was in the backstack at the point of departure, in my case. This isn't exactly an answer to the question, but if you are trying to clear a volatile workflow from the backstack and you don't want the user to "fall out" of your app on backpress subsequently, this is a viable option. See thread on the topic here: Android - Clearing Navigation Backstack

Edit: This code below creates separate application instances which I just noticed; not sure if there is a way to prevent that.

The solution I came up with was using taskAffinity, which you set in the manifest xml:

   while(nb < file.size()){
        QByteArray partial = file.read(65536);

        [...]

        qDebug() << clientConnection->state();
        qint64 nbb = clientConnection->write(partial, partial.count());
        nb += nbb;
    }

Starting the app the main affinity presumably collects any activities that are started in a task without a declared affinity, but when I want to start a new affinity for my workflow I add new task to the intent: In MainActivity, or anywhere I call the workflow:

wn.synset(args)

In intermediate workflow steps we do nothing special:

ps_list = []
for word1 in s1:
    best = max(word1.path_similarity(word2) for word2 in s2) #path_similarity is a method of each synset
    ps_list.append(best)

Finally, we call finish on the Affinity from our LAST workflow activity, after firing an intent for the post-workflow activity:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="co.example.app">
  <activity
  android:name=".activities.SplashActivity"
  android:launchMode="singleTop"
  android:taskAffinity="@string/affinity_main">
  </activity>
  <activity
  android:name=".activities.MainActivity"
  android:launchMode="singleTop"
  android:taskAffinity="@string/affinity_main">
  </activity>
  <activity
  android:name=".activities.WorkflowStepOneActivity"
  android:launchMode="singleTop"
  android:taskAffinity="@string/affinity_main">
  </activity>
  <activity
  android:name=".activities.WorkflowStepTwoActivity"
  android:launchMode="singleTop"
  android:taskAffinity="@string/affinity_workflow">
  </activity>
  <activity
  android:name=".activities.WorkflowStepThreeActivity"
  android:launchMode="singleTop"
  android:taskAffinity="@string/affinity_workflow">
  </activity>
  <activity
  android:name=".activities.LeaveWorkflowActivity"
  android:launchMode="singleTop"
  android:taskAffinity="@string/affinity_main">
  </activity>
</manifest>

This means our stack will look like this:

public void startWorkflow() {
  Intent intent = new Intent(MainActivity.this, 
  WorkflowStepOneActivity.class);
  intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  startActivity(intent);
}

答案 2 :(得分:0)

  1. Suppose you have following A->B->C->D->E activities and when you backpressed from activity E, you does'nt want to see activities C & D.
  2. Then you've to pass intent A to B by using this,

.module('fileSaverExample', ['ngFileSaver'])
.controller('ExampleCtrl', ['FileSaver', 'Blob', ExampleCtrl]);

  1. Then you've to pass intent B to C by using this,

var zip = new JSZip();
zip.file("Hello.txt", "Hello World\n");

// when everything has been downloaded, we can trigger the dl
zip.generateAsync({type:"blob"}).then(function (blob) { // 1) generate the zip file
FileSaver.saveAs(blob, "downloadables.zip");                          // 2) trigger the download
}, function (err) {
    console.log('err: '+ err);
});

  1. Then you've to pass intent C to D by using this,

=COUNTIF(RC[1]:RC[31], "x")

  1. Then you've to pass intent D to E by using this,

Intent i=new Intent(A.this,B.class);
startActivity(i);

  1. At final you get Activity E and after that when you backpressed from E ,you able to go at Activity B and after that A. Thankyou Happy Coding