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();
}
Related
I have this method to add a fragment :
public void addFragmentOnTop(Fragment fragment) {
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.content, fragment)
.addToBackStack(null)
.commit();
}
Assume that I will search item with key "earphone" in Home. Then the R.id.content will replace with SearchFragment. When I search again (not tap the back button to back to home, cause the SearchView is in toolbar) with key "headset", the R.id.content will replace again. The problem is, Why this generate a history fragment? When I tap back button, the R.id.content will show the search result with key "earphone". I have try with getSupportFragmentManager().remove(SearchFragment.TAG); but no affect to my apps. Anyone can help me? :(
Remove .addToBackStack(null);. It adds your Fragment on a backstack (creates that Fragment history that you are talking about), which is being poped by your Activity as soon as you press back button.
Hope this helps. Good luck :)
I have created an AppCompatActivity Opened fragment A->B->C->D->E->F
with replace()
I am on F which contain button when I press the
button I want to clear Fragments up to C and Want to open G on top of C so new Sequence will be A->B->C->G.I can do this with
popBackStackImmediate() and add G on top of C with replace function.
Problem: When I press the button I see C for fraction of seconds and then G Is displayed on it.To Prevent this I tried to stop the animations with help of answer but C still visible for fraction of seconds even when the animation is stopped for fragments.
Is there any better way we can design fragment flow or way to solve this flicks when replacing fragment on top of C?
I was so curious about this question that i created a sample project and implemented the same use-case that you mentioned in your question. Here is how i handled this.
Used this method to remove F,E,D fragments from backstack
private void removeFragments() {
getSupportFragmentManager().popBackStack("F", FragmentManager.POP_BACK_STACK_INCLUSIVE);
getSupportFragmentManager().popBackStack("E", FragmentManager.POP_BACK_STACK_INCLUSIVE);
getSupportFragmentManager().popBackStack("D", FragmentManager.POP_BACK_STACK_INCLUSIVE);
}
Used this method to replace fragment
private void replaceNewFragment(String key) {
getSupportFragmentManager().beginTransaction().addToBackStack(key)
.replace(android.R.id.content, AFragment.newInstance(key)).commit();
}
Here is a video demo video.
Here is complete of this project on github
More Generic Solution for such navigation flow, Replace fragment like this
getSupportFragmentManager().beginTransaction().
replace(R.id.f_container,new FragmentA())
.addToBackStack("A")
.commit();
getSupportFragmentManager().beginTransaction().
replace(R.id.f_container,new FragmentB())
.addToBackStack("B")
.commit();
like wise do it till fragment F and lets assume you have submit button on F
now inside onClick of submit button
Pop back stack till D with POP_BACK_STACK_INCLUSIVE flag like below and Add replace container with fragment G
getActivity().getSupportFragmentManager().popBackStack("D",
FragmentManager.POP_BACK_STACK_INCLUSIVE);
getSupportFragmentManager().beginTransaction().
replace(R.id.f_container,new FragmentG())
.addToBackStack("G")
.commit();
Now when you press back button you will see Fragment C
I hope this will help you, its working for me
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!
How to implement unlimited fragment stack in android like Facebook application where you navigate from one profile to another and when you press the back button it navigates back to each profile you visited.
What you need is adding each new fragment you created to backstack as following, and then when you press back on new fragments, you just need to pop it from stack:
Fragment fragment = new YourFragmentGoesHere();
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.frame_container, fragment)
.addToBackStack("Your Pre-defined String name for related fragment").commit();
And when you press back in new fragment you go backwards with following :
FragmentManager fm = getFragmentManager();
fm.popBackStack("Your pre-defined string name for fragment", FragmentManager.POP_BACK_STACK_INCLUSIVE);
That's it basically.
It seems that you're looking for implementing an unlimited back stack for fragments in Android. What you really want to do is first read this page:
Providing Proper Back Navigation
Pay particular attention to the "Implement Back Navigation for Fragments" section. I could just repeat all the information here, but I'll instead specify that yes, this is technically unlimited, in the same sense Facebook's back button functionality is unlimited (ie, limited by free memory).
As another note, pay careful attention if different fragments change the UI in certain ways, as the standard back navigation for fragments just reverses the exact fragment transaction you had. If you run into issues, make sure to properly override the onBackPressed method and/or try to add/remove/replace fragments differently (ex: this question here).
My Application has one Activity with a layout (FrameLayout) for many fragments.
At the start of the Application it displays a list of Places (PlacesFragment).
When a Place is clicked, a Fragment that displays a list of cams (CamFragment) is added. Inside this fragment there is also a button "info".
When the user press the "info" button a new fragment (InfoPlaceFragment) that displays information about the place, is added.
Here is a graphical explanation:
I want to be able to go back to "CamFragment(B)" from "InfoPlaceFragment(C)", and to go back to "PlacesFragment(A)" from "CamFragment(B)".
One Question:
1) Is it possible to realize this or an alternative solution is better?
------- EDIT -------
SOLUTION:
In the "MainActivity.java":
onPlaceParse() // This method is called when the data from the server is received:
// Here I create the "PlaceFragment"
fManager = getSupportFragmentManager();
fragmentPlaces = new PlacesFragment();
fManager.beginTransaction()
.replace(R.id.fragment_list_container, fragmentPlaces)
.commit();
onResortSelected // This method is called when the resort is selected
fragmentCams = new CamsFragment();
fManager.beginTransaction()
.replace(R.id.fragment_list_container, fragmentCams)
.addToBackStack(null)
.commit();
onButtonInfoSelected // This method is called when the btn info is selected
fragmentInfo = new InfoResortFragment();
fManager = getSupportFragmentManager();
fManager.beginTransaction()
.replace(R.id.fragment_list_container, fragmentInfo, "Info")
.addToBackStack(null)
.commit();
When you press the back button, if you are in the "Info Fragment" it returns to "PlaceFragment" and if you are in the "CamsFragment" it returns to "PlacesFragment".
This is possible, you need to call addToBackStack(null) in the fragment transactions.
Reference: Providing Proper Back Navigation
When you create your FragmentTransaction that will add/remove/replace (or whatever pattern you are using) the Fragments, just make sure to call addToBackStack(). Then, when the user presses the back button, the system will automatically cycle back through the entries added to the back stack, in reverse order from how they were originally added. No additional work is required on your part! :)