简体   繁体   English

onActivityResult 将数据从片段返回到活动

[英]onActivityResult return data from fragment to activity

I Have an activity A that opens an activity B using startActivityForResult .我有一个活动 A 使用startActivityForResult打开活动 B。

Now in activity B it's an activity fragment holder as well it contains an ActionBar with menu items.现在在活动 B 中,它是一个活动片段持有者,它还包含一个带有菜单项的ActionBar

Now whenever I press action bar button in activity B it should return data from selected fragment of an activity B not to its holder instead it should return data to activity A because it's the one who did the launch.现在,每当我按下活动 B 中的操作栏按钮时,它应该从活动 B 的选定片段返回数据,而不是将数据返回到其持有者,而是应该将数据返回到活动 A,因为它是启动的人。

So it's basically passing data fragment (inside activity B) to activity B then to Activity A.所以它基本上是将数据片段(在活动 B 内)传递给活动 B,然后再传递到活动 A。

I am trying hopelessly to find a way to solve it.我绝望地试图找到解决它的方法。 Is there any possible way to do it?有什么可能的方法吗?

Disclaimer there are many ways, this is the one I prefer, not the best ever and not the perfect one, I just like this.免责声明有很多方法,这是我更喜欢的一种,不是有史以来最好的,也不是完美的,我就是喜欢这个。

The easiest way, in my opinion, is to pass the data from Fragment inside B to ActivityB, then from ActivityB to ActivityA.在我看来,最简单的方法是将数据从 B 内部的 Fragment 传递到 ActivityB,然后从 ActivityB 传递到 ActivityA。

Step 1 to pass data from Fragment to container activity you have many ways;第 1 步将数据从 Fragment 传递到容器活动,您有多种方法; the one I usually use is to use an Interface:我通常使用的是使用接口:

Create interface for ActivityB为ActivityB创建接口

public interface IActivityB {
    void setDataAAndFinish(whateverType data);
}

Implement interface in your activityB在您的活动B中实现接口

public class InterventoActivity extends AppCompatActivity implements IInterventoActivity {

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    private Bundle dataA = null;
    @Override
    public void setDataAAndFinish(whateverType data) {
        dataA = data;
        Intent intent = new Intent();
        intent.putExtra("data", data)
        setResult(RESULT_OK, intent);        
        finish();
    }
}

Set activityA to request and accept return from ActivityB将activityA设置为请求并接受来自ActivityB的返回

first, start activityB for result and not normally首先,启动activityB以获得结果而不是正常

Intent i = new Intent(this, ActivityB.class);
startActivityForResult(i, 1);

Then read result然后读取结果

public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == 1) {
         if(resultCode == RESULT_OK) {
             whateverType data = data.getStringExtra("data");
         }     
    }
} 

Now from fragment现在从片段

((IActivityB)getActivity()).setDataAAndFinish(myDatas);

You need to declare a function in your ActivityB like the following.您需要在ActivityB声明一个函数,如下所示。

public void sendDataBackToActivityA(String dataToBePassedToActivityA) {
    Intent intent = new Intent();
    intent.putExtra("data", dataToBePassedToActivityA);
    setResult(RESULT_OK, intent);
    finish();
}

Now from the Fragment that you launched from ActivityB , just call the method on some action in your Fragment that was launched from ActivityB .现在从Fragment您从启动ActivityB ,只需拨打你的一些行动的方法Fragment是从启动ActivityB So the pseudo implementation of the process in your Fragment should look like the following.因此,您的Fragment流程的伪实现应如下所示。 Declare this function in your Fragment and invoke the function on some action in your Fragment .在声明此功能Fragment并调用你的一些动作的功能Fragment

public void sendDataToActivityAFromFragment(String dataToBePassed) {
    ((ActivityB)getActivity()).sendDataBackToActivityA(dataToBePassed);
}

This will serve your purpose I hope.我希望这将有助于您的目的。

You can declare static variables in your A activity and set it from the B activity您可以在 A 活动中声明静态变量并从 B 活动中设置它

Or you can make static class and set variables values from B Activity and get it in A activity或者您可以创建静态类并从 B 活动设置变量值并在 A 活动中获取它

Or you can send the menu value from Activity B to A while you exit Activity B and start activity A by using bundle或者,您可以在退出 Activity B 并使用 bundle 启动 Activity A 时将菜单值从 Activity B 发送到 A

this is how you set the data to be passed to activity A in activity B这就是您如何设置要传递给活动 B 中的活动 A 的数据

  val resultIntent = Intent()
        resultIntent.putExtra(DATA, "closed")
        setResult(Activity.RESULT_OK, resultIntent)
        finish()

this is how you get data in activity A这就是您在活动 A 中获取数据的方式

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
            super.onActivityResult(requestCode, resultCode, data)
            if (requestCode == FILE_UPLOAD_CODE) {
                when (resultCode) {
                    Activity.RESULT_OK -> {
                                      // data here is obtained data of the method paramater
                     }}}}

In your Activity A.Move from activity A to b like this:-在您的活动 A.Move 从活动 A 到 b 像这样:-

int YOUR_CODE=101;  // it can be whatever you like
Intent i = new Intent(getActivity(), B.class);                   
startActivityForResult(i,YOUR_CODE);

In your Activity B in its fragment在您的 Activity B 的片段中

Intent resultIntent = new Intent();
resultIntent.putExtra("NAME OF THE PARAMETER", valueOfParameter);
...
setResult(Activity.RESULT_OK, resultIntent);
finish();

And in your Activity A's onActivityResult() function do this:-在您的活动 A 的 onActivityResult()函数中执行以下操作:-

 @Override
 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
     super.onActivityResult(requestCode, resultCode, data);


     if (resultCode == RESULT_OK) {

         if (resultCode==YOUR_CODE)
         {
           String value = (String) data.getExtras().getString("NAME OF THE PARAMETER");  //get Data here

         }

     }

    }
In kotlin: - 
In class A
startActivityForResult(Intent(context, B::class.java), 143)

  override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        if (requestCode == 143) {
            if (data!!.extras.get("check").equals("0")) {
                childpager.currentItem = 0
            }
        }
    }

In Class B
 val intent = Intent()
 intent.putExtra("check", "0")
 setResult(Activity.RESULT_OK, intent)

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM