简体   繁体   English

如何将Parcelable对象从子活动传递给父活动?

[英]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.

相关问题 如何使用parcelable在活动之间传递线程类对象 - how to pass a thread class object from activity to activity using parcelable 将Parcelable对象从Activity传递到FragmentActivity - Pass Parcelable object from an Activity to a FragmentActivity 将可分割对象从片段传递到活动 - Pass an Parcelable Object from an Fragment to an activity 将实现Parcelable的对象从一个活动传递到另一个活动 - Pass Object implementing Parcelable from one Activity to Another Activity 如何将值从父级活动传递到子级活动,以及如何在子级活动退出时刷新父级活动 - How to pass value from parent activity to child activity and on child activity exit, refresh the parent activity 在活动之间传递可分配的对象 - Pass parcelable object between activity 如何将数据作为意图从子活动传递到父活动 - How to pass data as intent from child activity to parent activity 使用putextra将可拆分对象从子活动传递到主要活动 - Passing a parcelable object from child activity to primary using putextra 如何将非parcelable对象从活动传递到另一个活动? - How to pass non parcelable objects to from activity to another activity? 使用 Parcelable 将对象从一个 android 活动传递到另一个 - Use Parcelable to pass an object from one android activity to another
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM