如何使用导航抽屉活动中的后退按钮从片段返回到主片段

时间:2017-05-04 05:07:16

标签: android android-fragments onbackpressed

我有导航抽屉活动。我附加了片段A(主要片段)。在我的导航抽屉菜单中,我有其他碎片(B,C,D ..)。现在,当我从菜单(B,C,D)转到一些片段时,我按下后退按钮会发生什么,即app退出。我想要的是当我按下后退按钮返回片段A,而不是再次按下后退按钮退出应用程序。

这是我的主要活动:

 @Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);

    setSupportActionBar(toolbar);

    drawer = (DrawerLayout) findViewById(R.id.drawer_layout);

    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
            this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.setDrawerListener(toggle);

    toggle.syncState();

   // FRAGMENT A starting fagment   
    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);

    FragmentMainPage pocetnaFragment = new FragmentMainPage();
    FragmentManager fragmentManager = getSupportFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    fragmentTransaction.add(R.id.relativeLayoutZaFragment, pocetnaFragment, "1");
    fragmentTransaction.commit();     

    getSupportActionBar().setTitle("Auto magazin");

    ActionBar.LayoutParams p = new ActionBar.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
    p.gravity = Gravity.LEFT;

    view = getLayoutInflater().inflate(R.layout.action_bar, null);

    ActionBar.LayoutParams params = new ActionBar.LayoutParams(
            ActionBar.LayoutParams.WRAP_CONTENT,
            ActionBar.LayoutParams.MATCH_PARENT,
            Gravity.LEFT);

    TextView Title = (TextView) view.findViewById(R.id.actionbar_title);

    Title.setText("Auto magazin");

    getSupportActionBar().setCustomView(view,params);
    getSupportActionBar().setDisplayShowCustomEnabled(true); //show custom title
    getSupportActionBar().setDisplayShowTitleEnabled(false); //hide the default title
}

boolean doubleBackToExitPressedOnce = false;

@Override
public void onBackPressed() {
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    assert drawer != null;


    if (drawer.isDrawerOpen(GravityCompat.START)) {
        drawer.closeDrawer(GravityCompat.START);
        return;
    }
    else if (doubleBackToExitPressedOnce) {
        super.onBackPressed();
        return;
    }

    this.doubleBackToExitPressedOnce = true;

    Toast.makeText(this, "Pritisnite još jednom za izlaz", Toast.LENGTH_SHORT).show();

    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            doubleBackToExitPressedOnce = false;
        }
    }, 2000);
}

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

    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item)
{
    int id = item.getItemId();

    if (id == R.id.podesavanja) {

        startActivity(new Intent(this, Podesavanja.class));
        return true;
    }

    return super.onOptionsItemSelected(item);
}

@SuppressWarnings("StatementWithEmptyBody")
@Override

 // Other fragments, I want to back to Fragment A when click on back button
 // when i am in this Fragments
public boolean onNavigationItemSelected(MenuItem item)   {
    // Handle navigation view item clicks here.
    int id = item.getItemId();

    if (id == R.id.pocetna)   {

        FragmentMainPage fragmentMainPage = new FragmentMainPage() ;
        FragmentManager manager = getSupportFragmentManager();
        manager.beginTransaction().replace(R.id.relativeLayoutZaFragment
               , fragmentMainPage, fragmentMainPage.getTag()).commit();

        TextView Title = (TextView) view.findViewById(R.id.actionbar_title);
        Title.setText("Auto magazin");
    }
    else if (id == R.id.info) {
         FragmentMenuPages fragmentMenuPages = new FragmentMenuPages();
         FragmentManager  manager = getSupportFragmentManager();
         manager.beginTransaction().replace(R.id.relativeLayoutZaFragment
                , fragmentMenuPages, fragmentMenuPages.getTag()).commit();

        TextView Title = (TextView) view.findViewById(R.id.actionbar_title);
        Title.setText("Info");

        Bundle bundle = new Bundle();

        bundle.putString("href","http://www.magazinauto.com/category/info/");
        bundle.putString("title", "Info");
        fragmentMenuPages.setArguments(bundle);

    }
    else if (id == R.id.testovi)  {
        FragmentMenuPages fragmentMenuPages = new FragmentMenuPages();
        FragmentManager manager = getSupportFragmentManager();
        manager.beginTransaction().replace(R.id.relativeLayoutZaFragment
                , fragmentMenuPages, fragmentMenuPages.getTag()).commit();

        TextView Title = (TextView) view.findViewById(R.id.actionbar_title);
        Title.setText("Testovi");

        Bundle bundle = new Bundle();
        bundle.putString("href", "http://www.magazinauto.com/category/testovi/");

        bundle.putString("title", "Testovi");
        fragmentMenuPages.setArguments(bundle);

    }
    else if (id == R.id.promocije)  {
        FragmentMenuPages fragmentMenuPages = new FragmentMenuPages();
        FragmentManager  manager = getSupportFragmentManager();
        manager.beginTransaction().replace(R.id.relativeLayoutZaFragment
                , fragmentMenuPages, fragmentMenuPages.getTag()).commit();

        TextView Title = (TextView) view.findViewById(R.id.actionbar_title);
        Title.setText("Promocije");

        Bundle bundle = new Bundle();
        bundle.putString("href", "http://www.magazinauto.com/category/promocije/");
        bundle.putString("title", "Promocije");
        fragmentMenuPages.setArguments(bundle);
    }

    else if (id == R.id.svet) {

        FragmentMenuPages fragmentMenuPages = new FragmentMenuPages();
        FragmentManager  manager = getSupportFragmentManager();
        manager.beginTransaction().replace(R.id.relativeLayoutZaFragment
                , fragmentMenuPages, fragmentMenuPages.getTag()).commit();

        TextView Title = (TextView) view.findViewById(R.id.actionbar_title);
        Title.setText("Svet");

        Bundle bundle = new Bundle();
        bundle.putString("href", "http://www.magazinauto.com/category/svet/");
        bundle.putString("title", "Svet");
        fragmentMenuPages.setArguments(bundle);

    }

    else if (id == R.id.istorija)  {
        FragmentMenuPages fragmentMenuPages = new FragmentMenuPages();
        FragmentManager manager = getSupportFragmentManager();
        manager.beginTransaction().replace(R.id.relativeLayoutZaFragment
                , fragmentMenuPages, fragmentMenuPages.getTag()).commit();

        TextView Title = (TextView) view.findViewById(R.id.actionbar_title); // OTHER FRAGMENTS
        Title.setText("Istorija");

        Bundle bundle = new Bundle();
        bundle.putString("href", "http://www.magazinauto.com/category/istorija/");
        bundle.putString("title", "Istorija");
        fragmentMenuPages.setArguments(bundle);

    }
    else if (id == R.id.tehnika)  {
        FragmentMenuPages fragmentMenuPages = new FragmentMenuPages();
        FragmentManager manager = getSupportFragmentManager();
        manager.beginTransaction().replace(R.id.relativeLayoutZaFragment
                , fragmentMenuPages, fragmentMenuPages.getTag()).commit();

        TextView Title = (TextView) view.findViewById(R.id.actionbar_title);
        Title.setText("Tehnika");

        Bundle bundle = new Bundle();
        bundle.putString("href", "http://www.magazinauto.com/category/tehnika/");
        bundle.putString("title", "Tehnika");
        fragmentMenuPages.setArguments(bundle);
    }

    else if (id == R.id.transport) {
        FragmentMenuPages fragmentMenuPages = new FragmentMenuPages();
        FragmentManager manager = getSupportFragmentManager();
        manager.beginTransaction().replace(R.id.relativeLayoutZaFragment
                , fragmentMenuPages, fragmentMenuPages.getTag()).commit();

        TextView Title = (TextView) view.findViewById(R.id.actionbar_title);
        Title.setText("Transport");

        Bundle bundle = new Bundle();
        bundle.putString("href", "http://www.magazinauto.com/category/transport/");
        bundle.putString("title","Transport");
        fragmentMenuPages.setArguments(bundle);

    }
    else if (id == R.id.moto)  {

        FragmentMenuPages fragmentMenuPages = new FragmentMenuPages();
        FragmentManager manager = getSupportFragmentManager();
        manager.beginTransaction().replace(R.id.relativeLayoutZaFragment
                , fragmentMenuPages, fragmentMenuPages.getTag()).commit();

        TextView Title = (TextView) view.findViewById(R.id.actionbar_title);
        Title.setText("Moto");

        Bundle bundle = new Bundle();
        bundle.putString("href", "http://www.magazinauto.com/category/moto/");
        bundle.putString("title","Moto");
        fragmentMenuPages.setArguments(bundle);
    }

    else if (id == R.id.lifeStyle)  {

        FragmentMenuPages fragmentMenuPages = new FragmentMenuPages();
        FragmentManager manager = getSupportFragmentManager();
        manager.beginTransaction().replace(R.id.relativeLayoutZaFragment
                , fragmentMenuPages, fragmentMenuPages.getTag()).commit();

        TextView title = (TextView) view.findViewById(R.id.actionbar_title);
        title.setText("Lifestyle");

        Bundle bundle = new Bundle();
        bundle.putString("href", ("http://www.magazinauto.com/category/life-style/"));

        bundle.putString("title","Lifestyle");
        fragmentMenuPages.setArguments(bundle);
    }

    else if (id == R.id.daliznate) {
        FragmentDaLIPage fragmentMenuPages = new FragmentDaLIPage();
        FragmentManager manager = getSupportFragmentManager();
        manager.beginTransaction().replace(R.id.relativeLayoutZaFragment
                , fragmentMenuPages, fragmentMenuPages.getTag()).commit();

        TextView Title = (TextView) view.findViewById(R.id.actionbar_title);
        Title.setText("Da li znate?");


        Bundle bundle = new Bundle();
        bundle.putString("href", "http://www.magazinauto.com/category/da-li-znate/");
        bundle.putString("title","Da li znate?");
        fragmentMenuPages.setArguments(bundle);
    }

    else if (id == R.id.redakcija)  {
        RedakcijaFragment redakcijaFragment = new RedakcijaFragment();
        FragmentManager manager = getSupportFragmentManager();
        manager.beginTransaction().replace(R.id.relativeLayoutZaFragment
                ,redakcijaFragment, redakcijaFragment.getTag()).commit();

        TextView Title = (TextView) view.findViewById(R.id.actionbar_title);

        Title.setText("Redakcija");

    }

    else if (id == R.id.kontakt)  {
        KontaktFragment kontaktFragment = new KontaktFragment();

        FragmentManager manager = getSupportFragmentManager();

        manager.beginTransaction().replace(R.id.relativeLayoutZaFragment
                ,kontaktFragment, kontaktFragment.getTag()).commit();

        TextView Title = (TextView) view.findViewById(R.id.actionbar_title);
        Title.setText("Kontakt");
    }

    else {
        return super.onOptionsItemSelected(item);
    }

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawer.closeDrawer(GravityCompat.START);
    return true;
}

}

4 个答案:

答案 0 :(得分:1)

您需要覆盖此方法

 @Override
public void onBackPressed()
{
  if(getFragmentManager().getBackStackEntryCount() > 0)
      getFragmentManager().popBackStack();
  else
    super.onBackPressed(); 
}

并且不要忘记在片段事务上调用addToBackStack(null)

答案 1 :(得分:0)

覆盖片段活动中的onBackPressed()方法,并使用片段管理器弹出当前片段。

答案 2 :(得分:0)

查看getFragmentManager().popBackStack()方法(有几种方法可供选择)

奖金:你也可以使用这个技巧;)

getFragmentManager().popBackStackImmediate(); 

答案 3 :(得分:0)

这不能直接回答您的问题,因为不确定您要在这里实现什么。因此,在下面添加一段代码,可能会帮助您解决该问题(也许)。

在进行事务处理时,可以使用以下方法调用将片段设置为主片段:

fragmentTransaction.setPrimaryNavigationFragment(yourFragment);