【发布时间】: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