簡體   English   中英

是否可以讓循環暫停以等待其中的活動完成?

[英]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 。

發生的情況是應用程序啟動了活動,但它也繼續循環遍歷元素而不等待啟動的活動完成......

是否可以讓它等待開始的活動完成后再繼續?

在您的情況下,for 循環不會等待其他活動的結果。 所以,我有另一個邏輯來解決這個問題。

在 class 級別中聲明一個迭代器變量,如int i = 0;

當您想為結果啟動另一個活動時,從第i index questionBank[i]的列表中獲取數據並啟動 activityForResult。

在 onActivityResult 方法中增加 i。

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

這樣,您就可以達到您想要的結果。

有一個解決方案不需要 startforResult 或 Child 活動的更改,: ,您可以在 Parent Activity 上使用 startActivity(intent)。 使用活動生命周期,在調用子活動后停止並在子活動完成后恢復。

我認為這是一個優雅的解決方案。

您有一系列活動要在 Parent Class Activty 中開始:..... startActiviy(intent1) startActiviy(intent2) startActiviy(intent3).....

在我的情況下,它可能是一個循環... While( ) { startActiviy(intent) } ...

解決方案將序列分成兩部分:

  • 第一個 startactivity(...) 在 Parent 活動方法的末尾,因此父級將在此之后暫停;

  • 在 onResume 結束時 Parent 活動的 onResume 方法中的其他 startactivities(...) 以確保 Parent 活動將在生命周期中停止。

您需要一個 class 變量來在循環中進行流控制,在調用第一個 startActiviy 之前定義,並為每次調用 startActivity 遞增它的 onResume 方法。

最后一件事:您需要能夠定義意圖 onResume 方法,“方法”的一些局部變量應該在 Class scope 中(在“方法”上定義並且在“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

這對我來說可以! 我的代碼我創建了一個方法來調用每個子活動“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 活動甚至不知道誰是它的 Parent,當它完成生命周期時返回給 Parent,因此沒有從 Child 到 Parent 的通信。 所有代碼都在父活動中進行! 我希望你喜歡這個解決方案!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM