这是我的MainActivity.java类,我想使用backstack在我的片段和活动之间导航。我使用了addToBackStack(null)和popbackStack0,仍然无法正常工作。
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener, OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
//ImageView imageView=(ImageView) findViewById(R.id.mainpage);
//imageView.setImageResource(R.drawable.banner);
Button button1 = (Button) findViewById(R.id.cropbutton);
button1.setOnClickListener(this);
Button button2 = (Button) findViewById(R.id.diseasebutton);
button2.setOnClickListener(this);
Button button3 = (Button) findViewById(R.id.pestbutton);
button3.setOnClickListener(this);
Button button5 = (Button) findViewById(R.id.expertbutton);
button5.setOnClickListener(this);
DrawerLayout 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.addDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}
//boolean doubleBackToExitPressedOnce = false;
@Override
public void 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.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();
Button button1 = (Button) findViewById(R.id.cropbutton);
Button button2 = (Button) findViewById(R.id.diseasebutton);
Button button3=(Button) findViewById(R.id.pestbutton);
Button button5 = (Button) findViewById(R.id.expertbutton);
if (id == R.id.nav_camera) {
Cropfragment cropfragment=new Cropfragment();
FragmentManager manager=getSupportFragmentManager();
manager.beginTransaction().replace(R.id.relative1,cropfragment,cropfragment.getTag()).addToBackStack(null).commit();
manager.popBackStack("relative1",0);
} else if (id == R.id.nav_gallery) {
BlankFragment blankFragment=new BlankFragment();
FragmentManager manager=getSupportFragmentManager();
manager.beginTransaction().replace(R.id.relative1,blankFragment,blankFragment.getTag()).addToBackStack(null).commit();
manager.popBackStack("relative1",0);
} else if (id == R.id.nav_home) {
Intent appInfo = new Intent(MainActivity.this, MainActivity.class);
startActivity(appInfo);
} else if (id == R.id.nav_pesti) {
pest blankFragment=new pest();
FragmentManager manager=getSupportFragmentManager();
manager.beginTransaction().replace(R.id.relative1,blankFragment,blankFragment.getTag()).addToBackStack(null).commit();
manager.popBackStack("relative1",0);
}
else if (id == R.id.nav_expert) {
Intent appInfo = new Intent(MainActivity.this, detail_expert.class);
startActivity(appInfo);
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
@Override
public void onClick(View v) {
Button button1 = (Button) findViewById(R.id.cropbutton);
Button button2 = (Button) findViewById(R.id.diseasebutton);
Button button3=(Button) findViewById(R.id.pestbutton);
Button button5 = (Button) findViewById(R.id.expertbutton);
switch (v.getId()) {
case R.id.cropbutton:
Cropfragment cropfragment = new Cropfragment();
FragmentManager manager = getSupportFragmentManager();
manager.beginTransaction().replace(R.id.relative1, cropfragment, cropfragment.getTag()).addToBackStack(null).commit();
manager.popBackStack("relative1", 0);
break;
case R.id.diseasebutton:
BlankFragment blankFragment= new BlankFragment();
FragmentManager manager1 = getSupportFragmentManager();
manager1.beginTransaction().replace(R.id.relative1,blankFragment,blankFragment.getTag()).addToBackStack(null).commit();
manager1.popBackStack("relative1",0);
break;
case R.id.pestbutton:
pest pestFragment= new pest();
FragmentManager manager2 = getSupportFragmentManager();
manager2.beginTransaction().replace(R.id.relative1,pestFragment,pestFragment.getTag()).addToBackStack(null).commit();
manager2.popBackStack("relative1",0);
break;
case R.id.expertbutton:
Intent appInfo1 = new Intent(MainActivity.this, detail_expert.class);
startActivity(appInfo1);
break;
default:
break;
}
}
}
答案 0 :(得分:0)
查看代码我不确定你想要如何导航,但是通过代码,我相信你正在添加带有null的addbackstack并使用标记删除,所以添加标记而不是null然后使用标记{{1或者,只需要执行popbackstack()或者如果你想对manager.popBackStack("relative1",0);
做同样的事情,那么你需要覆盖`onBackPress(),获取片段的大小并在此活动中逐个弹出。