我有activity1带我到activity2,让我去片段。我想从片段回到activity2
public void onBackPressed() {
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
答案 0 :(得分:1)
{{1}}
答案 1 :(得分:0)
得到反向事件并调用
getactivity().getsupportfragmentmanager().popbackstack();
答案 2 :(得分:0)
从activity2添加片段时,将其添加到backstack,就像这样
getSupportFragmentManager().beginTransaction()
.replace(R.id.frame_layout, new SampleFragment())
.addToBackStack(SampleFragment.class.getSimpleName())
.commit();
onBackPressed检查片段是否存在于backstack中并弹出
@Override
public void onBackPressed() {
if(getSupportFragmentManager().getBackStackEntryCount()>0){
Fragment fragment=getSupportFragmentManager().findFragmentById(R.id.YOUR_FRAME_LAYOUT_ID);
if(fragment!=null && fragment.getClass().getSimpleName().equalsIgnoreCase(SampleFragment.class.getSimpleName())){
getSupportFragmentManager().popBackStackImmediate();
}
}else {
super.onBackPressed();
}
}
答案 3 :(得分:-1)
将该片段事务添加到backstack。