Android adds item on back stack even when its not specified - android

I have a fragment, called SplashScreenFragment. He is adding in onCreate of application with call of
supportFragmentManager.beginTransaction()
.replace(R.id.host_root, SplashScreen())
.commit()
After 3 seconds I replacing this fragment with another one, calling
activity?.supportFragmentManager?.beginTransaction()
.replace(R.id.host_root, fragment, fragment.tag())
.addToBackStack(null)
.commit()
But when I'm pressing back button, Im returning to my SplashScreen, but I wanna exit an application. How to fix this?

Remove ".addToBackStack(null)"
This code means your fragment to add backstack.
addToBackStack(String)

#Override
public void onBackPressed() {
}
If this is not critical for you, you can override the onBackPressed call and do nothing

Related

Which callback should be used to detect android fragment removed from screen and it is not visible anymore

I use a FragmentTransaction to show my fragment inside my activity and also I add it to backstack like this:
getSupportFragmentManager().beginTransaction()
.addToBackStack(null)
.setCustomAnimations(R.anim.enter_from_left, R.anim.exit_to_right, R.anim.enter_from_right, R.anim.exit_to_left)
.replace(R.id.parentRelativeLayout, myFragment)
.commit();
When back button is pressed I check if myFragment is on top and if it is the case, I just call super.onBackPressed() to pop it from back stack and show previous fragment.
I want to know which event/callback on my fragment could help me to detect it is not shown on activity anymore.
I used onPause(), onDetach() and ... but they are not triggered (as it is dsecribed https://stackoverflow.com/a/16252923/1080355)

Fragment doesn't display

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()

How to determine if fragment at top of other fragment is removed

I have fragment A and adding fragment B in same container (not replacing). I am adding this transaction on backstack also. Now, when device back is pressed, fragment B will be removed and fragment A will become visible. I want to do something when fragment A becomes visible. I searched lot but could not find anything helpful.
Note - I don't want to add backstackchangelistner and call onResume on that fragment.
You can override onHiddenChanged(boolean hidden) in a Fragment.
This will be called when its shown/hidden.
I use the same approach in my app, where i add a fragment and hide the old one, and then use the onHiddenChanged callback when the user presses Back, and the old fragment is shown again.
As you have 2 entries in Back stack, you can check back stack count on
Back Pressed method.
FragmentManager mFragmentManager = getSupportFragmentManager();
int count= mFragmentManager.getBackStackEntryCount();
if(count==1){
// do your work
}
you can try
#Override
public void setMenuVisibility(boolean menuVisible) {
super.setMenuVisibility(menuVisible);
}
or
`fragment.isVisible();`

Remove flickering of on fragment transitions backs-tack clear on android

I have two questions:
I want to remove all fragments in back-stack when user come back to main page.
However when I call following statement there is a flickering on the main page.
fragmentManager.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
how to remove this flickering? I tried the method here no luck.
Pop the fragment backstack without playing the Pop-Animation*
In fragment transition i use replace method. But in some
transitions I dont want to reload the entire data when user presses
the back button. To implement this I used hide() and add() methods.
When this is done above back-stack remove process becomes really bad
with so many animations.
Is there a best practice to implement this?
I had exactly the same issue as (1). Here is what worked for me:
the transaction that shows the first fragment is added to the backstack using a named backstate, eg: "bottom".
the backstack is cleared with popBackStackImmediate("bottom", ...INCLUSIVE).
So, whenever I want to replace whatever is in the backstack with a new fragment I use the following function:
protected void showInitialFragment(Fragment fragment) {
getSupportFragmentManager()
.popBackStackImmediate(BOTTOM_BACK_STATE, FragmentManager.POP_BACK_STACK_INCLUSIVE);
getSupportFragmentManager()
.beginTransaction()
.add(R.id.fragment_content, fragment)
.addToBackStack(BOTTOM_BACK_STATE)
.commit();
}
I also had to override onBackPressed() like this:
#Override
public void onBackPressed() {
if (getSupportFragmentManager().getBackStackEntryCount() == 1) {
finish();
} else {
super.onBackPressed();
}
}
Hope this helps!
Found an easy method https://stackoverflow.com/a/67005552/7418129. Sorry if I'm late. But it will definitely help someone.
The best solution for me was to add setReorderingAllowed(true).
Example:
parentFragmentManager.commit {
setReorderingAllowed(true)
addToBackStack(null)
setCustomAnimations(R.anim.enter_right, R.anim.exit_left, R.anim.enter_left, R.anim.exit_right)
replace<AnotherFragment>(R.id.fragment_container)
}

Detect backbutton pressed in Fragment

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

Categories

Resources