I have a BottomNavigation implemented in my app. when i tap on navigation items it shows their respective fragments, it is working fine. I have implemented a progress bar in every fragment so whenever i select a fragment from BottomNavigation the progress bar displays, my question is when i select any option from BottomNavigation progress bar shows then fragments load but when the fragment is loaded it should gets saved when i come back to that option from BottomNavigation it loads again... is there any way to save the state of that fragment so that it only loads for the first time.
Use onSavedInstanceState(Bundle) This will get you the required behavior, so this method is called before an activity may be killed so that when it comes back some time in the future it can restore its state.
Here i use activity which in respect is any activity, even if its a fragment, this example is from the official documentation.
For example, if activity B is launched in front of activity A, and at some point activity A is killed to reclaim resources, activity A will have a chance to save the current state of its user interface via this method so that when the user returns to activity A, the state of the user interface can be restored via onCreate(Bundle) or onRestoreInstanceState(Bundle).
check out this cool link, it describes everything and a little bit more: https://inthecheesefactory.com/blog/fragment-state-saving-best-practices/en
Related
Hello I currently have an Activity A with a fragment inside that the user can interact with, the goal here is to simply "background" Activity A and fragment and allow the user to use the rest of the app normally with a floating button that when pressed the user will be taken back to Activity A and the UI will be restored the same as the user left it. I've looked into onSaveInstanceState but that only applies to when the system ends it or when screen rotation happens and not when the user presses the back button. So is it possible to save Activity A with the same UI and restore it at any given time?
Edit: I also have no way of accessing/modifying the fragment's components since it's coming from an SDK already.
I am using the UI Navigation Graph. I have fragment A and B. My application starts on Fragment B to show some information. Then the user can press back and end up in A. Then from A the user usually picks something and ends up in B.
Normally I can pop B from the stack and show A. However, when the app just launches there is no A in the back stack. I guess I could add A to the backstack manually but I wonder if the Navigation UI Graph has something that lets me do this.
I cannot change the order of my application fragments. That means that I still need to show B first.
As per the Principles of Navigation, the start destination of your app should always be the last screen the user sees before they exit your app. That means that your Fragment A should be the start destination of your graph.
As per the Conditional Navigation documentation, if you have important information that the user should see (such as a first time user experience), you should have your Fragment A navigate to your Fragment B. This ensures that when you're on Fragment B, Fragment A is on the back stack already.
When I navigate from one Fragment to another with the new Navigation Components, I can do it successfully with the following:
btn_walk.setOnClickListener(Navigation.createNavigateOnClickListener(R.id.next_action))
Now, if I pop the backstack from the destination Fragment with
findNavController().popBackStack()
The Fragment that launched that Fragments recreates itself again, but the state is not being saved, I have been looking around to see how to fix it, but I don't really know if actually how to do an .add function with the navigate() from Navigation components
This gif shows the navigation from one fragment to another, when popping the stack from the destination fragment, it recreates the fragment that launched that one instead of saving the state of it.
Using the fragmenttransaction.add() method does maintain the instance of that fragment alive also if I pop the back stack.
Is there any way to do the same with Navigation Components ?
I have also readed this Navigation Architecture Fragment Reload Problem but it's unclear how it should be solved.
Fragments automatically restore their saved state when they return from the back stack: that is true with our without Navigation. Seems like the problem is with how you've coded your Fragment.
You can confirm this is a problem with your Fragment by turning on 'Don't keep activities', hitting the home button, then returning to your app and see if the Fragment restores its state perfectly. You can also test the case where you handle configuration changes (i.e., rotating the device) correctly - again, you should be able to restore your state exactly where you were. If you handle both those cases, then it'll work great in Navigation as well.
I have a navigational bar (a bunch of buttons) in a fragment and I'm trying to have it on every page of my app.
When I interact with the homepage and change the text to abc on a button and then click the navigational bar fragment, I am successfully taken to the next page. When I hit android's given back button, the app successfully moves back to the activity with the text changed to abc.
However, if I change the text to abc then click the nav bar to a new page and then click the nav bar back to the home page, the text has been reset to the original values and not abc.
I'm not really sure how else to explain the problem, anyone have any clues or want more clarification?
It is challenging to understand your app functionality without code base and UI. From my understanding, I believe you are not using the savedInstancestate bundle object to save your data whenever you are clicking the back button on the nav bar. You must bundle your data before going back to your activity or launching a new activity/fragment if you want to save your text changes. Look into two things:
Activity/Fragment lifecycle.
SavedInstanceState
Your fragment need tag or id when adding/replace in order to restore.
String mCurrentOverlayFragmentTag = "ControlOverlayFragment";
getFragmentManager()
.beginTransaction()
.replace(R.id.overlayHolder, fragment, mCurrentOverlayFragmentTag)
.commit();
On my app I am using a Main Activity, which has a Navigation Drawer, and as the user go to an options from the drawer, it will change the fragment beging displayed accordingly to the option selected.
If the user hit's "Back button" several times, it will go back to a point in which it will reach my Main Activity, which is a blank and empty layout.
When I reach this point (my main activit, empty), I would like to exit the app, or, open the Navigation Drawer.
The problem is, I don't know any event that shows me that I am back to the Main Activity. I checked the onResume, but it's never called, which makes sense, since the main activity has never been stopped.
I thought perhaps there would be an event from the fragment manager that would be called on the Main Activity when a fragment was detached, and from there I could check if there was no fragment at all attached?
When you push your first fragment, add a tag to it. Something like
transaction.replace( R.id.rootContainer, firstFragment, "rootFragment");
Whenever user presses back button, you can get your rootFragment from FragmentManager
FirstFragment myFragment = (FirstFragment) getSupportFragmentManager().findFragmentByTag("rootFragment");
And check if this fragment is visible or not, by myFragment.isVisible(), if not, then keep popping the stack, if it is visible, it means user is on the first fragment. Now you can either quit the app, or show your menu drawer.
Good night good sir. Thank you for your tip.
I used a different approach, based on your repply, that gave me quite a few insights.
On main activity, I Overrided the onKeyDown and check if it was pressed the Back Button. If yes, then I check the size of my Back Stack.
Uppon that, I decide if I want to finish my application.
Also, thanks for the tip of "labeling" the fragments on the back stack, didn't know I could do that.