简体   繁体   English

如何将结果从Child活动返回到android中的Parent?

[英]How to get back the result from Child activity to Parent in android?

I'am starting a child activity on click of a button from Parent. 我点击Parent的按钮开始儿童活动。 And i'am calculating some result (of type string) in child activity and finishing the child to come back to Parent. 我在儿童活动中计算一些结果(字符串类型)并完成孩子回到父母。 Is there any better way to get that result in Parent using intents or extras? 有没有更好的方法来使用Intent或extras在Parent中获得该结果? (I can get that result in Parent by making the result variable as public & static in Child) Please help me. (我可以通过在Child中将结果变量设置为public&static来获得Parent中的结果)请帮帮我。 I'am a newbie to android development. 我是android开发的新手。

startActivityForResult(new Intent(ParentActivity.this, ChildActivity.class), ACTIVITY_CONSTANT);

What should i write in onActivityResult() of Parent? 我应该在Parent的onActivityResult()中写什么?

Instead of startActivityForResult(new Intent(ParentActivity.this, ChildActivity.class), ACTIVITY_CONSTANT); 而不是startActivityForResult(new Intent(ParentActivity.this, ChildActivity.class), ACTIVITY_CONSTANT);

You can use putExtras() method to pass values between activities: 您可以使用putExtras()方法在活动之间传递值:

In Child Activity: 在儿童活动中:

Intent data = new Intent();
data.putExtra("myData1", "Data 1 value");
data.putExtra("myData2", "Data 2 value");
// Activity finished ok, return the data
setResult(RESULT_OK, data);
finish();

And in Parent activity, you can override onActivityResult() and inside that you can have Intent paramater and from the Intent parameter of this method you can retrieve the extra values passed from the child activity, such as intent.getStringExtra or intent.getSerializableExtra. 在Parent活动中,您可以覆盖onActivityResult(),并且可以使用Intent参数,并且可以从此方法的Intent参数中检索从子活动传递的额外值,例如intent.getStringExtra或intent.getSerializableExtra。

for example: 例如:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == RESULT_OK && requestCode == REQUEST_CODE) {
        if (data.hasExtra("myData1")) {
            Toast.makeText(this, data.getExtras().getString("myData1"),
                Toast.LENGTH_SHORT).show();
        }
    }
}

In Parent Activity 在父活动中

Intent intent = new Intent(getApplicationContext(), yourChildActivity.class);
intent.putExtra("key", "value");
startActivityForResult(intent, ACTIVITY_CONSTANT);

in child activity to sent back result of your parent activity through 在子活动中发送您父项活动的结果

Intent data = new Intent();
data.putExtra("key1", "value1");
data.putExtra("key2", "value2");
// Activity finished return ok, return the data
setResult(RESULT_OK, data);
finish();

and get child activity result information in your parent activity 并在父活动中获取子活动结果信息

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == RESULT_OK && requestCode == REQUEST_CODE) {
        if (data.hasExtra("key1") && data.hasExtra("key2")) {
            Toast.makeText(
                this,
                "Your reult is :  "data.getExtras().getString("key1") + " " + data.getExtras().getString("key2"),
                Toast.LENGTH_SHORT).show();
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将结果返回到父活动 - How to get result back to the parent activity 从子活动android返回到父活动 - Move back to parent activity from child activity android Android:如何在启动活动中从服务中获取结果 - Android : how to get result back from service in the launching activity 如何从ActivityGroup中的子活动返回到父活动? - How to return back to parent activity from child activity in ActivityGroup? 从子级活动返回到父级活动会清除父级的搜索结果 - going back to parent activity from child activity clears the search result of parent Android:当我按系统后退菜单时,从子活动返回后,父母活动数据丢失 - Android: parent activity data is lost after back from child activity when I press system back menu 如何在父级活动类(Android)中获得子级活动视图? - How to get child Activity View in Parent Activity Class(Android)? 将结果发送回家长活动,而无需完成孩子活动 - Send result back to parent activity wihout finishing child activity 如何在父选项卡活动中获得子活动的结果? - How to get result of child activities in a parent tab activity? 如何从其子活动中获取父活动的价值并将其保留在android中? - how to get value to parent activity from its child activity and also retain them in android?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM