Search code examples
androidandroid-fragmentsandroid-listfragment

Getting View after fragmentTransaction.Replace()


I have an activity with two fragments in it (both dynamically created), one of which is a ListFragment. I have implemented an onListItemClick handler in the activity. When an item is clicked, I want to replace both fragments with other fragments, and populate a TextView. However, after replacing the fragments I can't seem to get the View object I need to manipulate the TextView in the new Details fragment -- it returns null. Here is some relevant code (onListItemSelected is the handler that processes onListItemClick in the main activity).

@Override
public void onListItemSelected(int index) {
    inflateCheckinFragment();
    FragmentManager fm = getFragmentManager();

    FragmentTransaction fragmentTransaction = fm.beginTransaction();
        cif = new checkInFragment();
        fragmentTransaction.replace(R.id.action_container, cif, "ACTIONS");
        fragmentTransaction.addToBackStack(null);
        fragmentTransaction.commit()

    FragmentTransaction fragmentTransaction2 = fm.beginTransaction();
        gdf = new GeolocDetailsFragment();
        fragmentTransaction2.replace(R.id.fragment_container, gdf, "DETAILS");
        fragmentTransaction2.addToBackStack(null);
        fragmentTransaction2.commit();

    View gdfView = gdf.getView();

    TextView tv = (TextView)  gdfView.findViewById(R.id.textPOI);
    tv.setText(printPOI(poiList.get(index)));
}

Solution

  • I ended up just setting up the data in the onListItemSelected method. selectedPOI is a private class member.

    public void onListItemSelected(int index) {
            FragmentManager fm = getFragmentManager();
    
            FragmentTransaction fragmentTransaction = fm.beginTransaction();
            fragmentTransaction.replace(R.id.action_container, cif, TAG_CHECKIN);
            fragmentTransaction.replace(R.id.fragment_container, gdf, TAG_DETAILS);
            fragmentTransaction.addToBackStack(null);
            fragmentTransaction.commit();
    
            selectedPOI = poiList.get(index);
        }
    

    Then in the GeolocDetailsFragment class, I set up a handler to be called in the Activity in the Fragment's onCreateView method to set the TextView value.

    public class GeolocDetailsFragment extends Fragment {
        private TextSetter textSetter;
    
        public interface TextSetter {
            public String getActivityText();
        }
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            View view = inflater.inflate(R.layout.geoloc_details_fragment, container, false);
            TextView detailsText = (TextView) view.findViewById(R.id.textPOI);
    
            detailsText.setText(textSetter.getActivityText());
    
            return view;
        }
    
        public void onAttach(Activity activity) {
    
            super.onAttach(activity);
    
            try {
                textSetter = (TextSetter) activity;
            }
            catch (ClassCastException e) {
                throw new ClassCastException(activity.toString() + " must implement OnGetPOIListener");
            }
        }
    
    }
    

    Finally, I implemented getActivityText() in the main activity to get the string to pass to the TextView.

        @Override
        public String getActivityText() {
    
            return printPOI(selectedPOI);
        }