[英]How to pass Parcelable object from child to parent activity?
I'm building a small app that requiere authentication. 我正在构建一个需要验证身份的小应用程序。 In my main activity I have a
Parcelable
class named "user" that contains the username and password of an user, when a user click on a button it starts a new activity passing that user class. 在我的主要活动中,我有一个名为“user”的
Parcelable
类,其中包含用户的用户名和密码,当用户单击按钮时,它会启动传递该用户类的新活动。 It works like a charm, in the child activity the user fill the form to authenticate, then when user press the back button, I would like to send the "user" class back to my main activity. 它就像一个魅力,在用户填充表单进行身份验证的子活动中,然后当用户按下后退按钮时,我想将“用户”类发送回我的主要活动。
Is it possible to do that ?? 有可能吗?
Start your child activity with: 开始您的孩子活动:
startActivityForResult(startIntent, 1);
In your child activity, intercept the back button and append your data: 在您的子活动中,拦截后退按钮并附加您的数据:
@Override
public void onBackPressed() {
Intent data = new Intent();
data.putExtra("key", yourDataHere);
setResult(Activity.RESULT_OK, data);
super.onBackPressed();
}
And get data inside parent activity inside onActivityResult
并在
onActivityResult
获取父活动内的数据
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == 1 && resultCode == Activity.RESULT_OK){
DataType yourData = (DataType) data.getParcelableExtra("key");
//Do whatever you want with yourData
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.