2

I have a fragment(FragmentA) where I am making an Async call, and navigating to another fragment(FragmentB). FragmentB has a listview, which needs to be refreshed when the onPostExecute method is called, in the FragmentA. How to achieve this?

I tried to make the adapter static in FragmentB, and notify it from FragmentA's onPostExecute. But this doesn't help.

 protected void onPostExecute(String isEmpty) {
        pDialog.dismiss();
        FragmentB.adapter.notifyDateSetChanged();
        }

Please suggest some way to do this!

pagalpanda
  • 150
  • 2
  • 16
  • looking into following link for implementing interface for communication between fragments http://developer.android.com/training/basics/fragments/communicating.html – Nandakishore Shetty Sep 07 '15 at 12:41

3 Answers3

0

Communication between fragments should be via the container activity. Make a public method in your FragmentB which updates the adapter data. In FragmentA, when the data retrieved call the activity and then the activity should call the FragmentB request for data refresh and pass the data he gets from FragmentA to FragmentB.

galvan
  • 7,400
  • 7
  • 38
  • 55
0

What ever data that you are getting after the network call, pass that to a method of an interface in FragmentA. Make your container activity implement that interface and override its method. From your container activity, now pass the data from FragmentA to your other fragment. This is the most modular approach. Here is one link that might help you.

Community
  • 1
  • 1
Kaveesh Kanwal
  • 1,753
  • 17
  • 16
  • I am getting null for FragmentB fragment = (FragmentB) getSupportFragmentManager().findFragmentById(R.id.fragmentPostsOffer); Also, I forgot to mention, that, FragmentA has 2 tabs-one of them holds FragmentB. – pagalpanda Sep 07 '15 at 17:18
0

Create an interface like:

public interface SetNofifyListener {

    public void onNotify(boolean flag);
}

then in your async class:

SetNofifyListener mSetOnNotify;

 protected void onPostExecute(String isEmpty) {
        pDialog.dismiss();

        mSetOnNotify.onNotify(true);
 }

then implement this interface in your fragmentB and do whatever you want to do in its @Override method.

Pravesh
  • 822
  • 1
  • 8
  • 20