Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
505 views
in Technique[技术] by (71.8m points)

android - Handling fragment back stack with Navigation Drawer

I am implementing navigation drawer and works well. So i am calling fragment on navigation drawer click and it is also working and further more i am calling another fragment from Home page fragment and maintain the back stack for every fragment but the problem is back press from the child fragment i can't go to Home page fragment and just exited from application. I don't want this. What i want Click on

Navigation Drawer->HomePageFragment->AnotherChild Fragment(On List Item click of HomePageFragment)

but on back pressed without going to Homepage fragment its directly exit with application. Here is my code: (In Fragment Activity with Navigation Drawer)

class SlideitemListener implements ListView.OnItemClickListener {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position,
            long id) {

        updateDisplay(position);
    }

}

private void updateDisplay(int position) {
    Fragment fragment = null;
    switch (position) {
    case 0:
        fragment = new ScheduleFragment();
        break;
    case 1:
        fragment = new Result_Fragment();
        break;
    case 2:
        fragment = new Live_Match_Fragment();
        break;
    case 3:
        // fragment = new Live_Match_Fragment();
        break;

    case 4:
        fragment = new Team_Fragment();
        break;
    default:
        break;
    }

    if (fragment != null) {

        fragmentManager = getFragmentManager();
        fragmentManager.popBackStackImmediate("0", 0);
        int count = fragmentManager.getBackStackEntryCount();
        FragmentTransaction fragmentTransaction = fragmentManager
                .beginTransaction();
        fragmentTransaction.replace(R.id.frame_container, fragment)
                .setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
                .addToBackStack(String.valueOf(count)).commit();
        Log.e("Count in Activiy", ""+count);

        // update selected item and title, then close the drawer
        setTitle(menutitles[position]);
        mDrawerLayout.closeDrawer(mDrawerList);
    } else {
        // error in creating fragment
        Log.e("MainActivity", "Error in creating fragment");
    }

}

Now in BackPressed() in FragmentActivity.

@Override
public void onBackPressed() {
    if (fragmentManager.getBackStackEntryCount() <= 1) {
        finish();

        return;
    }
    super.onBackPressed();
}

Now calling another child fragment from HomePage fragment on Listview item click.

 team_lv.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {
            // TODO Auto-generated method stub
            TeamDetailFragment myDetailFragment = new TeamDetailFragment();
            FragmentManager fragmentManager = getFragmentManager();
            int count = fragmentManager.getBackStackEntryCount();
            Log.e("Count in Fragment", "" + count);
            FragmentTransaction fragmentTransaction = fragmentManager
                    .beginTransaction();
            fragmentTransaction
                    .replace(R.id.frame_container, myDetailFragment)
                    .setTransition(
                            FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
                    .addToBackStack(String.valueOf(count)).commit();

        }
    });

So anybody knows then help me. Help will be appreciate.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

There is an issue with nested fragments in android https://code.google.com/p/android/issues/detail?id=40323

Android doesn't handle back press well if transactions were in nested fragments. To surpass this i am using the following fix inside My Activity

@Override
public void onBackPressed() {
    // If the fragment exists and has some back-stack entry
    if (myFragment != null && myFragment.getChildFragmentManager().getBackStackEntryCount() > 0) {
        // Get the fragment fragment manager - and pop the backstack
        myFragment.getChildFragmentManager().popBackStack();
    }
    // Else, nothing in the direct fragment back stack
    else {
        // Let super handle the back press
        super.onBackPressed();
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...