【问题标题】:implementing Activities instead of fragments in Navigation drawer在导航抽屉中实现活动而不是片段
【发布时间】:2016-09-19 21:58:44
【问题描述】:

我一直在用 activites 制作这个导航抽屉。我的项目中有几个活动,我想在使用导航抽屉之间切换。 到目前为止,我已经做到了这一点

  1. 创建导航抽屉并编写一些活动代码。

  2. 在所有活动中显示相同的导航抽屉。

  3. 在应用启动时启动默认活动。

但现在我的问题是,当我第一次打开我的应用程序时,它会加载我想要的活动,我可以看到这个导航抽屉。当我切换活动并按下后退按钮时,我得到这个带有导航抽屉的空白活动。我知道它有点像初学者级别的问题。但请如果有人可以建议编辑。这是我的代码。

public class MainActivity extends AppCompatActivity
    implements NavigationView.OnNavigationItemSelectedListener {

NavigationView navigationView = null;
Toolbar toolbar = null;
DrawerLayout drawer;
FrameLayout frameLayout;

private static boolean isLaunch = true;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    frameLayout = (FrameLayout) findViewById(R.id.contant_frame);
    drawer = (DrawerLayout) findViewById(R.id.drawer_layout);

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

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });

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

    navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);

    if (isLaunch) {
        isLaunch = false;
        startActivity(new Intent (this,MyAct.class));
    }

}

@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);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
    // Handle navigation view item clicks here.
    int id = item.getItemId();

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

        startActivity(new Intent(this,About.class));

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

        startActivity(new Intent(this,About.class));

    }

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

}

我有同样的应用程序在简单的列表视图类型导航抽屉中完美运行。但现在我正在尝试实现花哨的材料设计。

【问题讨论】:

  • isLaunch 是干什么用的?

标签: android android-studio android-activity navigation


【解决方案1】:

尝试覆盖onBackButtonPressed 方法。

【讨论】:

    【解决方案2】:

    清除/删除 MyAct 和 About 类中的 onBackpressed 方法将带回最后一个视图,其中包含已填充的任何内容。

    但是如果你想打开其他视图或者想在按下返回键时开始新的活动,你可以在这个onBackPressed方法中添加你的代码

    希望对您有所帮助。干杯!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多