我正在尝试通过在按下单选按钮时切换它们来管理单个活动上的片段。在每次替换操作时,我调用addToBackStack()
方法,但当我按下后退按钮时,它会关闭活动而不是前一个片段。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_flow);
radioGroup = (RadioGroup) findViewById(R.id.rdoGrp);
rdBtnHome = (RadioButton) findViewById(R.id.rdBtnHome);
rdBtnSearch = (RadioButton) findViewById(R.id.rdBtnSearch);
rdBtnShoot = (RadioButton) findViewById(R.id.rdBtnShoot);
rdBtnUser = (RadioButton) findViewById(R.id.rdBtnUser);
rdBtnAlarm = (RadioButton) findViewById(R.id.rdBtnAlarm);
homeFragment = new HomeFragment();
searchFragment = new SearchFragment();
userFragment = new UserFragment();
shootFragment = new ShootFragment();
alarmFragment = new AlarmFragment();
getSupportActionBar().setDisplayOptions(android.support.v7.app.ActionBar.DISPLAY_SHOW_CUSTOM);
getSupportActionBar().setCustomView(R.layout.actionbar);
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.frameLayoutMain, homeFragment);
ft.addToBackStack(null);
ft.commit();
rdBtnHome.setChecked(true);
// Creating once image loader configuration
createImageLoaderConfiguration();
// Defining footer radiogroup including radiogroup callback listener
defineFooterRadioGroup();
Log.d("Logged User", Me.meGetInstance().toString());
}
private void defineFooterRadioGroup() {
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
switch (checkedId) {
case R.id.rdBtnHome:
ft.replace(R.id.frameLayoutMain, homeFragment);
ft.addToBackStack(null);
ft.commit();
break;
case R.id.rdBtnAlarm:
ft.replace(R.id.frameLayoutMain, alarmFragment);
ft.addToBackStack(null);
ft.commit();
break;
case R.id.rdBtnSearch:
ft.replace(R.id.frameLayoutMain, searchFragment);
ft.addToBackStack(null);
ft.commit();
break;
case R.id.rdBtnShoot:
ft.replace(R.id.frameLayoutMain, shootFragment);
ft.addToBackStack(null);
ft.commit();
break;
case R.id.rdBtnUser:
ft.replace(R.id.frameLayoutMain, userFragment);
ft.addToBackStack(null);
ft.commit();
break;
}
}
});
}
答案 0 :(得分:0)
答案 1 :(得分:0)
将popBackStack()
添加到您的Activity Back Pressed Listener,就像这样..
if (getSupportFragmentManager().getBackStackEntryCount() > 0) {
getSupportFragmentManager().popBackStack();
}
答案 2 :(得分:0)
这是一种在片段中删除包含代码的片段的方法。
public void removeMe() {
FragmentTransaction ft = getFragmentManager().beginTransaction();
Fragment prevHistory = getFragmentManager()
.findFragmentByTag("LOG_IN");
if (prevHistory != null) {
ft.remove(prevHistory);
}
ft.commit();
}
您需要调用removeMe方法。所以在后退按钮上添加一个关键的监听器。
view.setOnKeyListener(new View.OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if(keyCode==KeyEvent.KEYCODE_BACK){
removeMe();
}
return true;
}
});
视图可以是任何东西。如果您想要全局解决方案,视图就是片段的视图。
要使此方法有效,您需要在添加或替换片段时向片段事务添加标记。
Fragment newFragment = new Fragment();
ft.replace(R.id.fragment_container, newFragment, "The fragment tag goes here");