How to send data from fragment to activity [duplicate]

痞子三分冷 提交于 2019-12-30 05:29:11

问题


In my application I have two fragments in an activity. In one of the fragments I have data, such as :

String name = "Transporter";

I want send this name to container activity. How can I do it? Please help me.


回答1:


The fragment will be attached to the activity which you launch from.

Thus, you can create a callback method in your activity which can be called from fragment using the activity context object.

Please see the below code snippet :

public class YourFragment extends Fragment{

       OnCallbackReceived mCallback;

// Implement this interface in your Activity.

public interface OnCallbackReceived {
    public void Update();
}

In your fragment :

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);

    try {
        mCallback = (OnCallbackReceived) activity;
    } catch (ClassCastException e) {

    }
}

    // You can Call the event from fragment as mentioned below
    // mCallback is the activity context. 
    mCallback.Update();

Activity :

public class MainActivity extends Activity
        implements YourFragment.OnCallbackReceived {

    // Implemented method.
    public override void Update() {
        // Write your logic here.
    }



回答2:


try below code

// You Activity implements your interface
public class YourActivity implements FragmentA.TextClicked{
    @Override
    public void sendText(String text){
        // Your text 
    }
}

// Fragment A defines an Interface, and calls the method when needed

public class FragmentA extends Fragment {

    TextClicked mCallback;

    public interface TextClicked {
        public void sendText(String text);
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);

        // This makes sure that the container activity has implemented
        // the callback interface. If not, it throws an exception
        try {
            mCallback = (TextClicked) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString()
                + " must implement TextClicked");
        }
    }

    public void someMethod(){
        mCallback.sendText("YOUR TEXT");
    }

    @Override
    public void onDetach() {
        mCallback = null; // => avoid leaking, thanks @Deepscorn
        super.onDetach();
    }
}



回答3:


If you are calling an activity from a fragment and you want to send data to Activity you can use intents like below:

Intent intent = new Intent(getActivity(), YourActivity.class);
String name = "Transporter";
intent.putExtra("name", name);
startActivity(intent);

And in your activity you should get the data like this:

try {

    Intent intent = getIntent();
    String name = intent.getStringExtra("name");

} catch(Exception e) {
    e.printStackTrace();
}



回答4:


You need to create an interface.

Like 

interface DataReciver
{

  void getData(String data);

}


public class MainActivity extends AppcompactActivity
{

 // On fragment load


oncreate
{
  DataReciver obj=new DataReciver()
  {
   @overide
   void getData(String data)
   {
      // Here is your data in data variable 

   }
  }


  frgamenttranstatiion.add(YOUR CONTAINER, new FRAGMENT1(obj));
}


}


Create a fragment with constructor

public class Fragment1
{


 Fragment1(DataReciver datareciver)
 {
   datareciver.getData("amit sharma");
 }
}



回答5:


Currently best approach(and my suggestion) is using ViewModel. you can find samples in android site. But I should says it is still beta version.

https://developer.android.com/topic/libraries/architecture/viewmodel.html




回答6:


You can do it like

Create Method in activity like

public void doSomething(String abc)
{
  // do your stuff here
}

and access it from fragment

like

((HomeActivity)getActivity()).doSomething(string);


来源:https://stackoverflow.com/questions/45429603/how-to-send-data-from-fragment-to-activity

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!