Search code examples
androidandroid-fragmentsandroid-listviewback-stackandroid-nested-fragment

Android popBackStack does not load ListView in ListFragment


I ran into this problem where my ListFragment is empty after having popBackStackImmediate I have a main activity which extends FragmentActivity and I have a Navigation drawer attached with my main activity. The first navigation calls a Fragment which has a custom adapter which implements FragmentStatePagerAdapter showing three tabs. each of the tabs are ListFragments them selves. when i click on any row of the list it goes to another fragment, till this everything works fine, now I want to implement the backstack so that when user presses back button from the detail page they can come back to the FragmentList page again. when I press back button it takes me back to the FragmentList page that is fine but the page is empty, for some reason the list does not shows up. I've checked the log and found the data is there and it goes till last bit of the code with out any error but the page is empty, can any one help me with that?

When I select any item from the List of any tab I add the transaction into the backstack and move on to detail fragment:

@Override
public void onListItemClick(ListView l, View v, int position, long id) { 

    Fragment fragment = new LiveMatchDetailMainFragment();
    android.support.v4.app.FragmentManager fragmentManager = getFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    fragmentTransaction.replace(R.id.content_frame, fragment);
    fragmentTransaction.addToBackStack("LiveFragment");
    fragmentTransaction.commit();
}

In my main activity I have this:

@Override
public void onBackPressed(){
    Log.d("stack","back pressed");
    FragmentManager fm = getSupportFragmentManager();
    if (fm.getBackStackEntryCount() > 0) 
    {           
        //fm.popBackStack();
        boolean done = fm.popBackStackImmediate();
        Log.i("stack", String.valueOf(done));
    } else 
    {
        Log.i("stack", "nothing on backstack, calling super");
        super.onBackPressed();  
    }
}

which takes me to exact tab where I clicked the item from the list, but only problem is the fragment is empty. In the FragmentList page I have:

@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
    super.setUserVisibleHint(isVisibleToUser);

    if (isVisibleToUser) {
        LiveMatchList = new ArrayList<HashMap<String, Object>>();
        //Log.d("LiveMatchList setUserVisibleHint","setUserVisibleHint");
        new LoadUpcomingMatchList().execute();

    }
}

which creates a list using custom adapter, and it works fine for the first time, when I pop the backstack this method gets call and loads all the data comes down till onPostExecute but the list is never showed, sorry guys I am not new to stackoverflow but this is my second question post so I might not follow the convention correctly...


Solution

  • Apparently with some help I fixed the issue... I added the fragment transactions within the FragmentActivity and used my onListItemClick() callback method to call a FragmentActivity method, which should run the transaction.

    In EventFragment replaced:

    FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
    viewpager.setAdapter(new MyAdapter(fragmentManager));
    

    with:

    viewpager.setAdapter(new MyAdapter(getChildFragmentManager()));
    

    it was the getChildFragmentManager() that solved my problem mainly.