Search code examples
androidlistviewandroid-fragmentsandroid-nested-fragment

Nested fragment issue - frag1 calls frag2 which has an impact on the listview from frag1


I thought that calls to a fragment were synchronous but it's not. I have a fragment 1 that contains a listview that I update through an adapter. Then I have a button that launches fragment 2. Frag2 has an effect on the listview of frag1 so I thought I could do as follows below. But when I click on button btn1 my fragment frag2 is launched and immediately after I see the log.i.

I would like to avoid a refresh button in frag1.

Is there a way to do an action right after frag2 has ended?

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    context = getActivity().getApplicationContext();

    initFindView();

    btn1.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            FragmentTransaction ft = getFragmentManager().beginTransaction();
            Frag2 f2 = new Frag2();
            ft.replace(R.id.content_frame, f2);
            ft.addToBackStack(null);
            ft.commit();

            Log.i("TEST","Test - back from tst2");

        }
    });

}

Solution

  • By your question, you look to only need the result, you can achieve it trough startActivity(ForResult) or with Fragments trough ResultReceiver that is a Parcelable that you can use as argument, and wait for the result to update your Frag1.