原谅我的英语:(
我遇到“NavigationDrawer”的问题。我有没有特殊活动的碎片“Home”,“Import,Gallery和SlideShow”,所有活动都运行得很好。 但是,如果我单击导入(HOME> IMPORT),活动IMPORT将打开,但如果我单击(IMPORT> GALLERY / SLIDESHOW / HOME),导入活动将保持打开状态。 我必须按后退按钮才能进入HOME,点击其他活动
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
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);
}
@Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.home, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
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.
//here is the main place where we need to work on.
int id=item.getItemId();
switch (id){
case R.id.nav_home:
Intent h= new Intent(Home.this,Home.class);
startActivity(h);
break;
case R.id.nav_import:
Intent i= new Intent(Home.this,Import.class);
startActivity(i);
break;
case R.id.nav_gallery:
Intent g= new Intent(Home.this,Gallery.class);
startActivity(g);
break;
case R.id.nav_slideshow:
Intent s= new Intent(Home.this,Slideshow.class);
startActivity(s);
break;
// oh nightmare
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
进口活动
public class Import extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
DrawerLayout drawer;
NavigationView navigationView;
Toolbar toolbar=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_import);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Button Button9 = (Button) findViewById(R.id.Button9);
Button Button11 = (Button) findViewById(R.id.Button11);
//We dont need this.
drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
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);
Button9.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent it = new Intent(Import.this, PHP5.class);
startActivity(it);
}
});
Button11.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent it = new Intent(Import.this, PHP7.class);
startActivity(it);
}
});
}
@Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.home, 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.
//here is the main place where we need to work on.
int id=item.getItemId();
switch (id){
case R.id.nav_home:
Intent h= new Intent(Import.this,Home.class);
startActivity(h);
break;
case R.id.nav_import:
Intent i= new Intent(Import.this,Import.class);
startActivity(i);
break;
case R.id.nav_gallery:
Intent g= new Intent(Import.this,Gallery.class);
startActivity(g);
break;
case R.id.nav_slideshow:
Intent s= new Intent(Import.this,Slideshow.class);
startActivity(s);
break;
// after this lets start copying the above.
// FOLLOW MEEEEE>>>
//copy this now.
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
}
答案 0 :(得分:0)
也许您忘记在finish()
中致电IMPORT Activity
了?如果您在Intent
之后调用另一个Activity
,则当前活动将关闭,您的Home Activity
将再次显示。
答案 1 :(得分:0)
您需要从堆栈中删除所有先前的活动,可以通过意图
中的标记来完成 Intent it = new Intent(Import.this, OtherAct.class);
it.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(it);
否则,密钥的组合可能适合您:
it.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
检查有关其工作原理的文档: https://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_CLEAR_TOP https://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_NEW_TASK https://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_CLEAR_TASK