2

Just for reference I am been trying to follow the answer to this question

Basic Communication between two fragments

I have 2 Fragments within a ViewPager Adapter along with an Actionbar.

What I have is one fragment produces some data which can (if chosen) inserted to an SQLite table. The second Fragment simply displays all data in the table, however I am trying to make some communication that as soon as Fragment 1 inserts data into the SQLite table. Fragment 2 is called to refresh its select query (as in do the query again) to automatically show the latest data. At the moment this is manually done with a button which I feel is not great.

This is my interface in Fragment 1

onNumbersSavedListener mCallback;

public interface onNumbersSavedListener
{
    public void RequestQueryRefresh();
}


@Override
public void onAttach(Activity activity) {
    // TODO Auto-generated method stub
    super.onAttach(activity);

    try
    {
        mCallback = (onNumbersSavedListener) activity;
    }
    catch(ClassCastException e)
    {
        e.printStackTrace();
    }
}

This is the main Activity which contains the ViewPager and implements the interface

public class MainActivity extends FragmentActivity implements TabListener, GenerateFragment.onNumbersSavedListener

This is the main problem I am having which I do not have IDs for the fragments which answer referred in the link stated above does so.

@Override
public void RequestQueryRefresh() {
    // TODO Auto-generated method stub

}

TLDR: I am just looking for an easy and clean way for as soon as Fragment 1 saves into DB, fragment 2 updates its list view by re-running its query.

Community
  • 1
  • 1
user3364963
  • 387
  • 1
  • 9
  • 28
  • 1
    http://stackoverflow.com/questions/7379165/update-data-in-listfragment-as-part-of-viewpager – user Jul 20 '14 at 15:29
  • I believe the best approach to your problem is to use EventBus as otto! – Anderson K Jul 20 '14 at 15:29
  • I tried the answer pointed by Luksprog a try. I get null pointer exception. I will investigate what eventbus is and give it a shot – user3364963 Jul 20 '14 at 15:54
  • I edited my answer to correct some errors :)! You will enjoy, EventBus is a simple way to send events to listeners, broadcast style but much more flexible! lib that other genre, has a good explanation of this type of event [EventBus](http://greenrobot.github.io/EventBus/) – Anderson K Jul 20 '14 at 16:05

3 Answers3

2

see more about otto lib here : http://square.github.io/otto/

Edited:

public class FragmentA extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,  Bundle savedInstanceState) {            

        return super.onCreateView(inflater, container, savedInstanceState);
    }

    public void saveData(){

        //save datas before
        BusProvider.getInstance().post(new EventUpdateOtto());
    }
}


public class EventUpdateOtto{

    public EventUpdateOtto(){
    }
}

public class FragmentB extends Fragment {


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,  Bundle savedInstanceState) {
        return super.onCreateView(inflater, container, savedInstanceState);
    }


    @Subscribe
    public void subUpdateList(EventUpdateOtto mEventUpdateOtto){
        //update yout list here
    }

    @Override
    public void onResume() {
        BusProvider.getInstance().register(this);
        super.onResume();
    }

    @Override
    public void onPause() {
        BusProvider.getInstance().unregister(this);
        super.onPause();
    }
}


public  class BusProvider {
      private static final Bus BUS = new Bus();

      public static Bus getInstance() {
        return BUS;
      }

      private BusProvider() {
        // No instances.
      }
}
Anderson K
  • 5,115
  • 5
  • 29
  • 48
0

In your case you can improve your interface:

public interface onNumbersSavedListener
{
    public void RequestQueryRefresh(Bunde bundle/*or something other*/);
}
Eldar Miensutov
  • 1,767
  • 1
  • 14
  • 22
0

If you are using a cursor loader, the change should automatically be reflected in the fragment displaying it. However, the fragment that wants immediate updates whenever the table is changed can register as an observer to that table:

  // observer to the table
class MyObserver extends ContentObserver {

    final Handler mHandler;

    public MyObserver(Handler handler) {
        super(handler);
        // I used a handler to get back to my UI thread
        mHandler = handler;
    }

    @Override

    public void onChange(boolean selfChange) {

        this.onChange(selfChange, null);
    }

    @Override
    public void onChange(boolean selfChange, Uri uri) {

        Log.i(TAG, "MyObserver: onChange");
        // do what you want to do - this is what I implemented
        mHandler.post(myRunnable);
    }
}

Then, register it:

  mHandler = new Handler();
  mObserver = new MyObserver(mHandler);
  ContentResolver resolver = getContext().getContentResolver();
  resolver.registerContentObserver(uri, false, mEventLogObserver);

The other fragment should then do a notify:

  getContext().getContentResolver().notifyChange(uri, null);

The key is the uri - one watches it, the other notifies.

Janin
  • 41
  • 3