Popbackstack always returns previous call - android-fragments

I've got three Simple Fragments: FragmentA, FragmentB, FragmentC.
FragmentA -> FragmentB:
FragmentB fragment2 = new FragmentB ();
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.addToBackStack(null);
fragmentTransaction.replace(R.id.mainLayout, fragment2);
fragmentTransaction.commit();
FragmentB -> FragmentC:
FragmentC fC= new FragmentC ();
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.mainLayout, fC);
fragmentTransaction.addToBackStack("FragmentA");
fragmentTransaction.commit();
FragmentC -> FragmentA:
getActivity().getSupportFragmentManager().popBackStack("FragmentA",0);
The Issue is that I always return to FragmentB altough my aim is to return to FragmentA. What am I missing?

FragmentManager fmManager = getFragmentManager();
if (fmManager.getBackStackEntryCount() > 0) { fmManager.popBackStack(fmManager.getBackStackEntryAt(fmManager.getBackStackEntryCount()-2).getId(), fmManager.POP_BACK_STACK_INCLUSIVE); }
what this code does, it checks if there are stacked fragments. If yes, it gets the fragment at the first stake, which is fragmentA, and returns to this fragment. Whats important is that u are aware at which position the fragment is positioned!!

Related

add multiple fragment to cheesesquare demo

I am using an open source project(https://github.com/chrisbanes/cheesesquare) to develop my own application.
what I want is:
1.In the Home Fragment, there is a tablayout below to the toobar,and when the recycelview scroll, the Toolbar can hide, but the FloatingActionButton always stay;
2. In the Message Frgment, the is no Tab and on FloatingActionButton, only a simple blank Fragment with a Toolbar.
I try to do this in MainActivity:
private void setupDrawerContent(NavigationView navigationView) {
navigationView.setNavigationItemSelectedListener(
new NavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
menuItem.setChecked(true);
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
Fragment fragment = null;
int id = menuItem.getItemId();
if (id == R.id.nav_home) {
fragment = new HomeFragment();
} else if (id == R.id.nav_message) {
fragment = new MessageFragment();
} else if (id == R.id.nav_friends) {
fragment = new FriendsFragment();
} else if (id == R.id.nav_discussion) {
fragment = new DiscussionFragment();
}
ft.replace(R.id.viewpager, fragment);
ft.commit();
mDrawerLayout.closeDrawer(GravityCompat.START);
return true;
}
});
}
but this not work, because the tablayout stay both in HomeFragment and MessageFragment.
When I try to do change the tablayout to the layout xml of HomeFragment, I also meet some problem because the below code should write in MainActivity
tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
I feel puzzled, what should I do to achieve my goals?
I try to solve the problem like this, implement the basic functions.

Issue with fragmentmanager.popbackstack();

unable to solve the issue with Fragments back stack. When I try to pop, it throws errors :
Could not find class
'android.support.v4.app.FragmentTransitionCompat21$1', referenced from
method android.support.v4.app.FragmentTransitionCompat21.setEpicenter
Could not find class
'android.support.v4.app.FragmentTransitionCompat21$3', referenced from
method
android.support.v4.app.FragmentTransitionCompat21.setSharedElementEpicenter
and on backpressed again, it crashes.
My code :
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment_place, fragment, fragmentTag);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
Pop :
int stackCount = getSupportFragmentManager().getBackStackEntryCount();
if (stackCount > 0) {
getSupportFragmentManager().popBackStackImmediate();
}

How do I get rid of a fragment that is no longer being used

I am new to Android app programming and fragments.
I am transitioning between two fragments in my app with the use of two buttons. I noticed that hitting frgbutton1 and frgbutton2 (transitioning back and forth) creates a problem.
My first fragment is being overlapped by an exact copy of the first fragment.
My question is how do I destroy or remove the first fragment so that there is no overlapping copy later.
public void selectFrag(View view) {
Fragment fr1 = null;
Fragment fr2 = null;
FragmentManager fm = getFragmentManager();
FragmentTransaction fragmentTransaction = fm.beginTransaction();
if (view == findViewById(R.id.frgbutton1)) {
fr1 = new Fragment01();
fragmentTransaction.replace(R.id.fragment_placeholder, fr1);
}else if (view == findViewById(R.id.frgbutton2)) {
fr2 = new Fragment02();
fragmentTransaction.replace(R.id.fragment_placeholder, fr2);
}
fragmentTransaction.commit();
}
Try this:
public void selectFrag(View view) {
Fragment fr1 = null;
Fragment fr2 = null;
FragmentManager fm = getFragmentManager();
FragmentTransaction fragmentTransaction = fm.beginTransaction();
if (view.getId()== R.id.frgbutton2) {
fr1 = new Fragment01();
fragmentTransaction.replace(R.id.fragment_placeholder, fr1);
}else if (view.getId() == R.id.frgbutton1) {
fr2 = new Fragment02();
fragmentTransaction.replace(R.id.fragment_placeholder, fr2);
}
fragmentTransaction.commit();
}

android: edittext in fragment not showing value when returning from new fragment

When I return from the new Fragment to my old Fragment, the EditText setText() does not show anything!
From my FragmentActivity I create FragmentA. From FragmentA I create FragmentB like this:
FragmentB fragmentB = new FragmentB();
fragmentB.setTargetFragment(this, "RESULT");
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.frl_view_container, fragmentB);
transaction.addToBackStack(null);
transaction.commit();
From FragmentB I pass a string value back to FragmentA and return back to FragmentA.
This is the code I use in FragmentB:
String res = "OK";
Intent intent = new Intent();
intent.putExtra("RESULT", res);
getTargetFragment().onActivityResult(getTargetRequestCode(), 0xFF, intent);
getFragmentManager().popBackStack();
The res value is passed back successfully via FragmentA's onActivityResult:
String result;
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
if ((resultCode == FragmentActivity.RESULT_OK) && ( requestCode == 0xFF) ) {
result = data.getStringExtra("RESULT");
}
}
However, when I go back to FragmentA's onCreateView, I try to show the result I ported from FragmentB via a simple call:
edt_result.setText(result);
but the EditText does not show anything! The same happens if I simply write:
edt_result.setText("blah blah"); // The "blah blah" does not appear in my view!
Do you have any idea how I can overcome this problem?
Could it be that the EditText loses its state when I go back to FragmentA? I tried to override FragmentA's onSaveInstanceState method but onSaveInstanceState is not called at all when I leave FragmentA for FragmentB!
Any help is highly appreciated.
The answer is to call setText in onResume and not in onCreateView. Read here for more:
EditText Settext not working with Fragment

Replacing fragments in 1 activity

I have two fragments, call the fragment A and fragment B
both fragments take up the half screen when shown (this is ok)
the activity is loaded with Fragment A in onCreate being attached (this is ok)
now at some press of a button in fragment A i want to replace fragment A on screen with fragment B (this is done)
however, when i do a backpress, i want to replace fragment B with the original fragment A
here is my code in onCreate to show fragment A
Log.d(TAG, "replacing fragment");
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(resId, fragment);
ft.commit();
//no call to backstack intentionally, so that fragment A can't be detached with back press
this is my code on button click to show fragment B
Log.d(TAG, "replacing fragment");
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.setCustomAnimations(enterAnim, exitAnim);
ft.replace(resId, fragment);
ft.addToBackStack(null);
ft.commit();
now when in my onBackPressed method i wrote
getSupportFragmentManager().popBackStack();
this is what happens : fragment B gets removed, fragment A does not display behind it. meaning i get a blank white screen with nothing on it, in the place where fragment A was supposed to be.
why does this happen ?

Resources