【问题标题】:Is it possible to return values through onPause?是否可以通过 onPause 返回值?
【发布时间】:2020-08-31 20:26:59
【问题描述】:

我正在 Android 中进行试验,看看如何将值返回给主 Activity(来自子 Activity,Activity2),我想询问适当的方法。

在我的主要活动中,我有一个函数,它在字符串中显示请求代码和结果代码以进行概念验证:

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        String a2return = "A2 reqcode: "+requestCode + "A2 result: "+resultCode;
        TextView textView = findViewById(R.id.A2result);
        textView.setText(a2return);

    }

在我的子活动 Activity2 中,我有以下函数,它们是唯一调用 finish() 函数返回主活动的函数:


    // "Send text back" button click
    public void onButtonClick(View view) {

        Intent resultIntent = new Intent();
        resultIntent.putExtra("rv1", value);
        resultIntent.putExtra("rv2",value+99);
        setResult(Activity2.RESULT_OK, resultIntent);
        finish();
    }



    @Override
    public void onPause() {
        super.onPause();
        String status = "Im in PAUSE, wait a bit";
        // Capture the layout's TextView and set the string as its text
        TextView textView = findViewById(R.id.a2_status);
        textView.setText(status);

        Intent resultIntent = new Intent();
        resultIntent.putExtra("rv1", value);
        resultIntent.putExtra("rv2",value+99);
        setResult(Activity2.RESULT_OK, resultIntent);
        finish();

    }

通过“OnButtonClick”返回主活动按预期正常工作。在 Main Activity 中,我看到适当的代码表明 Activity2 已完成。但是,onPause 没有。 onPause 函数在用户按下返回按钮返回主 Activity 时调用,这是 Android Manifest 中指示的标准返回按钮。

        <activity android:name=".Activity2" android:parentActivityName=".MainActivity">
                <!-- The meta-data tag is required if you support API level 15 and lower -->
                <meta-data
                    android:name="android.support.PARENT_ACTIVITY"
                    android:value=".MainActivity" />
        </activity>


按照我在这里编写代码的方式,它应该以与 OnButtonClick 相同的方式完成。 onPause 是否不意味着在激活时返回任何内容?

【问题讨论】:

    标签: android android-activity onactivityresult onpause


    【解决方案1】:

    为此,您应该覆盖 onBackPressed() 而不是 onPause()

    @Override
    public void onBackPressed() {
        Intent resultIntent = new Intent();
        resultIntent.putExtra("rv1", value);
        resultIntent.putExtra("rv2",value+99);
        setResult(Activity2.RESULT_OK, resultIntent);
    
        super.onBackPressed();
    }
    

    对于操作栏中的向上导航按钮,使用如下内容:

        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            if (item.getItemId() == android.R.id.home) {
                onBackPressed();
                return true;
            }
            return (super.onOptionsItemSelected(item));
        }
    

    【讨论】:

    • 感谢您的回复。这就是我一直在寻找的方法。我开始使用 Android,这很有趣。这可能是一个愚蠢的后续行动,但是是否有任何“on~”方法很常见并且应该在所有程序中“考虑”?像这样的 onBackPressed...我从来没有想过那种场景。
    • 在生命周期方法(onCreate()onStart 等)之外,在 Activity 级别上我想不出任何其他方法。但是你使用哪些回调真的取决于你在做什么。
    • 再次感谢您的回复。我尝试了您的解决方案,它有效,但它适用于硬件后退按钮。您使用什么功能来处理操作栏上显示的后退按钮?这就是我要说的:developer.android.com/training/basics/firstapp/…
    猜你喜欢
    • 1970-01-01
    • 2015-10-05
    • 2018-04-04
    • 2018-08-28
    • 1970-01-01
    • 2014-12-19
    • 2019-05-06
    • 1970-01-01
    • 2014-10-31
    相关资源
    最近更新 更多