简体   繁体   English

Android Intent B是否不会返回Intent A的下一行?

[英]Android Intent B does not return to next line of Intent A?

So my main Activity within the onOptionsItemSelected block has a settings option that when clicked, launches a settings activity and passes some values to it. 因此,我的onOptionsItemSelected块中的主要Activity具有一个settings选项,当单击该选项时,它会启动一个settings活动并将一些值传递给它。 The code looks like this 代码看起来像这样

@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        Intent intent = new Intent(this, SettingsActivity.class);
        intent.putExtra("SettingsObj", settings);
        intent.putExtra("UpdateDate", employees[0].updated);
        startActivityForResult(intent, 1);
        System.out.println("Did we return to the same instance????????????????");
        AdjustSettings();
    }

    return super.onOptionsItemSelected(item);
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == 1) {
        if (resultCode == RESULT_OK) {
            System.out.println("Did we get to this POINT????????");
            settings = (Settings) data.getExtras().getSerializable("result");
            System.out.println("What is the size of text?  " + settings.TextSize);
        }
    }
}

Now the settings activity is called successfully and I pass back a Settings object afterwards. 现在,设置活动已成功调用,之后我又传递了一个设置对象。 The Settings activity code passing back the value looks like this 传回值的“设置”活动代码如下所示

@Override
public void onPause() {
    returnIntent.putExtra("result", settings);
    setResult(RESULT_OK, returnIntent);
    finish();

    super.onPause();
}

Now when i back out of the settings page, my main Activity does not resume at the next line printing out the question "Did we return?". 现在,当我退出设置页面时,我的主“活动”不会在下一行继续显示“我们返回了吗?”问题。 The only print out code is what is at the beginning of the class. 唯一的打印输出代码是课程开始时的内容。 Does hitting the return arrow on my phone inside intent 2 not take me to the next line in intent 1 or is it a new instance of intent 1? 击中意图2内电话上的返回箭头是否不会将我带到意图1的下一行,或者它是意图1的新实例? Also, wasn't sure if i should return my intent data in the onPause or onStop :P Which is better? 另外,不确定我是否应该在onPause或onStop中返回我的意图数据:P哪个更好? Thanks! 谢谢!

Normally, your code "Did we return..." and AdjustSettings() will be executed just after the start of your second activity (because startActivityForResult isn't a blocking call, it's just a request for the system to start the activity x). 通常,您的代码“ Did we return ...”和AdjustSettings()将在第二个活动开始后立即执行(因为startActivityForResult不是阻塞调用,它只是系统启动活动x的请求) 。 So your first activity will not wait the end of the second to execute these lines. 因此,您的第一个活动将不会等待第二个活动的结尾执行这些行。

About the result of your second activity, check out this thread : setResult does not work when BACK button pressed . 关于第二个活动的结果,请查看此线程: 按BACK按钮时setResult不起作用 Calling setResult on the onPause() method seems to be too late (your result will be always RESULT_CANCELED), you have to do it in the onBackPressed() method. 在onPause()方法上调用setResult似乎为时已晚(您的结果将始终为RESULT_CANCELED),您必须在onBackPressed()方法中进行此操作。 However, I'm not sure your application workflow is correct, the back button isn't designed to save settings. 但是,我不确定您的应用程序工作流程是否正确,后退按钮并非用于保存设置。 You should take a look at the preferences system : http://developer.android.com/guide/topics/ui/settings.html 您应该看看首选项系统: http : //developer.android.com/guide/topics/ui/settings.html

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

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