Tried to use bundle, but for some reason returns null

In the onCreate method of the first fragment of fmonday (which should receive data) wrote:

 string1 = getArguments().getString("latitude"); 

From the AlertDFragment dialog I transfer the data:

  Bundle bundle = new Bundle(); bundle.putString("latitude", string1); fmonday newfrag = new fmonday(); newfrag.setArguments(bundle); 

That is, when onDismiss(); must pass one string variable.

What's wrong? Or are there other outputs?

  • You already thought it was answered that it is necessary to transfer through activites, and not directly from the fragment to the fragment. Or use some kind of bus like EventBus or Otto for direct transfer from fragment to fragment - pavlofff

1 answer 1

You can use a static instance of the class with the necessary info.

For example:

 public class MyDataClass { public static MyDataClass sData; private int myData; private Context ctx; // необязательно, только если нужно использовать контекст в работе с данными ... public MyDataClass (Context ctx) { this.ctx = ctx; ...первичная инициализация myData; } public static MyDataClass get (Context ctx) { if (sData == null) sData = new MyDataClass (ctx); return sData; } public void setData (int data) { myData = data; } public int getData () { return myData; } } 

use in DialogFragment :

  MyDataClass.get(getActivity()).setData(); MyDataClass.get(getActivity()).getData();