-2

For Fragment(put data to activity)

m=(MainActivity)getActivity();
new Handler().postDelayed(new Runnable() {
      @Override
      public void run() {
         Intent in=new Intent(getActivity(),MainActivity.class)
         in.putExtra("test",test);
         startActivty(in)
      }
},10);

For Activity (get data from fragment )

{
String get_data=getIntent.getStringExtra("test");
}
//it will return always null...any body help me?

2 Answers2

3

startActivty(in) will start the same activity.

Instead of this, you can make use of Interface. It's the easiest way to pass the data.

in your fragment, you can have an interface like,

SubmitData mSubmitData;

public interface SubmitData{
        void DataListener(String s);
    }


mSubmitData.DataListener("data to be sent");

In your activity, implement the SubmitData interface. It will make you override the DataListener method, where you can get the data.

public class MyActivity extends AppCompatActivity implements YourFragment.SubmitData{

     @Override
      public void DataListener(String s) {
         // Data from the fragment
    }
debugger
  • 580
  • 1
  • 6
  • 16
  • Sorry it is not working,could you give sample code for detaily? i need data put to MainActivity from fragment(get that string on MainActivity class)... could you help me? – Navaneethakrishnan Aug 30 '17 at 09:42
  • add the interface to the fragment. When the event gets triggered, pass the data to the method. On MainActivity, implement the Interface. It will make you override the method that you have in your interface. When ever event gets triggered on the fragment, you will get the data on the activity – debugger Aug 30 '17 at 09:55
  • mSubmitData.DataListener("data to be sent");(can not resolve symbol DataListener) here DataListener ???? need to create new DataListener class??? how to pass data??? please help me!!! – Navaneethakrishnan Aug 30 '17 at 10:46
  • bro, u need to create an interface with the method "DataListener" i.e. public interface SubmitData{ void DataListener(String s); } in your fragment – debugger Aug 30 '17 at 11:17
  • SubmitData mSubmitData; public interface SubmitData{ void DataListener(); } mSubmitData.DataListener("sent","sent"); here DataListener can not resolve symbol – Navaneethakrishnan Aug 30 '17 at 11:27
  • yes bro....i have created interface in my class(extends fragment)but where(how) to pass data like(mSubmitData.DataListener("data to be sent");) here DataListener can not resolve symbol...pleaes help me? – Navaneethakrishnan Aug 30 '17 at 11:28
  • if you are trying to pass key value pair like ("sent","sent"), your method inside the interface should be same like void DataListener(String key, String value)... You haven't passed anything... So it's throwing you the error. – debugger Aug 30 '17 at 11:40
  • Ok... bro i have cleared...but i need where to declare --------mSubmitData.DataListener(key , value); – Navaneethakrishnan Aug 30 '17 at 12:25
  • where do you have your action...when do you want your data to be sent??? For example, action can be your button click listener... Hope it is clear. – debugger Aug 30 '17 at 12:35
0

This questions has been asked and answered multiple times. You can find a valid reply here https://stackoverflow.com/a/9977370/5828132

Basically, it consists of creating an interface in the Fragment (for example) including a simple method. The Fragment has to declare a field of that type, and the Activity hosting the Fragment has to implement (implements) that interface. Both entities are usually connected using a explicit cast, in the onAttach() callback of the Fragment life-cycle, i.e.:

@Override
public void onAttach(Context context) {
   super.onAttach(context);
   // fragmentField_interfaceType = (interfaceType) context;
}

Hope it helps!

Pablo L.
  • 1
  • 3