9
我的应用程序的结构是这样的:确定堆栈中的片段空状态的Android
SplashActivity -> MainActivity -> (switching between many fragments)
我期待什么:从主要活动结束应用程序时,片段回栈计数为零。这里是我的尝试:
在SplashAcitivty
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getIntent().getBooleanExtra("exit", false)) {
finish();
}
}
在MainActivity:
@Override
public void onBackPressed() {
// I need to implement this method
if(backstackCount() == 0){
Intent intent = new Intent(this, SplashActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra(SplashActivity.EXIT_KEY, true);
startActivity(intent);
}else{
super.onBackPressed();
}
}
那么,请告诉我,我怎么能确定返回堆叠它的时候是空的?因为我使用了SlideMenu
库,所以我的所有片段都会多次切换,并且在切换时将它们添加到背堆栈中。看起来像这样:
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.content_frame, fragment)
.addToBackStack(null)
.commit();
你可以使用'getBackStackEntryCount' – dreamcoder
@dreamcoder谢谢,它的工作。我不相信我不知道这个。你能回答我的问题,然后我可以接受吗? – R4j