是否可以让循环暂停等待其中的 activity 完成?

Is it possible to make a loop pause waiting for activity inside it to finish?

我正在尝试实现的一个想法是:遍历列表的元素,根据元素具有的特定值显示并等待来自不同活动的反馈。

我认为可行的方法:

for (Model item:questionBank) {


if (item.getTaskType() == "taskType1") {
    Intent intent = new Intent(MainActivity.this, 2ndactivity.class);

    intent.putExtra("word", item.getWord());
    startActivityForResult(intent,REQUEST_CODE);


}}

在代码后面加上一个覆盖方法 onActivityResult。

发生的是应用程序启动 activity 但它还在继续遍历元素而不等待启动 activity 完成...

是否可以让它等待开始 activity 完成后再继续?

在你的例子中,for 循环不会等待来自其他 activity 的结果。所以,我有另一种逻辑来解决这个问题。

int i = 0;

一样在class级别声明一个迭代器变量

当您想为结果启动另一个 activity 时,从 i index questionBank[i] 处的列表中获取数据并启动 activityForResult。

在onActivityResult方法中自增i.

i++
String word = questionBank[i];
startActivityForResult(intent,REQUEST_CODE);

这样就可以达到你想要的效果了。

有一个解决方案不需要 startforResult 或改变 Child activity!! , 你可以在 Parent Activity 上使用 startActivity(intent): 使用 activity 生命周期, 在调用子 Activiy 后停止并在子 activity 完成后恢复。

我认为这是一个优雅的解决方案。

您有一系列活动要在父 Class 活动中开始: ..... 开始活动(意图1) 开始活动(意图2) 开始活动(意图3) .....

它可以像我的情况一样是一个循环 ... 尽管( ) { 开始活动(意图) } ...

解决方案是将序列一分为二:

  • 第一次开始activity(...)结束 Parent activity 的方法,所以 parent 会在那之后暂停;

  • Parent 的 onResume 方法中的其他 startactivities(...) activity 在 onResume 结束时确保 Parent activity 将停止 生命周期。

您需要一个 class 变量来控制循环中的流量,在调用第一个 startActiviy 之前定义 并在每次调用 startActivity.

时增加 onResume 方法

最后一件事:您需要能够定义 onResume 方法的意图,“method”的一些局部变量应该在 Class 范围内(在“method”上定义并可在“onResume”中访问)。

Class Parent Activity {
  int r = 0; // control flow of child activities
  // variables to define intent inside onResume
 
   // the first call of startactivity goes here
   private void method ( )){
   .....
   // just an example  decreasing for each activity call,          
   r = 3; // call 4 times startActivity I guess, in this example
   startActivity (intent) // in the to acitvity stop 
  }

  // all subsequent startactivity goes here
  protected void onResume(){
    super.onResume();
    ....
    if (r > 0) {
        r--;
        startActivity(intent);
    }
  }//end onResum
}//Class end

对我来说效果很好! 我的代码我创建了一个方法来调用每个 Child activity "callinsertn"

public class TableActivity extends AppCompatActivity implements MainFragment.OnFragmentInteractionListener{
private Iterator< Pair<Integer, Integer> > iter; // control flow 
private String[] startup_tcolnames;    //need to define my intents
private Set<Pair<Integer, Integer>> selection;//need to define my intents

  protected void onCreate ( ) {
      ....
      ....
       mUpdateButton.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
            //     Toast.makeText(getApplicationContext(), "chamou callback na tabela col " + mrowid, Toast.LENGTH_LONG).show();

                ....
                ....
                iter = selection.iterator();
                if (iter.hasNext()) {
                    Pair<Integer, Integer> pair = iter.next();
                    callInsertn(pair);
                }

          }
       }
  }

  protected void onResume(){
    super.onResume();
    MainFragment.class.getSimpleName();

    if(iter !=null) {
        if (iter.hasNext()) {
            Pair<Integer, Integer> cell_coord = iter.next();
            callInsertn(cell_coord);
        } else {
        //its over. all child activities  ran
        selection.clear();
        selectedcell = false;
        Log.i("DEBUG", "final final do update button");
        iter =null;

        }


       
    }
  }

  //start my Child activity 
  //no flow control variables only accessing class vars to define intent                           
  private void callInsertn(Pair<Integer, Integer> cell_coord){

    
        mfrag_ind = cell_coord.second;
        mrowid = (long) cell_coord.first;
    
        Intent i = new Intent(TableActivity.this, Insertn.class);
        i.putExtra("operation", "update");
        //  i.putExtra 
        //  i.putExtra 
        

        startActivity (i);//start the Child activity
        
}
  

Child activity 甚至不知道谁是它的 Parent ,当它完成生命周期 returns 到 Parent 所以没有从 Child 到 Parent 的通信。所有代码都在 Parent Activity 上! 希望您喜欢这个解决方案!