简体   繁体   中英

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

An idea I'm trying to implement is: loop through elements of a list, displaying and waiting to hear back from different activities depending on a particular value an element has.

What I thought would work:

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);


}}

plus an override method onActivityResult later in the code.

What happens is application starts the activity but it also continues looping through elements without waiting for started activity to finish...

Is it possible to make it wait for started activity to finish before moving on?

In your case for loop will not wait for result from other activity. So, I have another logic to solve this problem.

Declare a iterator variable in the class level like int i = 0;

When you want to start another activity for result, get data from the list at the i index questionBank[i] and start activityForResult.

In the onActivityResult method increment i.

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

In this way, you can achieve your desired results.

There is a solution no need startforResult or changes in Child activity,: , you can use startActivity(intent) on Parent Activity. Using the activity lifecycle , to stop after call child Activiy and resume after child activity finish.

I think its an elegant solution.

You have a sequence of activities to start inside Parent Class Activty: ..... startActiviy(intent1) startActiviy(intent2) startActiviy(intent3).....

It can be a loop like in my case... While( ) { startActiviy(intent) }...

the solution is split the sequence in two:

  • the first startactivity(...) in the end of the method of Parent activity, so the parent will pause after that;

  • the other startactivities(...) inside onResume method of Parent activity in the end of onResume to make sure the Parent activity will stop due the lifecycle.

You need a class variable to flow control as in loop, defined before call the first startActiviy and increment it onResume method for each call of startActivity.

The last thing: you need to be able to define intents onResume method, some local variables of "method" should be in Class scope (defined on "method" and accessible in "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

it works fine for me! my code I created a method to call each 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
        
}
  

The Child activity doesnt even wonder who is its Parent, when it finishes the lifecycle returns to Parent so no communication from Child to Parent. All code goes on Parent Activity! I hope you enjoy the solution!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM