1

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.

Thunder Dragon
  • 581
  • 2
  • 11
  • 30

6 Answers6

7

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.
    }
4

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();
}
sam
  • 1,047
  • 1
  • 20
  • 32
4

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();
    }
}
arne.z
  • 2,938
  • 2
  • 22
  • 42
Rhn Bhadani
  • 2,208
  • 1
  • 17
  • 25
0

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");
 }
}
Amit Sharma
  • 928
  • 2
  • 10
  • 34
  • my friend I want send from fragment to activity, not activity to fragment –  Aug 01 '17 at 05:38
  • Check the code carefully this is to send data from fragment to activity Check the custructor of fragment when i am call datareciver.getData("amit sharma"); this goes to activity obj which is activity – Amit Sharma Aug 01 '17 at 05:39
0

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

Afshin
  • 7,636
  • 1
  • 13
  • 42
0

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);
Anil
  • 1,067
  • 1
  • 10
  • 24