在导航抽屉中切换活动和片段

时间:2018-06-17 03:56:41

标签: android android-fragments navigation-drawer

我不是Android的专家,但对片段非常感兴趣。非常感谢你的帮助!

我在我的应用中实现了导航抽屉。有一个基本抽屉活动和一些用户可以从抽屉菜单切换的片段。如果我想移动到另一个片段没有问题,但是当我使用intent开始一个新活动时它正在工作。如果你查看我下面的代码,你会看到我使用两个单独的菜单:一个用于ActionBar图标(购物车和搜索),其中意图完美地工作,另一个用于Nav抽屉在片段之间跳转。有没有办法把它合二为一?基本上,我需要从导航抽屉转移到配置文件并注销活动。提前感谢您的建议!

The screen is here

Java文件

public class BaseDrawerActivity extends AppCompatActivity  {

    private DrawerLayout drawerLayout;
    private NavigationView nvDrawer;
    private ActionBarDrawerToggle actionBarDrawerToggle;

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

        drawerLayout = findViewById(R.id.drawer);
        actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, R.string.open, R.string.close);
        drawerLayout.addDrawerListener(actionBarDrawerToggle);
        actionBarDrawerToggle.syncState();

        nvDrawer = findViewById(R.id.nvgView);
        // Setup drawer view
        setupDrawerContent(nvDrawer);

        getSupportActionBar().setDisplayShowHomeEnabled(true);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

//Menu created for cart and search icons
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main_menu, menu);
        return super.onCreateOptionsMenu(menu);
        //return true;
    }

//This method is designed only for cart and search icons
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();

        if(actionBarDrawerToggle.onOptionsItemSelected(item)){
            return true;
        } else if (id == R.id.cart) {
            Intent intent = new Intent(getApplicationContext(), CartActivity.class);
            startActivity(intent);
        } else if (id == R.id.search) {
Intent intent = new Intent(getApplicationContext(), SearchActivity.class);
            startActivity(intent);
        }
        return super.onOptionsItemSelected(item);
    }

//This method is to set up the drawer content/menu
    private void setupDrawerContent(NavigationView navigationView) {
        navigationView.setNavigationItemSelectedListener(
                new NavigationView.OnNavigationItemSelectedListener() {
                    @Override
                    public boolean onNavigationItemSelected(MenuItem menuItem) {
                        selectDrawerItem(menuItem);
                        return true;
                    }
                });
    }

    public void selectDrawerItem(MenuItem menuItem) {
        // Here we create a new fragment and specify the fragment to show based on nav item clicked
        Fragment fragment = null;
        Class fragmentClass;
        switch(menuItem.getItemId()) {
            case R.id.nav_home:
                fragmentClass = HomeFragment.class;
                break;
            case  R.id.nav_cat:
                fragmentClass = CatalogFragment.class;
                break;
            case  R.id.nav_logout:
                fragmentClass = CatalogFragment.class;
                break;

            case R.id.nav_login:
                //what should I add here to be able to move to another activity (not a fragment). It doesn't work when I use intents
            case R.id.nav_profile:
                //need to move to another activity
            default:
                fragmentClass = HomeFragment.class;
        }

        try {
            fragment = (Fragment) fragmentClass.newInstance();
        } catch (Exception e) {
            e.printStackTrace();
        }

        // Insert the fragment by replacing any existing fragment
        FragmentManager fragmentManager = getSupportFragmentManager();
        fragmentManager.beginTransaction().replace(R.id.flcontent, fragment).addToBackStack("back tag").commit();

        // Highlight the selected item has been done by NavigationView
        menuItem.setChecked(true);
        // Set action bar title
        setTitle(menuItem.getTitle());
        // Close the navigation drawer
        drawerLayout.closeDrawers();
    }

}

1 个答案:

答案 0 :(得分:0)

您是否尝试过对optionItems执行的操作,并在启动Activity时阻止与该片段相关的代码执行:

public class BaseDrawerActivity extends AppCompatActivity  {

private DrawerLayout drawerLayout;
private NavigationView nvDrawer;
private ActionBarDrawerToggle actionBarDrawerToggle;

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

    drawerLayout = findViewById(R.id.drawer);
    actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, R.string.open, R.string.close);
    drawerLayout.addDrawerListener(actionBarDrawerToggle);
    actionBarDrawerToggle.syncState();

    nvDrawer = findViewById(R.id.nvgView);
    // Setup drawer view
    setupDrawerContent(nvDrawer);

    getSupportActionBar().setDisplayShowHomeEnabled(true);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

//Menu created for cart and search icons
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main_menu, menu);
    return super.onCreateOptionsMenu(menu);
    //return true;
}

//This method is design only for cart and search icons
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();

    if(actionBarDrawerToggle.onOptionsItemSelected(item)){
        return true;
    } else if (id == R.id.cart) {
        Intent intent = new Intent(getApplicationContext(), CartActivity.class);
        startActivity(intent);
    } else if (id == R.id.search) {
Intent intent = new Intent(getApplicationContext(), SearchActivity.class);
        startActivity(intent);
    }
    return super.onOptionsItemSelected(item);
}

//This method is to set up the drawer content/menu
private void setupDrawerContent(NavigationView navigationView) {
    navigationView.setNavigationItemSelectedListener(
            new NavigationView.OnNavigationItemSelectedListener() {
                @Override
                public boolean onNavigationItemSelected(MenuItem menuItem) {
                    selectDrawerItem(menuItem);
                    return true;
                }
            });
}

public void selectDrawerItem(MenuItem menuItem) {
    // Here we create a new fragment and specify the fragment to show based on nav item clicked
    Fragment fragment = null;
    Class fragmentClass;
    switch(menuItem.getItemId()) {
        case R.id.nav_home:
            fragmentClass = HomeFragment.class;
            break;
        case  R.id.nav_cat:
            fragmentClass = CatalogFragment.class;
            break;
        case  R.id.nav_logout:
            fragmentClass = CatalogFragment.class;
            break;

        case R.id.nav_login:
             Intent intent = new Intent(getApplicationContext(), OtherActivity.class);
            startActivity(intent);
            break;
        case R.id.nav_profile:
             Intent intent = new Intent(getApplicationContext(), ProfileActivity.class);
             startActivity(intent);
             break;
        default:
            fragmentClass = HomeFragment.class;
    }
    //Code related to fragment should not execute when choosing to start an Activity     
    if(fragmentClass != null){
        try {
            fragment = (Fragment) fragmentClass.newInstance();
        } catch (Exception e) {
            e.printStackTrace();
        }

        // Insert the fragment by replacing any existing fragment
        FragmentManager fragmentManager = getSupportFragmentManager();
    fragmentManager.beginTransaction().replace(R.id.flcontent, fragment).addToBackStack("back tag").commit();

        // Highlight the selected item has been done by NavigationView
    }
    menuItem.setChecked(true);
    // Set action bar title
    setTitle(menuItem.getTitle());
    // Close the navigation drawer
    drawerLayout.closeDrawers();
    }

}