Previous fragment visible below new fragment - android

I have a TabLayout with a ViewPager. The ViewPager have four fragments F1, F2, F3 and F4. F1 contains a FrameLayout which can have 2 fragments F11 and F12. Initially I add F11 in the FrameLayout with below code.
Fragment11 fragment11 = new Fragment11();
fragment11.setArguments(getActivity().getIntent().getExtras());
getActivity().getSupportFragmentManager().beginTransaction()
.add(R.id.fragment_container, fragment11, Constants.FRAGMENT_11)
.commit();
F11 contains a ListView. When I click on any item/row in this ListView then F11 is replaced with F12. F12 is a detail fragment.
Fragment12 fragment12 = new Fragment12();
FragmentTransaction fragmentTransaction = getActivity().getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, fragment12, Constants.FRAGMENT_12)
.addToBackStack(null)
.commit();
For normal app flow it works fine. Now suppose currently I am in Fragment12 and I pressed home button. Now I open any heavy application (camera or any other app) to remove my app from memory. Now I started my app again.
Now there are two Fragment11 and one Fragment12 are visible, all at the same time. When I press back Fragment12 is removed and now two Fragment11 are visible. When I click the ListView row of Fragment11 then the top Fragment11 is replaced with Fragment12, however, the bottom Fragment11 remains there.
This is what I want:
When the app comes to the foreground from background then Fragment12 should be visible and when I press back Fragment12 should be popped to show Fragment11.
How can I do this?

I know it's too late but this answer can help others. it's not a proper solution but it's working for me. So put android:clickable="true" and android:background="?android:attr/colorBackground" in the root view of your fragment. hope this will work.

I have faced the same problem. But I also could not find a proper solution for this. In my scenario when I open multiple apps and them come back to my app it causes the same issue with Fragments that happen on your side. But I made my app stable. I have done this by the following solution:
When your app remove from memory then again open the app then onCreate() method called of your MainActivity in which you are showing Fragments.
Here I checked the backstackEntrycount and if it is greater then 0, then remove all fragments and show the base fragment.
// Clear all fragments and show base fragment, So it will not create
// issue after opening
// multiple apps
FrameLayout flSliding = (FrameLayout) findViewById(R.id.fl_sliding);
flSliding.post(new Runnable() {
#Override
public void run() {
FragmentManager fragmentManager = getSupportFragmentManager();
Log.i("NavigationDrawerActivity",
"onCreate " + fragmentManager.getBackStackEntryCount());
if (fragmentManager.getBackStackEntryCount() > 0) {
removeFragment(fragmentManager.getBackStackEntryCount());
}
Log.i("NavigationDrawerActivity",
"onCreate " + fragmentManager.getBackStackEntryCount());
}
});
Hope it will help!

Related

Fragments are drawn on top of eachother

The scenario is this...I click on a button that loads the next fragment. While the animation is still going I hit the recent apps button. After the recent apps screen is shown i return to my app.
Then I see the two fragments drawn on top of each other. As if Android forgot to remove the previous fragment.
I click back to remove all fragments until I reach the first fragment loaded in this FragmentActivity. I log all the fragments I get from FragmentManager and it only shows one. But I still see two fragments drawn one on top of the other. The one that should have been removed is not responding to touch events and the other one responds as it should.
Is this an Android bug or my fault? Is there any way to fix this or prevent it from happening ?
It does not happen every time. This is how I load my fragments
public void loadNewFragment(AnimFragment newFragment, boolean addToSTack, boolean animate, String tag) {
if (newFragment != null) {
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.setCustomAnimations(R.anim.fragment_enter_from_right, R.anim.fragment_exit_to_left, R.anim.fragment_enter_from_left, R.anim.fragment_exit_to_right);
transaction.replace(R.id.fragment_container, newFragment, tag);
if (addToSTack) {
transaction.addToBackStack(tag);
}
int transactionId = transaction.commit();
newFragment.setTransactionId(transactionId);
}
}
and I remove them on back pressed with
getSupportFragmentManager().popBackStackImmediate();
In my opinion "below" fragment is onPause() state and it's still alive and visible.
Try to use white background on the root layout of the front fragment to avoid showing the below fragment. Moreover the root layout size (width and height) of the front fragment should be match_parent.
Check this site to understand Fragment lifecycle.
https://google-developer-training.gitbooks.io/android-developer-advanced-course-concepts/unit-1-expand-the-user-experience/lesson-1-fragments/1-2-c-fragment-lifecycle-and-communications/1-2-c-fragment-lifecycle-and-communications.html

Parent Activity with fragments shows blank screen occassionally

I'm working on a application which has Single activity rest all fragments...
This issue i'm facing occurs occasionally.. like when ever i press back button from any of the fragment the previous fragment appears... but sometimes it happens that My activity shows blank screen with action bar present, and rest of the screen is blank instead of showing the fragment... Since this issue appears occasionally i'm not able to debug it also... Any idea why it happens so??
Here is the code fragment transaction..
private void loadFragment(Fragment fragment, int activityNumber) {
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction trnx = manager.beginTransaction();
trnx.replace(R.id.fragPage, fragment, "Current_Fragment");
if (activityNumber != FragmentActivityNumbers.HOME_ORDERNUM) {
trnx.addToBackStack(null);
}
trnx.commit();
}
I suggest to test if its the first fragment so you use Add instead of replace then you can use replace for the others fragments .

Android Pager using Fragment

In android Coding I have some doubts.I am working In Paging concepts.
I have two Fragments. One is Master Fragment and another one is Transaction Fragment.
In master one Button is available. That is vendor. If we click the vendor button we can create a new vendor details.
Its work fine to move forward. That is if we click the vendor button the next page is opened. But if return backward by pressing it doesn't work.
By pressing the back button from new vendor creation to vendor it works. But if we press back button from vendor to Pager fragment it does n't work.
Here I have attached the coding and screen shots. Please help me. Thanks in advance.
Screen Shots
Master
Transaction
Vendor
Vendor Creation
The following is the code when we click the backbutton
Fragment fragment = new MainActivity();
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.frag_act_cont, fragment, "Class Name").commit();
I don't know if I correctly understand your question. Have a look at the Android Developer Page here
To be able to provide proper back navigation you have to add the fragment to the back stack by
getSupportFragmentManager().beginTransaction()
.add(detailFragment, "detail")
// Add this transaction to the back stack
.addToBackStack()
.commit();
When you press back button the onBackPressed method is called that causes activity to finish.Have a look at this post:http://stackoverflow.com/questions/22011751/life-cycle-of-android-activity-after-pressing-back-button
and try this piece of code.On your backpress method do something like this:
#Override
public void onBackPressed() {
super.onBackPressed();
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.frag_act_cont, "Class Name").commit();
}

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.

Back key to pop the Fragment shows overlapping fragments

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.

Categories

Resources