Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
355 views
in Technique[技术] by (71.8m points)

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

Mighty all, I'm new to android and look for help or suggestions here.

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 though elements without waiting for started activity to finish...

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

Thank you for sharing your thoughts in advance!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Suggested Solution:

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 increament i.

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

In this way, you can achieve your desired results. Let me know if this solve your problem.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...