1

Currently Used Google Navigation Drawer, having some problem when user backpress.

  1. When user backpress, they didn't update menu Item
  2. Example When I click the App. The sequence are A>B>C>B>C ,if I backpress I wanted to be C>B>A. How should I code this way out ?

My code

public class MainActivity extends AppCompatActivity {

    private String appTitle;
    private Toolbar toolbar;
    private NavigationView navigationView;
    private DrawerLayout drawerLayout;
    private TextView toolbarTitle;
    private Fragment fragment;
    private FragmentManager fragmentManager;
    private Title title;
    private MenuItem menuItem2;



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

        //Actionbar
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        toolbarTitle = (TextView) toolbar.findViewById(R.id.toolbar_title);
        toolbarTitle.setTextColor(Color.parseColor("#FFFFFF"));
        setTitle("");

        fragmentManager = getSupportFragmentManager();
        fragment= new HomeFragment();
        title = new Title(getApplicationContext());
        fragmentManager.beginTransaction().replace(R.id.content_frame,fragment , title.getStrHome()).commit();

        navigationView = (NavigationView) findViewById(R.id.navigation_view);
        navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(MenuItem menuItem) {
                menuItem2 = menuItem;

               menuItem.setChecked(true);
                drawerLayout.closeDrawers();

                if (menuItem.getItemId() == R.id.nav_home) {
                    fragment = new HomeFragment();
                    appTitle = title.getStrHome();
                }  else if ((menuItem.getItemId() == R.id.nav_direction)) {
                    fragment = new DirectionFragment();
                    appTitle = title.getStrDirection();
                } else if ((menuItem.getItemId() == R.id.nav_more)) {
                    fragment = new MoreFragment();
                    appTitle = title.getStrMore();
                } else if((menuItem.getItemId()==R.id.nav_directory)){
                    fragment = new DirectoryFragment();
                    appTitle = title.getStrDirectory();
                }
                else {
                    Toast.makeText(getApplicationContext(), "Error", Toast.LENGTH_SHORT).show();
                }
                replaceFragment(fragment,appTitle);

                return true;
            }
        });

        drawerLayout = (DrawerLayout) findViewById(R.id.drawer);
        ActionBarDrawerToggle actionBarDrawerToggle =
     new ActionBarDrawerToggle
         (this,drawerLayout,toolbar,R.string.drawer_open,R.string.drawer_close){
            @Override
            public void onDrawerClosed(View drawerView) {
                super.onDrawerClosed(drawerView);
            }

            @Override
            public void onDrawerOpened(View drawerView) {
                super.onDrawerOpened(drawerView);
            }
        };


        drawerLayout.setDrawerListener(actionBarDrawerToggle);
        actionBarDrawerToggle.syncState();

    }



    public void onBackPressed() {
        if(drawerLayout.isDrawerOpen(GravityCompat.START)){
            drawerLayout.closeDrawer(GravityCompat.START);
        }
        else {
            if (fragmentManager.getBackStackEntryCount() > 0 ){
                fragmentManager.popBackStack();
                toolbarTitle.setText(appTitle);
                menuItem2.setChecked(true);


            } else {
                finish();
            }
        }

    }
    public void replaceFragment(Fragment fragment, String tag){
        toolbarTitle.setText(tag);
        FragmentTransaction ft =  fragmentManager.beginTransaction().replace(R.id.content_frame, fragment, tag);
        ft.addToBackStack(tag);

        ft.commit();
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }




}
4

2 回答 2

0

研究 Android Activity 启动模式。

是可用的明确解释。看看吧。

于 2015-11-04T08:56:10.133 回答
0

正如关于 Android任务和后台堆栈的官方页面所述,您可以查看如何使用FLAG_ACTIVITY_CLEAR_TOPIntent 标志完成您想要的行为。这张照片很好地说明了这一点。

您的常规后退按钮如下:

在此处输入图像描述

当你指定这个标志时,你会得到你需要的行为


编辑:

官方文档指出:

FLAG_ACTIVITY_CLEAR_TOP

如果正在启动的活动已经在当前任务中运行,则不会启动该活动的新实例,而是销毁其上的所有其他活动

这意味着,您的不同活动不会重复,并且您将拥有一个干净的 Back Stack。

在你的例子A>B>C>B>C中是不可能的。取而代之的是A>B>C,因为BC已经存在于堆栈中,所以它们不会被添加,而是被替换。

于 2015-11-03T14:36:39.093 回答