0

Scenario is: I have two fragments FragmentA and FragmentB. In FragmentA, I have a simple form and in FragmentB, I am showing data of that form in ListView.

I am getting data in listview but not in real time. Like first I insert data, then, I restart Application to see data in list.

My question is how to notify change from one fragment to another in real time.

Arman Reyaz
  • 417
  • 1
  • 5
  • 9
  • 1
    Possible duplicate of [How to pass data between fragments](http://stackoverflow.com/questions/5194548/how-to-pass-data-between-fragments) – Mike M. Apr 11 '16 at 10:10
  • Check this: http://stackoverflow.com/questions/36363185/receiving-data-from-a-dialogfragment-if-youre-calling-from-an-activity-vs-a-fra/36363310#36363310 – Rohit Arya Apr 11 '16 at 10:12

2 Answers2

0
you can use listener in your activity and then notify:
class MyActivity extends Activity {
FragmentA fragA;
FragmentB fragB;

public void notifyB() {
    fragB.notifyB();
}

class FragmentA extends Fragment{
 private void notifyB(){
     ((MyActivity) getActivity()).notifyB();
 }
class FragmentB extends Fragment{
    public notifyB() {
         // do something
    }
}
A-_-S
  • 680
  • 5
  • 21
  • of course better practice is to define interface, but you use it with the same mechanism – A-_-S Apr 11 '16 at 10:13
0

After setting the variable of the FragmentTransaction you can do like this:

Bundle bundle = new Bundle();
bundle.putString("flag", variable)// You can pass Strings, Int, Boolean. 
//The "flag" is the key to get the value in the other fragment.
//The variable is wat you need to pass to the other fragment.

In you new fragment you can get your viable like this:

Bundle bundle = new Bundle();
String a = bundle.getString("flag");
// now you have te value in the variable a

this works in eclipse

Manuel_Rodrigues
  • 560
  • 2
  • 18