Back key to pop the Fragment shows overlapping fragments - android

I created a sample app to test this overlapping issue.
I have a fragment type, Fragment1, and I create a new instance of Fragment1 and add it to a FrameLayout in my activity at runtime. I add the fragment with the help of a few buttons.
Note: I have given each new instance of Fragment1 a different number(#1, #2, #3, etc.) to display on the UI to help me figure out which fragment I am viewing.
So.. here is what I do:
Click on Button 3, create new instance of Fragment1 and add it to Frame1.
Click on Button 4, create new instance of Fragment1 and add it to Frame1 and add it to the fragment backstack.
Repeat 1 and 2.
Repeat 1 and 2.
Now, I have fragments in this order: 1(#1),2(#2),1(#3),2(#4),1(#5),2(#6).
I press the back key when viewing fragment #6.
Back key press, UI displays (#5).
Back key press, UI displays (#3 AND #5),
Back key press, UI displays (#1, #3, AND #5)
It seems fragments are getting displayed ON TOP of each other.
WHY? Is there an overlapping issue? How can I clear out this overlapping issue. I thought this would be an issue on the compatibility library... but it is also on 3.0.
Code for adding fragments:
public int doFragmentChange(int cont1, Fragment frag1, String tag1, int cont2, Fragment frag2, String tag2,
boolean addToStack, String stackTag) {
FragmentManager fm = getFragmentManager();// getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
if (frag1 != null) {
ft.replace(cont1, frag1, tag1);
}
if (frag2 != null) {
ft.replace(cont2, frag2, tag2);
}
// add fragment to stack
if (addToStack)
ft.addToBackStack(stackTag);
return ft.commit();
}

If you perform two add calls one after the other (two commit calls) then yes the fragments will appear overlaid, one on top of the other effectively.
So (for new example) if say you replace frag1 with frag2 and then frag3 with frag4 in the same frame with no backstack transaction then I would expect frag2 and frag4 to be overlaid.
Furtheremore there is also a potential issue in your chaining of replace. You should call a separate commit for each. See Android — Replace Fragment Back Stack With New Stack?.

Just override the onBackPress() or onKeyUp and remove the top fragment.

Related

Android popBackStack shows fragment not added to the backstack

MainFragment launches FragA that launches FragB that lunches FragC.
Back button press on FragB should go to FragA and back press on FragC should also go to FragA.
FragB is the only one where isToAddToBackStack is false.
childFragmentManager.commit {
replace(containerViewId, fragment, fragment::class.java.name)
if (isToAddToBackStack) {
addToBackStack(backStateName)
}
}
Whenever the back button is pressed on FragC, FragB is shown:
if (childFragmentManager.backStackEntryCount > 1) {
childFragmentManager.popBackStack()
return
}
What's the better way to achieve the navigation that I'm expecting?
Fragment transactions can involve two different types of tags. The one that most Android developers are familiar with is the Fragment tag, which you can use to find a specific Fragment in your FragmentManager later via findFragmentByTag(). This is useful for finding a Fragment when your application is in a particular state, but keep in mind that the Fragment needs to be added to your FragmentManager. If you have removed() or replaced() a Fragment and haven’t added it to the back stack, you won’t be able to find it.
The other type of tag is the BackStackRecord’s name passed into addToBackStack(). This name identifies a particular back stack record, which is a record of what occurred in a particular transaction. popBackStackImmediate() and its counterparts have variants that accept a back stack record name to pop the back stack to a particular state.
//fm is FragmentManager
// Fragment a is on the screen
Fragment a = new A_Fragment()
fm.beginTransaction()
.remove(null /*no fragments in R.id.content*/)
.add(R.id.content, aFragment, "fragment-a")
.commit();
// user wants to go from A to B
Fragment bFragment = new B_Fragment();
fm.beginTransaction()
.remove(fm.findFragmentById(R.id.content))
.add(R.id.content, bFragment, "fragment-b")
.addToBackStack("a")
.commit();
// user wants to go from B to C
fm.beginTransaction()
.remove(fm.findFragmentById(R.id.content))
.add(R.id.content, new C_Fragment(), "fragment-c")
.commit();

android add fragment advice

In my application I have two fragments A and B. A fragment contains Google map and listview inside on it B fragment Google map only.
I want open B fragment from A fragment but when I press back button it has to go to A fragment without loading data.
public void addPage(final DefaultFragment pDefaultFragment, final boolean isAddToBackStack){
FragmentTransaction transaction = mFragmentManager.beginTransaction();
transaction.add(R.id.content_frame, pDefaultFragment);
//transaction.replace(R.id.content_frame, pDefaultFragment);
if (isAddToBackStack) transaction.addToBackStack(null);
transaction.commitAllowingStateLoss();
}
I use this code for adding fragment
Bframent b = new Bfragment();
addPage(b,true);
I know differences between transaction. add and transaction.replace. My problem is when I use transaction.add A fragment's map doesn't destroy it stays above of B fragment's map when pressing button. But when use transaction.replace after press back button data loads again.
So could anyone tell me what should I do to Press back button without to load data without any problem on view also.
On back press just provide your fragment null;

Android Fragments - How to implement Backstacking and Recalling previous fragment

I have android screen layout shown below
Apps screen divided into 3 fragments, Header, Footer and Content. Header and Footer fragments are fixed. Content fragment is changing according to content. I replaces fragment1-fragment3 according to need. Initially Fragment1 is shows in content area. When i press a next button fragment1 is replace by fragment2. This is the order. I have question, if i press another button previous, how can i return to previous fragment ( fragment2 -> fragment1). Is any built in mechanism available in fragment class.
Please guide me...
thanks in advance
try this code
FragmentTransaction tx = fragmentManager.beginTransation();
tx.replace( R.id.fragment, new MyFragment() ).addToBackStack( "tag" ).commit();
Whenever you try to call new fragment then add back stack for it through
ft.addToBackStack(null);
And now if you want to back from one fragment to another then, make one method..
public void DeleteCurrentFragment()
{
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
Fragment currentFrag = getSupportFragmentManager().findFragmentById(R.id.detailFragment);
String fragName = "NONE";
if (currentFrag!=null)
fragName = currentFrag.getClass().getSimpleName();
if (currentFrag != null)
transaction.remove(currentFrag);
transaction.commit();
}
And after that call this:
getSupportFragmentManager().popBackStack();
DeleteCurrentFragment();
you can try something like:
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction ft = manager.beginTransaction();
ft.add(R.id.contentFragment, <your fragment>);
ft.addToBackStack(null);
ft.commit();
so suppose you're on fragment1 and after doing some stuff you want push fragment2 then on your contentFragment you can add or replace the fragment (fragment2 in this case) as per your requirement. Now when you call addToBackStack it means add this transaction to the back stack. So that the transaction will be remembered after it is committed, and will reverse its operation when later popped off the stack. Internally it maintains a stack so you don't want to do anything. At last when you press back button from fragment2 it checks whether the transaction having any fragment in its stack, if yes then it calls it. Its like top of stack. When you call addToBackStack in that stack fragment1 is added. So when you press back button stack's top is fragment1 so it calls it.
It happens when you press hardware back button. If you want to do this on any button, then on that button's click listener simply onBackPressed() method.

Preserving fragment state

In my app, I have the following architecture:
MainActivity
|_ FirstFragment
| |_ GridFragment
| |_ MapFragment
|
|_ SecondFragment
|
|_ ThirdFragment
Please note that:
MainActivity is using ActionBarSherlock and SlidingMenu.
SlidingMenu switches between fragments (FirstFragment, SecondFragment, ThirdFragment).
FirstFragment creates a button into the action bar to switch between grid and map mode. This is, to switch between its subfragments.
What I want to achieve
I need fragments need to preserve its state when switching between them. This is, if I am in FirstFragment and then select map mode (show MapFragment subfragment), and from the slide menu choose another option and come back, I should see the map again. Right now, it's resetting each fragment when selecting its section from the slide menu.
This isn't surprising, as at the moment I am commiting transactions with new FirstFragment() when the menu options are pressed.
However, I first tried to have references to each fragment into his parent. For example, MainActivity had three fragments members which, when commiting transactions, were checked if exist previously and instantiated if necessary. This worked fine until I added the second level of fragments, then it started throwing exceptions when committing the transaction (saying that the Activity was destroyed).
As you may have noticed, I am far from being an Android expert and need some guidance on this topic.
How can I preserve fragment states without having a reference for each of them?
Next thing I tried after posting the question, was to avoid keeping the fragments in local variables inside its container and using FragmentManager.getFragmentByTag() to access them at any given time, but the problem persisted, as FragmentTransaction.replace() was destroying the fragments.
My solution
As Luksprog pointed out in his comment, I had to manage all fragments manually. In order to achieve this, I had to go back to my former approach, where I had local variables for each fragment. Then, MainActivity does the following:
Instantiate its three fragments.
this.firstFragment = new FirstFragment();
this.secondFragment = new SecondFragment();
this.thirdFragment = new ThirdFragment();
Attach the three fragments, and hide all of them except the initial section.
getSupportFragmentManager()
.beginTransaction()
.add(R.id.content_frame, this.firstFragment)
.add(R.id.content_frame, this.secondFragment)
.add(R.id.content_frame, this.thirdFragment)
.hide(this.secondFragment)
.hide(this.thirdFragment)
.commit();
To switch content, SlideMenu is calling this function:
public void switchContent(Fragment newContent) {
if (newContent != null) {
getSupportFragmentManager()
.beginTransaction()
.detach(this.firstFragment)
.detach(this.secondFragment)
.detach(this.thirdFragment)
.attach(newContent)
.commit();
// Restore menu open gesture if map is not present
if (!(newContent instanceof firstFragment) && getSlidingMenu().getTouchModeAbove() != SlidingMenu.TOUCHMODE_FULLSCREEN)
getSlidingMenu().setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
// Set menu open gesture if map is present
if (newContent instanceof firstFragment && firstFragment.currentFragment == FirstFragment.MAP_FRAGMENT)
getSlidingMenu().setTouchModeAbove(SlidingMenu.TOUCHMODE_NONE);
getSlidingMenu().showContent();
}
}
Then, the fragments which has subfragments (FirstFragment) is doing the same but:
It's using getChildFragmentManager() instead of getSupportFragmentManager().
It's using show() and hide() to replace content, because when using detach() and attach() the map wasn't preserving it's coordinates.

Swap two fragment simultaneously

In my application i have one activity and i am adding two fragments at run time.I need to swap these two fragment simultaneously. Fragment 1 consist a button and i want when i click that button fragment 1 moves to right side of the screen and other fragment to the left side of the activity.
In the onClick method of the button i tried something like this
#Override
public void onClick(View v) {
FragmentTransaction ft = getFragmentManager().beginTransaction();
Fragment newFragment = getFragmentManager().findFragmentById(R.id.pageA);
ft.remove(newFragment);
Fragment newFragmentB = getFragmentManager().findFragmentById(R.id.pageB);
ft.remove(newFragmentB);
ft.add(R.id.pageB, newFragment);
ft.add(R.id.pageA, newFragmentB);
ft.addToBackStack(null);
ft.commit();
}
But i am getting the following error
java.lang.IllegalStateException: Can't change container ID of fragment PageA{40653da0 #0 id=0x7f060001}: was 2131099649 now 2131099650
I want something like this when i click the button on Page A then Position of Page A and PageB should swap with each other.
I have a similar issue ( IllegalStateException: Can't change container ID of Fragment ) and i solved by swapping the containers instead of the fragments... Nonetheless i still have no clue as to whether it's possibile to swap directly fragments. As I wrote in the aforementioned post, it seems to work only on ics!
I posted a solution to this problem to a similar question. My approach is to re-create the fragment, but keeping the state by saving it first and re-applying it to the new instance.
See https://stackoverflow.com/a/14951987/599614.

Categories

Resources