Android fragments popBackStack - android

I have the following issue and I'm no quite sure whether it is a bug or misunderstanding by me.
I have a single activity, Fragment A (attached to activity's onCreate Method) and Fragment B (not attached to activity during its creation). On button click, Fragment A is replaced with Fragment B with transition. So far so good.
On Fragment B there is button which invokes fragmentManager.popBackStackImmediate(). If I press this button and turn the screen off at the same time (during fragment transition) then, when the screen is back on, the two fragments are visible (fragment B should have been destroyed, but it is not).
This problem starts with support lib 27.1.0+. Prior to that, the fragments were replaced correctly during Back stack navigation. Have in mind that I'm using fragments from support library.
I read behavior changes but I could not manage to link some of it to my issue.
Does anybody knows some work around or has more information about it?
Thanks in advance!
UPDATE:
Problem exists only on devices up to Android 6. On Android 7+ everything is alright.

fragmentManager.beginTrasaction().replace().addToBackStack() - do it for FragmentA in onCreate and do it on click for FragmentB. And try popBackStack(). I think you forgot addToBackStack FragmentA.

Related

Activity is destroyed when hit home button (only on Pixel3)

I have one big and long question
I have one activity with 4 fragments using viewPager.
the first fragment also has 4 fragments using viewPager
the second fragment has 6 fragments using general fragment way (replace or add on framelayout)
Here are my question and problem
For example, I am in the first tab and in the second fragments, then hit homebutton and then bring my app upfront. after that suddenly all functionalities are not working. the back button is not working. the button in the fragment is not working. (error log said the fragment is not attatched)
it happens only on pixel3.
I have 5 different android phones but working fine on those phones.
I thought it could be the memory issue, so I turned on "profiler" from Android Studio, and I saw the weird thing. only pixel3 when I pressed the home button the activity is destroyed. I tested all other phones with "Profiler" the activity's state is saved not destroyed.
Does anyone have the idea of why it happened and fixed the problem?
Are the embedded fragments problem? or is it OS10 problem?
I found the issue. there is one option in developer mode named "Don't keep activities". that was on, that is why my app is working differently on pixel3.

Maintaining the state of fragment when navigating to other fragment using Navgation Architechture component

In my project I have used Navigation component to navigate between fragments. But when I navigate from one fragment to other fragment the previous one fragment loose its state when coming back to it.
For ex. I have fragment A and write something in its editText and then I navigate to other fragment B. But when I come back to my fragment A using backpress the editText in fragment A is empty.
Is there a workaround or solution to this issue?
As per the open issue, Navigation does not directly support multiple back stacks - i.e., saving the state of stack B when you go back to B from A or C since Fragments do not support multiple back stacks.
As per this comment:
The NavigationAdvancedSample is now available at https://github.com/googlesamples/android-architecture-components/tree/master/NavigationAdvancedSample
This sample uses multiple NavHostFragments, one for each bottom navigation tab, to work around the current limitations of the Fragment API in supporting multiple back stacks.
We'll be proceeding with the Fragment API to support multiple back stacks and the Navigation API to plug into it once created, which will remove the need for anything like the NavigationExtensions.kt file. We'll continue to use this issue to track that work.
Therefore you can use the NavigationAdvancedSample approach in your app right now and star the issue so that you get updates for when the underlying issue is resolved and direct support is added to Navigation.

How to keep a fragment running in background which can also be brought to foreground when needed?

Weird but I could not find any related text on this on the net.
I have a fragment with Cordova WebView in it. The app has many other fragments with native list views, etc. User can use the options in Navigation Drawer to switch between the fragments, one of which is the WebView fragment.
I want that the WebView fragment keeps on running in background when it is replaced by another fragment. Later based on user's choice, this fragment might need to be brought to the foreground again.
Is this possible?
If you call replace(), the fragment is removed. You could instead try detaching the fragment from the UI using detach(), and then later recalling it with attach().

FragmentActivity with ViewPager managing multiple fragments taking 2 back presses to exit

I have tried searching across to find a solution for it, but none of them seemed to work.
My use case is: I have a FragmentActivity which contains a ViewPager to manager couple of fragments whose that if coming from different data sources - thus things like Service,Content Proviers,Loaders etc are involved.
While the onCreateView of my Fragments is only called once, but when I try to exit the application by pressing the back key, the view pager first removes the fragment set and display the blank activity, and the consequent press on the back actually exits the application.
I have tried couple of things like:
calling finish() in onKey... onBackPressed within the activity
calling getActivity().finish() from withing the Fragment and its
main layout view
but none of them worked.
**closing application by using its PID - thats something I dont want to try.
Its surely not the standard behaviour becuase I have seen people on SO asking the opposite of my query with their apps getting exited.
So, I would really like the activity to be closed on the first press of the back button.

Saving a backstack across orientation change

I am trying out using fragments with my tabhost for the first time and I am pretty impressed. That said, one of my tabs uses two fragments, first the user sees Fragment A and then if they hit a button in Fragment A it gets swapped with fragment B, and puts fragment A on the backstack. This all seems to work fine, except it does something funny on a change in orientation:
If the user is on Fragment B and there is an orientation change, it still shows fragment B. However if after the orientation change, they switch to a different tab and then switch back to the original tab, it goes back to showing Fragment A.
This does not happen without the orientation change. IE... if you are just on Fragment B, switch tabs, and then switch back, you still see Fragment B. Is there any way to preserve this across an orientation change?
Thanks in advance!
try to add this code to your manifest, for your activity:
android:configChanges="orientation|keyboardHidden"
______EDIT______
As ErstwhileIII mentioned, this is not real fix, and this will force Android to don't recreate Activity when device rotated. However, as you can see, I have answered to this question on October 14, 2011, and if you are looking for better answer, just try to find newer answers.

Categories

Resources