0

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");

        }
    });

}
narb
  • 958
  • 1
  • 13
  • 39
  • "Frag2 has an effect on the listview of frag1" - I suppose this means actions taken while Frag2 is displayed will change the data which is shown in the ListView? If this is the case, maybe you should use a LoaderManager (like in this [blog post by Alex J. Lockwood](http://www.androiddesignpatterns.com/2012/07/understanding-loadermanager.html)) – Bö macht Blau Sep 19 '17 at 17:37
  • Looks good. I will give it a try. Thanks. – narb Sep 19 '17 at 17:51

1 Answers1

1

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.

Marcos Vasconcelos
  • 18,136
  • 30
  • 106
  • 167
  • I was wrongly under the impression you could not start an activity from a fragment. Thanks. – narb Sep 20 '17 at 06:28