[英]how to return requestCode from child activity to parent activity in default back button click
I am having using onActivityResult method in my ParentActivity
and i call a ChildActivity
from my ParentActivity
in a Button
click. 我在ParentActivity
中使用onActivityResult方法,并在Button
单击中从ParentActivity
调用ChildActivity
。 In my ChildActivity
when I click the default back button and when it goes to my ParentActivity
, I am not getting my requestCode that i set in my ChildActivity
in onStop()
method by: 在我的ChildActivity
当我单击默认后退按钮时,当它转到我的ParentActivity
,我没有得到我在onStop()
方法中设置在ChildActivity
中的requestCode :
setResult(2); 的setResult(2);
How can I return my requestCode
from my ChildActivity
to ParentActivity
when I click back button. 当我单击后退按钮时,如何将我的requestCode
从我的ChildActivity
返回到ParentActivity
。
Please help me. 请帮我。
Thanks in advance. 提前致谢。
Here is ma code: 这是ma代码:
//Parent activity
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Toast.makeText(this,resultCode+"", Toast.LENGTH_LONG).show();
if(resultCode==2){
finish();
}
}
//Child activity
protected void onStop() {
setResult(2);
super.onStop();
}
protected void onPause() {
setResult(2);
super.onStop();
}
You can use this code in your child activity 您可以在子活动中使用此代码
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_BACK){
setResult(RESULT_OK);
finish(); // If you have no further use for this activity or there is no dependency on this activity
return true;
}
return super.onKeyDown(keyCode, event);
}
This piece of code will return result ok code from your child activity to parent activity 这段代码将从您的子活动返回结果ok代码到父活动
Now in your parent activity in 现在在您的父活动中
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case 2:
if(resultCode == -1){
// Here you write your code which you have to write on result receive
}
break;
default:
break;
}
super.onActivityResult(requestCode, resultCode, data);
}
Let me know if this helps you 如果这有助于您,请告诉我
覆盖onBackPressed()比覆盖onKeyDown()更好
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.