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 :)
Related
The project is multiple-fragment project. Launch application to start BaseActivity, which all fragments are attached into it.
Issue:
Fragment A is the first fragment and is launched very well.
Fragment B is trigged by clicking button of Fragment A, also launched very well.
During from B back to A, the problem occurs. Nothing to display, only background, the BaseActivity.
All fragments use the api "replace" to display, although this API may produce performance problems, please ignore it firstly.
I have tried many methods, but nothing works.
The launched fragment method shows below:
public void navigateToFragment(Fragment fragment, boolean isClear){
if(isClear){
getSupportFragmentManager()
.popBackStack(null,FragmentManager.POP_BACK_STACK_INCLUSIVE);
}
getSupportFragmentManager().beginTransaction()
.addToBackStack(null)
.replace(R.id.container,fragment)
.commit()
}
As I know, popBackStack and replace, commit can lead to one fragment is launched twice, but why no display after replace whatever I replace it?
try use add instead of replace
getSupportFragmentManager().beginTransaction()
.addToBackStack(null)
.add(R.id.container,fragment)
.commit()
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();
}
I'm working on an activity which shows a couple different fragments.
Let's say Activity A shows fragments B C and D in that order. When I press the back button on D, I would like C to be shown, C goes to B, etc. However, whenever I press the back button Activity A acts as if the back button was pressed on it and exits the activity.
Here's what my transactions look like, this is all done in Activity A...
getFragmentManager().beginTransaction()
.add(R.id.container, FragmentB.newInstance(Types.Mine))
.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
.addToBackStack(null)
.commit();
I then for the following fragments, I perform:
getFragmentManager().beginTransaction()
.replace(R.id.container, FragmentC.newInstance(Types.OXY))
.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
.addToBackStack(null)
.commit();
Changing the add to replace. I've done this with a couple different configurations but can't seem to get the correct behavior. Any suggestions??
Edit: I've looked at the getFragmentManager().getBackStackEntryCount() after every transaction, and it seems they're being added correctly because the count keeps going up...
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! :)
I need to detect backbutton pressed in Fragment.
My app start download data in Fragment, so I need to stop download (also do some operations) on back button pressed, and dont need if home button pressed. So I cant use onPause(), because its called in both cases.
I use addToBackStack(null), but I need to do some operations (stop download and ) in current fragment and then return to previous fragment.
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.content_frame, fragment)
.addToBackStack(null)
.commit();
When you are done with the things you want to do in the fragment you could use something like this to remove the current fragment. If last fragment was added to the stack (as you mentioned) then removing current fragment will automatically display last fragment.
this.getActivity().getSupportFragmentManager().beginTransaction()
.remove(this).commit();
or
this.getActivity().getFragmentManager().beginTransaction()
.remove(this).commit();
Look at this link
On yout activity you can overide onBackPressed() et then call popBackStack