简体   繁体   English

如何将值从父级活动传递到子级活动,以及如何在子级活动退出时刷新父级活动

[英]How to pass value from parent activity to child activity and on child activity exit, refresh the parent activity

I have 3 activity. 我有3个活动。

  1. Main window 5 buttons 主窗口5个按钮
  2. From main window on button press this window open (calling it as parent) 从按钮的主窗口中,按下此窗口即可打开(将其称为父窗口)
  3. On parent window button press this window open calling it as the end child window. 在父窗口按钮上,按此窗口可打开,将其称为最终子窗口。

Now from child window I get value from parent window as below: 现在从子窗口中,我从父窗口中获得了价值,如下所示:

    // Set - from Window1
    Intent MyRotationsAddPicture1 = new Intent(getBaseContext(), MyRotationsAddPicture.class);
    MyRotationsAddPicture1.putExtra("Title", "1");
    MyRotationsAddPicture1.putExtra("Content", "2");               
    startActivity(MyRotationsAddPicture1);

    // Get - from Window2
    Log.d(TAG, getIntent().getExtras().getString("Title"));
    // Workout and exit this Window2 > to go back Window1 and show the latest update on window1
    this.finish(); 
    System.exit(0);

But now, after working out on child window, I exit this and go back to my previous parent window. 但是现在,在子窗口上工作之后,我退出此窗口并返回到上一个父窗口。 Once I go there, how can I refresh my imageView which have been modified and need to show the latest image in parent window? 转到那里后,如何刷新已修改并需要在父窗口中显示最新图像的imageView?

In WIndow1: 在Windows1中:

Start your child activity as 开始您的孩子活动

Intent MyRotationsAddPicture1 = new Intent(getBaseContext(), MyRotationsAddPicture.class);
    MyRotationsAddPicture1.putExtra("Title", "1");
    MyRotationsAddPicture1.putExtra("Content", "2");               
    startActivityForResult(MyRotationsAddPicture1, 0);

Override: 覆盖:

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

}

In window 2: 在窗口2中:

// Get - from Window2
    Log.d(TAG, getIntent().getExtras().getString("Title"));
    // Workout and exit this Window2 > to go back Window1 and show the latest update on window1
setResult(0);
    this.finish(); 

protected void onActivityResult (int requestCode, int resultCode, Intent data) 受保护的void onActivityResult(int requestCode,int resultCode,Intent数据)

Added in API level 1 Called when an activity you launched exits, giving you the requestCode you started it with, the resultCode it returned, and any additional data from it. 在API级别1中添加。当您启动的活动退出时,会向您提供该密码,并向您提供启动它的requestCode,返回的resultCode以及其中的任何其他数据。 The resultCode will be RESULT_CANCELED if the activity explicitly returned that, didn't return any result, or crashed during its operation. 如果活动明确返回该结果,未返回任何结果或在其操作期间崩溃,则resultCode将为RESULT_CANCELED。

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

相关问题 刷新孩子的家长活动? - Refreshing parent activity from child? 如何从子活动中更快地加载父活动? - How to load faster Parent Activity from Child Activity? 如何在不编写任何代码的情况下使用从父活动到每个子活动的单一视图? - How to use a single view from the parent activity to each child activity without writing any code in child activity? 如何在不完成/停止孩子活动的情况下从孩子活动中完成父母活动? - How to finish parent activity from child activity without finishing/stopping child activity? 将自定义对象的 ArrayList 从子活动传回父活动 - Pass ArrayList of custom objects from child activity back to parent Android:仅在父活动中的子活动上调用函数 - Android: call a function only on child activity from parent activity 将数组中的字符串从父活动传递给子活动 - Passing string in array from parent activity to child activity 将数据从子活动传递到父活动 - Pass data from sub activity to parent activity 如何从地图活动中获取所选位置并将其传递给父活动? - How to get selected location from map activity and pass it to a parent activity? startActivityForResult 2如何从1个父Activity激活并在只有1个孩子被分裂时获得onActivityResult - How startActivityForResult 2 Activity from 1 parent Activity and get onActivityResult when only 1 child is finisched
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM