如何使用导航抽屉中的后退按钮导航到上一个片段?

时间:2014-04-09 15:46:20

标签: android android-fragments back-button navigation-drawer

我知道以前曾经问过类似的问题,但我似乎无法找到我正在寻找的东西。我的问题是我的后退按钮当前正在退出应用程序。我要做的是让它导航到前一个片段。就像你打开fragment1 - > fragment2,然后按后退按钮,它会带你回到fragment1。

我目前的代码是这样的。

public class MainActivity extends FragmentActivity {
private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
private ActionBarDrawerToggle mDrawerToggle;

private CharSequence mDrawerTitle;
private CharSequence mTitle;
private String[] mPageTitles;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mTitle = mDrawerTitle = getTitle();
    mPageTitles = getResources().getStringArray(R.array.menu_array);
    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    mDrawerList = (ListView) findViewById(R.id.left_drawer);

    // set a custom shadow that overlays the main content when the drawer
    // opens
    mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow,
            GravityCompat.START);





    // set up the drawer's list view with items and click listener
    mDrawerList.setAdapter(new ArrayAdapter<String>(this,
            R.layout.drawer_list_item, mPageTitles));
    mDrawerList.setOnItemClickListener(new DrawerItemClickListener());

    // enable ActionBar app icon to behave as action to toggle nav drawer
    getActionBar().setDisplayHomeAsUpEnabled(true);
    getActionBar().setHomeButtonEnabled(true);

    // ActionBarDrawerToggle ties together the the proper interactions
    // between the sliding drawer and the action bar app icon
    mDrawerToggle = new ActionBarDrawerToggle(this, /* host Activity */
    mDrawerLayout, /* DrawerLayout object */
    R.drawable.ic_drawer, /* nav drawer image to replace 'Up' caret */
    R.string.drawer_open, /* "open drawer" description for accessibility */
    R.string.drawer_close /* "close drawer" description for accessibility */

    )

    {
        public void onDrawerClosed(View view) {
            getActionBar().setTitle(mTitle);
            invalidateOptionsMenu(); // creates call to
                                        // onPrepareOptionsMenu()
        }

        public void onDrawerOpened(View drawerView) {
            getActionBar().setTitle(mDrawerTitle);
            invalidateOptionsMenu(); // creates call to
                                        // onPrepareOptionsMenu()
        }

    };
    mDrawerLayout.setDrawerListener(mDrawerToggle);

    // Sets the firstpage if no state is found
    if (savedInstanceState == null) {
        selectItem(0);
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.main, menu);

    return super.onCreateOptionsMenu(menu);
}


/* Called whenever we call invalidateOptionsMenu() */
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
    // If the nav drawer is open, hide action items related to the content
    // view
    return super.onPrepareOptionsMenu(menu);
}

// Toogles the drawer menu
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    return mDrawerToggle.onOptionsItemSelected(item);

}

/* The click listner for ListView in the navigation drawer */
private class DrawerItemClickListener implements
        ListView.OnItemClickListener {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position,
            long id) {
        selectItem(position);
    }
}

private void selectItem(int position) {
    // update the main content by replacing fragments

    FragmentManager fragmentManager = getFragmentManager();

    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

    switch (position) {
    case 0:
        KontaktInfoFragment kontakt = new KontaktInfoFragment();
        fragmentTransaction.replace(R.id.content_frame, kontakt);
        fragmentTransaction.addToBackStack("Replace");
        fragmentTransaction.commit();

        break;
    case 1:
        OmOssFragment omoss = new OmOssFragment();
        fragmentTransaction.replace(R.id.content_frame, omoss);
        fragmentTransaction.addToBackStack("Replace");
        fragmentTransaction.commit();
        break;
    case 2:
        MainScreenActivity mainscreen = new MainScreenActivity();
        fragmentTransaction.replace(R.id.content_frame, mainscreen);
        fragmentTransaction.addToBackStack("Replace");
        fragmentTransaction.commit();
        break;
    case 3:
        LoginFragment login = new LoginFragment();
        fragmentTransaction.replace(R.id.content_frame, login);
        fragmentTransaction.addToBackStack("Replace");
        fragmentTransaction.commit();
        break;
    case 4:
        ContactFormFragment form = new ContactFormFragment();
        fragmentTransaction.replace(R.id.content_frame, form);
        fragmentTransaction.addToBackStack("Replace");
        fragmentTransaction.commit();
        break;  

    }

    // update selected item and title, then close the drawer
    mDrawerList.setItemChecked(position, true);
    setTitle(mPageTitles[position]);
    mDrawerLayout.closeDrawer(mDrawerList);
}

@Override
public void setTitle(CharSequence title) {
    mTitle = title;
    getActionBar().setTitle(mTitle);
}

}

1 个答案:

答案 0 :(得分:0)

当您按下碎片内部的后退按钮onBackPressed()时,如果您声明了活动,则会调用您的活动方法。因此,导航抽屉内的碎片处理后退按钮可以是这样的方式..

MainActvity

     public static boolean isMainActivityShown ;
     public static boolean isFragment1Shown=false ;
     public static boolean isFragment2Shown=false ;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        isMainActivityShown=true  //inside onCreate method put isMainActivityShown true
. 
. 
.
}
                 Fragment currentFragment = new Fragment1();
                  isMainActivityShown=false;   //when moving to fragment1 
                isFragment1Shown=true;
               frgManager = getFragmentManager();
               frgManager.beginTransaction().replace(R.id.content_frame, currentFragment)
                                        .commit();
     @Override
        public void onBackPressed() {

            if(isMainActivityShown)
            {
                finish();
            }
            else if(isFragment1Shown)
            {
               //write the code to handle back button when you are in Fragment1
            }
           else if(isFragment2Shown)
            {  //When you are in Fragment 2 pressing back button will move to fragment1
                Fragment currentFragment = new Fragment1();
                isFragment2Shown=false;
    isFragment1Shown=true;

                FragmentManager frgManager;
                frgManager = getFragmentManager();
                frgManager.beginTransaction().replace(R.id.content_frame, currentFragment)
                        .commit();
            }

            }

片段1

Fragment currentFragment = new Fragment2();

            MainActivity.isFragment1Shown=false;
MainActivity.isFragment2Shown=true;
                            frgManager = getFragmentManager();
                            frgManager.beginTransaction().replace(R.id.content_frame, currentFragment)
                                    .commit();