I have two Fragments - FragmentA and FragmentB.
I try to show FragmentB from FragmentA.
FragmentB fragmentB = new FragmentB();
MainActivity activity = (MainActivity) getActivity();
int fragmentContainer = activity.getFragmentContainer();
FragmentTransaction fragmentTransaction = activity.getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(fragmentContainer, fragmentB);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
MainActivity
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
fragmentA = new FragmentA(getSupportFragmentManager());
//...
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.add(FRAGMENT_CONTAINER,fragmentA);
fragmentTransaction.commit();
}
FragmentB is showed, but when i click on back button i have a next situation - FragmentB was dismissed, but FragmentA not showed.
Why this happens and what i need to edit to get a correct result?
getActivity().getChildFragmentManager().beginTransaction();
use this it may be works inside fragmentB
Thanks to New Developer for provided link.
All that we need it's just add a
#Override
public void onBackPressed(){
if (getSupportFragmentManager().getBackStackEntryCount() == 1){
finish();
}
else {
super.onBackPressed();
}
}
and
private void replaceFragment (Fragment fragment){
String backStateName = fragment.getClass().getName();
String fragmentTag = backStateName;
FragmentManager manager = getSupportFragmentManager();
boolean fragmentPopped = manager.popBackStackImmediate (backStateName, 0);
if (!fragmentPopped && manager.findFragmentByTag(fragmentTag) == null){ //fragment not in back stack, create it.
FragmentTransaction ft = manager.beginTransaction();
ft.replace(R.id.content_frame, fragment, fragmentTag);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
ft.addToBackStack(backStateName);
ft.commit();
}
}
and manage fragments in activity.
So if we need a swap fragments just call :
replaceFragment(new MySwappingFragment());
Related
I have a Android app with one activity and two fragments. First fragment (MainFragment) show a list of "items" and second (DetailsFragment) display a item's details (very basic).
On the normal flow, the activity start and show first fragment and the second is shown when a item is clicked.
But the second fragment can be shown directly throught click on a notification (by putting extra arguments to the activity).
String id = getIntent().getStringExtra("id");
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
if (id != null) {
fragment = new DetailsFragment();
Bundle args = new Bundle();
args.putString("id", id);
fragment.setArguments(args);
} else {
fragment = new MainFragment();
}
ft.replace(R.id.main, fragment);
ft.commit();
In the second case, the problem is : How to open MainFragment when clicking back from DetailsFragment ? Actually the app finish because only the second fragment has been created.
First way: Its very simple. You just need to use addToBackStack(null). It will save your desired fragment and when you use backpressed it will open that one.
Do something like this:
if (id != null) {
fragment = new DetailsFragment();
Bundle args = new Bundle();
args.putString("id", id);
fragment.setArguments(args);
ft.replace(R.id.main, fragment);
ft.commit();
} else {
fragment = new MainFragment();
ft.replace(R.id.main, fragment).addToBackStack(null);
ft.commit();
}
Second way: in your DetailsFragment's onResume() method write this code.
#Override
public void onResume() {
super.onResume();
getView().setFocusableInTouchMode(true);
getView().requestFocus();
getView().setOnKeyListener(new View.OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_UP && keyCode == KeyEvent.KEYCODE_BACK) {
fragment = new MainFragment();
fragmentManager = getFragmentManager();
fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.main, fragment);
fragmentTransaction.commit();
return true;
}
return false;
}
});
}
Add the main fragment in the onCreate() of the activity and override the onBackPressed() in the main activity
FragmentTransaction mFragmentTransaction = fragmentManager.beginTransaction();
mFragmentTransaction.replace(R.id.frame_container_recharege, fragment);
mFragmentTransaction.commit();
#Override
public void onBackPressed() {
FragmentManager fragmentManager = getSupportFragmentManager();
if (fragmentManager.getBackStackEntryCount() > 1) {
fragmentManager.popBackStack();
} else {
super.onBackPressed();
}
}
This is very common situation in Android, and simply saying you can just add it to backstack. So when user presses back button he will see the previous fragment(MainFragment).
getFragmentManager()
.beginTransaction()
.addToBackStack(yourFragment.getClass().getSimpleName())
.replace(R.example.container, yourFragment)
.commit();
Have a read official Android documentation about the back stack
UPDATE:
If you open DetailsFragment directly(without opening MainFragment) then you should check the back stack, if it is empty then open MainFragment manually. Here is the full code:
if (getSupportFragmentManager().getBackStackEntryCount() == 0) // empty back stack{
getFragmentManager()
.beginTransaction()
.replace(R.example.container, MainFragment)
.commit();
}
You can use something like this on your back press or back button clicked
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
fragment = new MainFragment();
ft.replace(R.id.fragment_container, frag);
ft.commit();
add fragment like this
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction ft = manager.beginTransaction();
ft.replace(containerId, fragment);
ft.addToBackStack(backStateName);
ft.commit();
and in your activity backpress put this
getSupportFragmentManager().popBackStack();
I have some fragments which will be replaced by following method. I think there's something wrong with my code because I want to prevent from adding multiple times a fragment into the back stack. If I click on fragment B twice, all instances will be added to the back stack and pressing back button will be passed through the two created instances.
public void replaceFragment(Fragment fragment, boolean addToBackStack, boolean customAnimation) {
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
String tag = fragment.getClass().getSimpleName();
if (customAnimation) {
transaction.setCustomAnimations(R.anim.slide_in_bottom, R.anim.slide_out_bottom, R.anim.slide_in_bottom, R.anim.slide_out_bottom);
}
transaction.replace(R.id.fragment_container, fragment, tag);
// remove from back stack if exists
// always return false!
boolean f = manager.popBackStackImmediate(tag, 0);
if (addToBackStack) {
transaction.addToBackStack(tag);
}
transaction.commit();
}
Keep it simple and just add to the back stack if needed.
If the Fragment being added is the same class as the current Fragment, don't add to the back stack:
public void replaceFragment(Fragment frag) {
FragmentManager manager = getSupportFragmentManager();
if (manager != null){
FragmentTransaction t = manager.beginTransaction();
Fragment currentFrag = manager.findFragmentById(R.id.content_frame);
//Check if the new Fragment is the same
//If it is, don't add to the back stack
if (currentFrag != null && currentFrag.getClass().equals(frag.getClass())) {
t.replace(R.id.content_frame, frag).commit();
} else {
t.replace(R.id.content_frame, frag).addToBackStack(null).commit();
}
}
}
try this on your activity onBackPressed method :
#Override
public void onBackPressed() {
FragmentManager fm = getSupportFragmentManager();
if (fm.getBackStackEntryCount() > 0) {
if (fm.getBackStackEntryAt(fm.getBackStackEntryCount() - 1).getName().equals("your fragment tag")) {
fm.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
}
I have a fragment activity from where i can start my fragments which contain a viewpager. In my fragment activity I have added this piece of code.
fragment = new ItemPagerFragment();
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.content_frame, fragment);
transaction.addToBackStack(null);
transaction.commit();
Now when I press back button only blank screen appears and it does not lead me to my fragment activity.
What wrong i might be doing?
Use this in activity that holds fragment.
#Override
public void onBackPressed() {
if (fragmentManager.getBackStackEntryCount() > 1) {
fragmentManager.popBackStack();
} else
finish();
}
Try this way,
// Create new fragment and transaction
Fragment newFragment = new ItemPagerFragment();
// consider using Java coding conventions (upper first char class names!!!)
FragmentTransaction transaction = getFragmentManager().beginTransaction();
// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack
transaction.replace(R.id.content_frame, newFragment);
transaction.addToBackStack(null);
// Commit the transaction
transaction.commit();
Modify your code
#Override
public void onBackPressed() {
if(some condition) {
// do something
} else {
super.onBackPressed();
}
}
Please follow this link
Handling back button press Inside Fragments
You have to tell to the fragment manager that the back button was pressed. Override onBackPressed on your Activity
#Override
public void onBackPressed() {
if (getFragmentManager().getBackStackEntryCount() == 0) {
super.onBackPressed();
} else {
getFragmentManager().popBackStack();
}
}
private void changeFragment (Fragment fragment){
String backStackName = fragment.getClass().getName();
FragmentManager manager = getSupportFragmentManager();
boolean fragmentPopped = manager.popBackStackImmediate (backStackName, 0);
if (!fragmentPopped){ //fragment not in back stack, create it.
FragmentTransaction ft = manager.beginTransaction();
ft.replace(R.id.content_frame, fragment);
ft.addToBackStack(backStackName );
ft.commit();
}
}
While launching fragment assign a TAG to each fragment.When you want to back press check the existence of an fragment using assigned TAG name and then do corresponding action.for example:
private final static String TAG_FRAGMENT = "TAG_FRAGMENT";
//when want to add fragment to view
private void showFragment() {
final Myfragment fragment = new MyFragment();
final FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.fragment, fragment, TAG_FRAGMENT);
transaction.addToBackStack(null);
transaction.commit();
}
//in activity back pressed check the existence of fragment by TAG
#Override
public void onBackPressed() {
final Myfragment fragment = (Myfragment) getSupportFragmentManager().findFragmentByTag(TAG_FRAGMENT);
if (fragment.allowBackPressed()) { // and then you define a method allowBackPressed with the logic to allow back pressed or not
super.onBackPressed();
}
}
i have 2 fragments, when i replace the second fragment and press the back button at that time my first fragment shows blank,here is my code
Fragment fragment = new MovieDetails();
Bundle args = new Bundle();
args.putString("background", item.getBackg());
args.putString("thumb", item.getThumb());
args.putString("id", item.getCode());
fragment.setArguments(args);
String backStateName = fragment.getClass().getName();
boolean fragmentPopped = getActivity()
.getSupportFragmentManager().popBackStackImmediate(
backStateName, 0);
if (!fragmentPopped) { // fragment not in back stack, create it.
FragmentTransaction ft = getActivity()
.getSupportFragmentManager().beginTransaction();
ft.replace(R.id.content_frame, fragment);
ft.addToBackStack("TAG");
ft.commit();
}
getActivity().getSupportFragmentManager().beginTransaction()
.replace(R.id.content_frame, fragment).commit();
You can do this
private void showFragment() {
final Myfragment fragment = new MyFragment();
final FragmentTransaction transaction =
getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.fragment, fragment, TAG_FRAGMENT);
transaction.addToBackStack(null);
transaction.commit();
}
#Override
public void onBackPressed() {
final Myfragment fragment = (Myfragment)getSupportFragmentManager().findFragmentByTag(TAG_FRAGMENT);
if (fragment.allowBackPressed()) {
super.onBackPressed();
}
This probably isn't your problem, but it was mine: I'd over-ridden onSaveInstanceState() and forgotten to call through to super. This resulted in a blank fragment after pressing 'back', almost certainly because super.onSaveInstanceState() is where the fragment stack gets saved.
I have an Activity. This activity check if the user is already logged in or not. If the user already logged in the activity show the LoginFragment. If the user login from the LoginFragment its showin the MainFragment and from the MainFragment user can go to the SettingsFragment.
But if I press the back button on SettingsFragment, it loads a blank fragment.
I use this method for "fragment navigation":
public void showFragment(String fragmentName){
Fragment fragment = null;
boolean backstack = true;
if (fragmentName.equals(SysKeys.MAIN_FRAGMENT)){
fragment = new MainFragment();
} else if (fragmentName.equals(SysKeys.LOGIN_FRAGMENT)){
fragment = new LoginFragment();
backstack = false;
} else if (fragmentName.equals(SysKeys.SETTINGS_FRAGMENT)){
fragment = new SettingsFragment();
} else if (fragmentName.equals(SysKeys.CUSTOM_LOGIN_FRAGMENT)){
fragment = new CustomLoginFragment();
}
if (fragment != null){
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.setCustomAnimations(R.anim.slide_in_from_right, R.anim.slide_out_to_left, R.anim.slide_in_from_left, R.anim.slide_out_to_right);
fragmentTransaction.replace(R.id.main_layout, fragment, fragmentName);
if (backstack){
fragmentTransaction.addToBackStack(null);
}
fragmentTransaction.commit();
}
}
I don't want to backstack the LoginFragment, but the others i would like. What is wrong with my code or logic?
UPDATE:
Now it not show the LoginFragment if I press back button on the MainFragment, but sometimes it just reload the MainFragment if I press the back button.
Here is the code:
public void showFragment(String fragmentName){
Fragment fragment = null;
boolean backstack = true;
if (fragmentName.equals(SysKeys.MAIN_FRAGMENT)){
fragment = new MainFragment();
backstack = false;
} else if (fragmentName.equals(SysKeys.LOGIN_FRAGMENT)){
fragment = new LoginFragment();
backstack = false;
} else if (fragmentName.equals(SysKeys.SETTINGS_FRAGMENT)){
fragment = new SettingsFragment();
} else if (fragmentName.equals(SysKeys.CUSTOM_LOGIN_FRAGMENT)){
fragment = new CustomLoginFragment();
}
if (fragment != null){
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.setCustomAnimations(R.anim.slide_in_from_right, R.anim.slide_out_to_left, R.anim.slide_in_from_left, R.anim.slide_out_to_right);
fragmentTransaction.replace(R.id.main_layout, fragment, fragmentName);
if (backstack){
fragmentTransaction.addToBackStack(null);
}
fragmentTransaction.commit();
}
}
Because every time you show the fragment, you already the pop the previous backstack. Why must you do that?
Try to remove fragmentManager.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
Also set backstack = false when you create MainFragment, So it'll not add MainFragment to backstack and you will finhish activity when press back from MainFragment
Hope this helps.