I have an activity that includes two fragments, I can navigate between the frgaments by bottom navigation menu. In both of them I have a reyclerView with image and text (data is retrieving by FireBase database).
I have the following code in MainActivity (using switch) that replaces the fragment by using navigation menu. (That is why it recreates the whole fragment every time and retrieves the data again as I found out).
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_space, selectedFragment).commit();
I would like to save the state of each fragment (and allow user in the future manually refresh recyclerView) but so far I failed. How can I make the fragment not be recreated?
Thank you in advance.
Use ViewPager with disabled swipe.
Related
I need to remove all data of Home Fragment while navigation to Gallery Fragment in Navigation Drawer activity.
My Problem is that I have to delete some value in gallery fragment but that value is loaded in home fragment. By deleting value on Gallery Fragment on a null object reference error comes in Home Fragment.
So my question is that how I can remove loaded data from Home Fragment while navigation to Gallery Fragment.
Kind of hard to answer without knowing what exactly you use for Navigation
If you don't use something like MVVM, you probably should. Navigation Component is also good.
If you are a beginner, the easiest option is to edit the method that gets called when navigating away and just set your variables to null (if within the same Fragment).
Lifecycle methods like onStop() get called in other cases too so I wouldn't use those.
If the Fragment that needs stuff deleted isn't the one executing the code, you should probably have the data outside of a Fragment (e.g. Repository, you can delete the data from it anywhere)
I have a ListFragment that has a recyclerview and onClick on any items opens the DetailsFragment. The details fragment contains another recyclerview on the bottom that shows "MORE ITEMS". Now onClick on any of these items should open the DetailsFragment for that particular item. Basically the fragment needs to be refreshed.
In the past, I would just replace fragment using fragmentManager. How do I go about refreshing the fragment? How can I create an action that points to the same fragment?
Creating an action from DetailsFragment to DetailsFragment and navigating to this action worked as mentioned by Alex
The best way to update a fragment is to use a shared viewmodel between fragments or activity/fragment.
You can read the documentation here https://developer.android.com/topic/libraries/architecture/viewmodel#sharing
Activity or Fragment A is doing some operation, at the end of it updates the viewmodel. Fragment B (that needs to be updated) is observing the same viewmodel and can update the values in the UX.
Creating an action that reloads the fragment is time consuming and not always suitable for each use case.
I want to create application which structure can be simplified as this:
Where Events tab has static content in our stackoverflow world, and is not reloading when app is working. So we can simplify it as a ViewPager.
Problem is with reloading Home tab, because when we set option in Drawer, accurate fragment should replaced Home tab.
Question
So here is my question, is it something similiar to ViewPager which replace current window from right side ? Because I replacing fragment in view pager is quit hard, so I want to replace home fragment in traditional way.
I`m not sure what your problem is. You don't want to update home fragment, you just want to replace it, so replace it and notify viewpager adapter for your change, save preview fragment before that and override back button press logic to replace again whit backuped fragment.
I am having an issue here that is driving me nuts. What I have is the following:
FragmentActivity1 holds the viewPager. It instantiates the FragmentAdapter and two fragments and attach them to the ViewPager.
Fragment1 has only one button. When user clicks this button, I create one Intent and invoke a FragmentActivty. This fragmentActivity contains a form, which the user fills in and press OK. When he does that, I persist the data in the DB of the app.
Fragment2 is a ListFragment, and lists all the data that was inserted previously by the user.
By the time user completes the form and presses OK, I persist the data, like I said and finish() the activity, returning to Fragment1. WHen I swype to Fragment2, the data is not there. I need then to swype to Frag1 and then back to Frag2. Only then I can see the problem.
I have tried setting listeners between activities and fragments but, still, cant make this work.
Have anyone seen? I am willing to share my small project as well, so you guys can take a look.
Thanks,
Felipe
I usually fill my ListView in onResume() in the ListFragment. When I'm done adding new stuff to the list (In a seperate FragmentActivity, just like you), the ListView automatically gets refreshed, because onResume() gets called ;)
Another approach is using onActivityResult(), then you can update list only when something new is added or something is removed
I really,really need a serious help over here.
I am creating an android app which uses PagerAdapter to create Fragments in an activity. Different Fragment consists of different views according to need which are created during run time. In the last fragment, I have created a sort of "Submit button", which when clicked, is supposed to get user entered values from each views(like EditText) from all Fragments.
I am using following command to get the views(int the above mentioned button clicklistener):
EditText e = (EditText) (getActivity().findViewById(i));
But its not geting the view with that ID except of last two fragments.
So, I am assuming that, it is saving the state of only last two fragments in that activity.
So, How can I save the 'view states' of all the fragments in the activity??
And Isn't it so that, when a view is created in a Fragment, and is added in its layout , that view is also added in the main activity layout?? Am I understanding it in the wrong way??Please let me know.
To simplify it, my question is:
How can we save the contents, entered by users, in dynamically created EditText in Fragments, created using the ViewPager (So that it can be accessed later)??
When you add a fragment to a FragmentPagerAdapter (assuming you haven't written a custom pager) it is added to the FragmentManager. The Fragment manager is independent of the activity lifecycle so it retains the fragment state.
If you take a look here: http://code.google.com/p/ratebeerforandroid/source/browse/trunk/JakeWharton-ActionBarSherlock/library/src/android/support/v4/app/FragmentPagerAdapter.java?spec=svn42&r=42
You can get an idea of how it works. If you want to save the fragment state for another session you'll need to pull back each fragment (getItem) and either use the onSaveInstanceState or write your own custom function
Hope that helps