Answer the question
In order to leave comments, you need to log in
Is there a problem when working with fragments dynamically?
Is there a problem when working with fragments dynamically?
main class:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar_actionbar);
setSupportActionBar(toolbar);
actionBar = getSupportActionBar();
actionBar.setHomeButtonEnabled(true);
actionBar.setDisplayHomeAsUpEnabled(false);
// вызываем первый фрагмент regist
fragment = RegistFragment.newInstance();
actionBar.setTitle(getResources().getString(R.string.regist_info));
selectItem(fragment);
getSupportFragmentManager().addOnBackStackChangedListener(new FragmentManager.OnBackStackChangedListener() {
public void onBackStackChanged() {
int backCount = getSupportFragmentManager().getBackStackEntryCount();
if (backCount > 1){
actionBar.setDisplayHomeAsUpEnabled(true);
} else {
actionBar.setDisplayHomeAsUpEnabled(false);
}
}
});
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
onBackPressed();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
@Override
public void onBackPressed() {
if (getFragmentManager().getBackStackEntryCount() > 0) {
getFragmentManager().popBackStack();
} else {
finish();
}
}
public void changeFrag(Fragment fragment, String titleFrag) {
actionBar.setTitle(titleFrag);
selectItem(fragment);
}
private void selectItem(Fragment fragment) {
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.setCustomAnimations(R.anim.fragment_slide_left_enter, R.anim.fragment_slide_left_exit,
R.anim.fragment_slide_right_enter, R.anim.fragment_slide_right_exit);
fragmentTransaction.addToBackStack(fragment.getClass().getName());
fragmentTransaction.replace(R.id.content_frame, fragment).commit();
}
Answer the question
In order to leave comments, you need to log in
Try add instead of replace:
fragmentTransaction.add(R.id.content_frame, fragment).commit();
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question