简体   繁体   English

从onActivityResult调用新的StartActivityForResult

[英]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. 我正在尝试从“ onActivityResult”函数中调用新的Intent,但是结果不是我希望的。 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. 问题是在每次调用onActivityResult之后,“ main”活动都会重新启动,这意味着它的onStart被再次调用,并且我的所有捆绑软件都在无休止的循环中重新创建。 The only way to avoid this appears to be by adding "finish();" 避免这种情况的唯一方法似乎是添加“ finish();”。 to the end of the onActivityResult function, but this stops the entire process after only one call to onActivityResult. 到onActivityResult函数的末尾,但这只会在一次调用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(). 我不清楚您要做什么,但是如果只需要在首次启动活动时创建Bundles,请在onCreate()中进行。 Usually, the callbacks you implement for an activity are onCreate, onPause, and onResume. 通常,为活动实现的回调为onCreate,onPause和onResume。 In an activity's normal life, it's in a lifecycle loop between onResume and onPause. 在活动的正常生活中,它处于onResume和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. 通常,Android应用程序的更好模型是让每个活动独立运行,并在必要时切换到另一个活动。 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. 本质上,这是一个没有“主程序”的“程序”,只是当用户单击启动器中的应用程序图标时启动的“相等中的第一个”活动。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android:onActivityResult()未调用startActivityForResult - Android: onActivityResult() is not calling for startActivityForResult Android:startActivityForResult没有调用onActivityResult - Android: startActivityForResult not calling onActivityResult 没有来自startActivityForResult的回调(onActivityResult) - No callback (onActivityResult) from startActivityForResult 侦听器内部的startactivityforresult()未调用onActivityResult() - startactivityforresult() inside a listener not calling onActivityResult() 调用startActivityForResult永远不会传递给onActivityResult - Calling startActivityForResult never pass to onActivityResult 返回startActivityForResult而不调用onActivityResult - startActivityForResult returning without calling onActivityResult Android:startActivityForResult停止调用onActivityResult - Android: startActivityForResult stopped calling onActivityResult 将 ID 从 startActivityForResult 传递给 onActivityResult - Passing an ID from startActivityForResult to onActivityResult Android-在onActivityResult中调用startActivityForResult有多安全? - Android - how safe is calling startActivityForResult within onActivityResult? 我正在调用 startActivityForResult 并发现未调用 onActivityResult - I'm calling startActivityForResult and finding that onActivityResult is not called
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM