导航抽屉调用另一个活动 - 在menuItem上关闭抽屉单击它项目与当前活动匹配

时间:2015-12-18 16:34:54

标签: android android-activity navigation-drawer navigationview

我使用这个答案创建了一个NavigationDrawer类:Same Navigation Drawer in different Activities

我的问题是当我点击当前活动的导航抽屉项时,活动会重新加载,如果点击的项目与当前活动相同,我只需要它关闭navigationDrawer。

This is my Drawer Activity

    import android.content.Intent;
    import android.support.design.widget.NavigationView;
    import android.support.v4.view.GravityCompat;
    import android.support.v4.widget.DrawerLayout;
    import android.support.v7.app.ActionBarDrawerToggle;
    import android.support.v7.app.AppCompatActivity;
    import android.support.v7.widget.Toolbar;
    import android.view.MenuItem;

    import com.rafael.test.LoginActivity;
    import com.rafael.test.HomeActivity;
    import com.rafael.test.R;
    import com.rafael.test.TokenActivity;

    public class DrawerBase extends AppCompatActivity
    implements NavigationView.OnNavigationItemSelectedListener   {

        public DrawerLayout drawer;
public NavigationView navigationView;

protected void onCreateDrawer() {


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


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

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


    // Creates the NavigationView and set Listener
    navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);


}


@Override
public void onBackPressed() {
    if (drawer.isDrawerOpen(GravityCompat.START)) {
        drawer.closeDrawer(GravityCompat.START);
    } else {
        super.onBackPressed();
    }
}

// Here is where I'm handling the menu item click
@Override
public boolean onNavigationItemSelected(MenuItem item) {


    int id = item.getItemId();

    if (id == R.id.home) {
        Intent i = new Intent(this, HomeActivity.class);
        startActivity(i);

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

        Intent i = new Intent(this,FeedActivity.class);
        startActivity(i);

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

        Intent i = new Intent(this, TokenActivity.class);
        startActivity(i);

    }

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

}

2 个答案:

答案 0 :(得分:0)

有几种方法可以做到这一点。

您可以创建一个条件来简单地检查当前活动,该活动可以存储在BaseActivity中的静态变量中。

或者,如果您不想重新创建位于顶部的活动,您可以将singleTop launchMode添加到清单中,如下所示:

<activity
        android:name=".YourActivity"
        android:launchMode="singleTop"
</activity>

另一个类似的替代方案是,如果您不想从导航抽屉重新创建活动,但想要在其他地方重新创建活动,则将singleTop标志添加到导航器意图中...

intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);

答案 1 :(得分:0)

得到答案,但出于某种原因,发布它的人将其删除。

在我的OnNavigationItemSelecte()方法中,我更改了测试以调用Intent

来自:

int id = item.getItemId();

    if (id == R.id.home) {
        Intent i = new Intent(this, HomeActivity.class);
        startActivity(i);

到这个

if (id == R.id.home) {
    // This code gets the class name to use in the test
    if(this.getClass().getSimpleName().equals("HomeActivity")){
       drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    }else{
       Intent i = new Intent(this, HomeActivity.class);
        startActivity(i);
    }