Search code examples
androidandroid-fragmentsonbackpressedfragment-backstack

Android - Blank fragment when returning from backstack


I have a view that shows a List of Properties, at the bottom of the screen there's a button that opens a fragment containing a MapView.

  @Override
public void onClick(View v) {
    switch (v.getId()) {
        case R.id.find_property_btn_map:

            PropertyMapFragment fragment = PropertyMapFragment.newInstance(false, null);

            getActivity().getSupportFragmentManager().beginTransaction()
                    .addToBackStack(null)
                    .replace(((ViewGroup) getView().getParent()).getId(), fragment, PropertyMapFragment.class.getSimpleName())
                    .commit();

            break;
    }
}

The onCreateView method for my Properties fragments is as follows

@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    mRootView = inflater.inflate(R.layout.fragment_properties_list, container, false);
    getmBtnMap().setOnClickListener(this);
    getmBtnSaveSearch().setOnClickListener(this);
    getmBtnSort().setOnClickListener(this);
    getmListView().setAdapter(getPropertyAdapter());
    getmListView().setOnItemClickListener(this);
    getmListView().setOnScrollListener(this);
    getmListView().setScrollingCacheEnabled(false);

    searchProperties();

    return mRootView;
}

searchProperties(); takes care of calling a Web Service and filling the ArrayAdapter.

The thing is that, when I open the MapFragment and then press the back button, my Property fragment is blank and the buttons do not respond to onClick events.

I tried debugging and saw that onCreateView() is being called when coming back to Property fragment, but the buttons are no longer working and the listview is nowhere to be seen.

What am I doing wrong?


Solution

  • If you are trying to launch that web view from a fragment, then try to use add fragment instead of replace.

    For example :

    getActivity().getSupportFragmentManager().beginTransaction()
                        .addToBackStack(null)
                        .replace(YOUR_CONTAINER_ID, fragment, PropertyMapFragment.class.getSimpleName())
                        .commit();
    

    The code above is replace that fragment container with the new one and not adding that fragment on top of that. So when you do a replace fragment with that ID, it just replaces that view with new one.

    Now,

    Same code but with add:

    getActivity().getSupportFragmentManager().beginTransaction()
                            .addToBackStack(null)
                            .add(YOUR_CONTAINER_ID, fragment, PropertyMapFragment.class.getSimpleName())
                            .commit();
    

    Now, that web view fragment that you have will be added to the view/fragment and now when you press back from that web view, you previous fragment will be visible.

    Just make sure that that the ID you are replacing is the same as the one in which you have all the other properties.

    Maybe i misinterpreted the question so please correct me in that context.

    Hope this helps.