简体   繁体   中英

Calling a new StartActivityForResult from onActivityResult

I am trying to call a new Intent from within an "onActivityResult" function, but the results are not what I hoped for. It either loops endlessly, or quits early.

Within the "main" activity I create an array of bundles, then for each bundle I create an intent, wait for the current activity to return "done", then start a new one with the next bundle.

The problem is that the "main" activity restarts after every call to onActivityResult, which means it's onStart gets called again, and all my bundles are recreated in an endless loop. The only way to avoid this appears to be by adding "finish();" to the end of the onActivityResult function, but this stops the entire process after only one call to onActivityResult.

Here's the code (abridged):

public class mainActivity extends Activity {

   ArrayList<Bundle> bundles;
   int taskId = 0;
   // A few other things here; nothing important to this question.

    public void onCreate(savedInstanceState)) {
        super.onCreate(savedInstanceState);
        bundles = new ArrayList<Bundle>();
    }

    public void onStart() {
        // Here I perform a loop that creates a number of bundles
        // and adds them to the "bundles" array (not shown).

        // Start the first activity:
        Intent firstIntent = new Intent(this, StuffDoer.class);
        firstIntent.putExtras(bundles.get(0));
        startActivityForResult(firstIntent, taskId);
        bundles.remove(0);
    }

    public void onActivityResult(int requestCode, int result, Intent data) {
        super.onActivityResult(requestCode, result, data);
        if (result == RESULT_OK) {
            if (bundles.size() > 0) {
                taskId += 1;
                Intent intent = new Intent(this, StuffDoer.class);
                intent.putExtras(bundles.get(0));
                startActivityForResult(intent, taskId);
                bundles.remove(0);
            } else {
                Log.v(TAG, "No more to do, finishing");
                finish();
            }
        } else {
            Log.v(TAG, "Did not get the expected return code");
        }
        // finish(); // If I uncomment this, it only performs this function 
                     // once before quitting. Commented out, it loops forever 
                     // (runs the onStart, adds more bundles, etc.).
    }
}

What is the correct way to do this?

It's not clear to me what you're trying to do, but if you only need to create your Bundles when the activity is first started, do it in onCreate(). Usually, the callbacks you implement for an activity are onCreate, onPause, and onResume. In an activity's normal life, it's in a lifecycle loop between onResume and onPause.

However, I'm curious. Why do you need to return to the main activity each time? It sounds as if your main activity "controls" the other activities. Usually, a better model for an Android app is to have each activity work independently, and switch to another activity when necessary. This is essentially a "program" with no "main", except for a "first among equals" activity that's started when the user clicks the app icon in the Launcher.

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