1

i've written an activity with three fragments. PlantFragment.java is the main fragment that has a floating button to select a farmer. This FAB starts a fragment FarmersSelectorFragment.java which gives a list of farmers and on selecting one, it pops from the stack returning a result to the PlantFragment.java. Then i want to start another Fragment when the result is ok. The FarmersSelectorFragment.java returns the farmer selected, however starting the next fragment fails with nothing happening. How can i start a new Fragment when a result is returned.

PlantFragment.java

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (resultCode == RESULT_OK) {
            if (requestCode == REQUEST_CODE) {
                Fragment fragment = new PlantAddFragment();
                fragment.setArguments(data.getBundleExtra("bundle"));

                getFragmentManager().beginTransaction()


.replace(((ViewGroup) getView().getParent()).getId(), fragment)
                        .addToBackStack(null)
                        .commit();
            }
        }
    }

I managed to pick the returned data on result however (ViewGroup) getView().getParent()).getId() throws a null pointer in the onActivityResult

Isaac Obella
  • 2,613
  • 2
  • 16
  • 29
  • Is FarmetSelectorFragment is part of same activity which contains plant fragment? – mudit_sen Sep 21 '17 at 10:36
  • In FarmersSelectorFragment you find a fragment by tag PAGE_ID but I don't see you adding a tag when you replace the fragment. You are adding TAG as an argument to the Fragment you created instead. See https://developer.android.com/reference/android/app/FragmentTransaction.html#replace(int, android.app.Fragment, java.lang.String) Do you even reach the fragment transaction code in onActivityResult? – Peter Sep 21 '17 at 10:41
  • i did pick the PAGE_ID using if (bundle != null) { PAGE_ID = bundle.getString("PAGE_ID"); } and used it to return the result to the fragment – Isaac Obella Sep 21 '17 at 10:49
  • See this link may help you:https://www.androidtutorialonline.com/onactivityresult-in-fragment/ – Android Tutorial Sep 17 '18 at 07:48

0 Answers0