Reputation: 985
I am trying to manage Fragments on single activity by switching them when radio buttons are pressed. On each replace operation I am calling addToBackStack()
method but when I pressing back button it closes the activity instead going to previous fragment.
@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;
}
}
});
}
Upvotes: 0
Views: 1319
Reputation: 5696
Here is a way to remove a fragment with code inside the fragment.
public void removeMe() {
FragmentTransaction ft = getFragmentManager().beginTransaction();
Fragment prevHistory = getFragmentManager()
.findFragmentByTag("LOG_IN");
if (prevHistory != null) {
ft.remove(prevHistory);
}
ft.commit();
}
You need to call the removeMe method. So add a key listener to the back button.
view.setOnKeyListener(new View.OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if(keyCode==KeyEvent.KEYCODE_BACK){
removeMe();
}
return true;
}
});
The view can be anything. If you want a global solution, the view is the fragment's view.
To make this method work you need to add a tag to the fragment transaction when you add or replace a fragment.
Fragment newFragment = new Fragment();
ft.replace(R.id.fragment_container, newFragment, "The fragment tag goes here");
Upvotes: 0
Reputation: 2128
Added popBackStack()
to your Activity's BackPressed Listener like this..
if (getSupportFragmentManager().getBackStackEntryCount() > 0) {
getSupportFragmentManager().popBackStack();
}
Upvotes: 2
Reputation: 4425
Upvotes: 0