我一直在使用活动的导航抽屉。我的项目中有几个活动,我想在导航抽屉之间切换。 到目前为止,我已经实现了这个目标
创建导航抽屉并编写一些活动代码。
在所有活动中显示相同的导航抽屉。
在应用启动时启动默认活动。
但现在我的问题是,当我第一次打开我的应用程序时,它会加载我想要的活动,我可以看到这个导航抽屉。当我切换活动并按下后退按钮时,我通过导航抽屉得到这个空白活动。我知道它有点初学者级别的问题。但是,如果有人可以建议编辑,请。这是我的代码。
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;
}
}
我这个应用程序在简单的listview类型导航抽屉中运行完美。但现在我正在尝试实施精美的材料设计。
答案 0 :(得分:0)
尝试覆盖onBackButtonPressed
方法。
答案 1 :(得分:0)
清除/删除MyAct和About类中的onBackpressed
方法将返回包含任何已填充内容的最后一个视图。
但是,如果要在按下后退键时打开其他视图或想要开始新活动,可以在此onBackPressed
方法中添加代码
希望它有所帮助。干杯!