Recycler view goes null on switching fragment - android

I am using a recycler view to show some items, everything works fine but when I open another fragment and then reopen the fragment having recyclerview. it doesn't show anything.
Can somebody tell me what is its cause?

You should have a look at the lifecycle of a fragment.
It seems your code for filling the recyclerview with data is in the wrong method.
Lifecycle methods like onCreate(), onCreateView() etc. are just called on creating the fragment.
if you leave the fragment (and do not finish() it by yourself), it won't be destroyed so these methods are not called again.
You need to fill your recycler view in onResume() method

Related

Restore RecyclerView's scroll position after performing Fragment replace()

I have a simple activity with two fragment: main (contains a list of items) and detail. Details screen is a fragment, which is displayed when an item is clicked in main list. DetailFragment is being shown using replace().
After navigating back from detail screen I would like to have exact same scroll position in RecyclerView that I left it with.
As long as replace() initiates onDestroyView() and onCreateView() cycle to happen for MainFragment, after coming back from DetailFragment the RecyclerView has a scroll position of 0 (at the top), because this is a brand new RecyclerView which has no connection to the one that was there before leaving to DetailFragment.
replace()ing fragment does not initiate onSaveInstanceState() to be called, that's why using techniques outlined here are not applicable.
I wonder what is the correct way of handling this use-case?
Of course I can save the position on onDestroyView() and later manually scroll to that position, but maybe I'm missing some obvious solution?
An answer, that will propose to use add() instead of replace() is not welcome.
I've had RecyclerView declared as a child of NestedScrollView. This was the issue.
When I got rid of NestedScrollView everything was handled automatically.
As onSaveInstanceState() is not possible, try using ViewModel to save the required data.
When you declare and set the adapter to your first recyclerview, try having a call back to your actvity passing the adapter instance , where in your activity you can try saving the adapter instance or any required data in the ViewModel of your activity.
Once you come back from your details fragment you can get the saved instance of your reycycler data from the activity`s ViewModel.

Where should I code in Fragment that it will run only once- android

I am implementing Tab Layout with Swipeable Views in android. For implementation I had followed
AndroidHive
and
Tabs and swipe views.
But with both, I am facing the same problem. I am having 3 fragments but when my application run, onCreateView of 1st and 2nd Fragment called instead of only of 1st fragment's. When I swipe and go to 2nd fragment, onCreateView of 3rd Fragments get called.
So, whatever I code in 2nd fragment execute in the first fragment view. I researched and came to know that it happen to keep the next fragment into memory for smooth animation. But I am wondering, where I would code in the fragment so that it will execute only once or how to restrict Fragment getItem() method to be called only once. What can be the solution for this?
According to the Fragment's documentation you can implement the onCreate() link:
The system calls this when creating the fragment. Within your implementation, you should initialize essential components of the fragment that you want to retain when the fragment is paused or stopped, then resumed.
This should fix your problem, because i guess you now only use the onCreateView which might be called more than once.
For more information you can check the Fragment's lifecycle or documentation

View pager with fragments - showing dialog onStart

I'm new in using view pager, I've done everything correctly, I conveted my activities into fragments and put them in FragmentPagerStateAdapter.
When testing it in the emulator, what I noticed is that the onCtreate to the onResume are called one fragment before the actual visibility on the view pager - it makes sense, the device wants to get ready for the next step, but the problem is that I have one fragment which calls a dialog every onStart so that actually happens on the wrong fragment.
What should be done?
You can use an OnPageChangeListener on your ViewPager to detect when you navigate to the right fragment.

Fragment basics

I am working on a application for which I have some issues with fragments. First of all , I want to know if it is possible to make one fragment inside another fragment. And the second one is when to call onCreateView() and onActivityCreated() and which is best?
Per the Android sources:
onCreateView is called by the Activity during the construction of the view hierarchy. This is where the Fragment has the opportunity to instantiate its own user interface view.
onActivityCreated is called when the Activity has been fully created and finished instantiating the view hierarchy. At this point it's safe for the Fragment to access the its views and restore itself from some saved state.
You cannot create a fragment inside another fragment. You should communicate between fragments thru activity.See http://developer.android.com/guide/components/fragments.html#CommunicatingWithActivity
I didn't understand your second question clearly. But I generally leave onActivityCreated() blank without changing it. And use onCreateView() in a similar way with onCreate().

Fragment LIfecycle in Custom Tab View

I have a custom widget that performs FragmentTransaction.replace when buttons are pressed. Currently, my code is set up such that the first time a fragment is created, it attaches a bunch of stuff to the view that isn't originally part of the xml layout file.
When the app first launches, all my fragments show stuff correctly, however, let's say I start on Fragment A. I can then transition to Fragment B (with B showing up correctly), however, when I transition back to Fragment A, all the stuff I have attached to the view of Fragment A is now gone. I know this happens because onCreateView is called which probably means the Fragment's view is re-generated when FragmentTransaction.replace is called.
Is there a way where I can keep my fragments around instead of having them re-generate their views when FragmentTransaction.replace is called?
Thanks!
Instead of using fragmentTransaction.replace, use fragmentTransaction.show and fragmentTransaction.hide.
That will keep your fragments from being destroyed.

Categories

Resources