I'm trying to send data to 3 other fragments in a Tab of 4 from the first Tab which gets the data from the server, but observing the creation of the Tabbed Fragments, First and Second is Initialized, so the second doesn't get the data and the third and fourth get it. Even, when I tried to load the data from onResume in the second Fragment Is there anyway I can let tell the Second Fragment should reload itself when the Tab is clicked. Any help would be appreciated
Maybe using OnPageChangeListener would when you detect when the SecondFragment becomes active.
I would generally recommend notifying such things through the parent Activity
Related
i have an Activity which haves several Fragments and am using PagerAdapter for organising this Fragments , i got to know when my activity, loads all of my Fragment's codes also gets executed even when am at first Fragment at that time my whole codes of all fragment 2 or 3 also got executed which are in the onCreateView of my Fragment so can we stop this behaviour i want to execute this codes only when user selects that particular fragment
you can limit your pager by calling this method default load number is 3
yourPager.setOffscreenPageLimit([number of pages]);
if you want just one page without preload try this link by TejjD
in my app i use SlidingTabLayout to make tabs ,my app has three tabs for each tab attach with fragment hence i have three classes extend from Fragment when Run App
show me onCreateView and OnCreate of first and second fragment created together i want to when click on second tab onCreateView and OnCreate of second tab created
because i load things from webservice when click second tab
like facebook app that happen loading things when click on tab
Looks like you're using SlidingTabLayout combined with ViewPager. ViewPager by default manages 3 items (curent one on one on the "left"/"right" side) one of which is displayed and 2 others are in idle state. You can't decrease number of such items so the only way for you is to move your web service call from onCreate/onViewCreated to some other place. For example you may call setHasOptionsMenu(true) in your second fragment and implement your web service call in onOptionsMenuCreated callback of your second fragment.
There is a predefined method called setOffscreenPageLimit in view pager,
setOffscreenPageLimit(int limit)
Set the number of pages that should be retained to either side of the current page in the view hierarchy in an idle state.
For eg.
ViewPager.setOffscreenPageLimit(3);
I have 2 viewpagers containing different fragments now I have to move to the fragment of one viewpager to fragment of another viewpager. But when I hit back the previous fragments data have to hold. The problem here is I need infinite loop navigation i.e., from fragment1 of Viewpager1 contains a textview when I click on that it has to go to details screen which is in fragment1 of viewpager2 and another textview which is in fragment1 of viewpager2 it has to go to fragment1 of viewpager1.
I have tried using stack and I can navigate but when I go to 3 or 4 levels and hit back I have lost the previous data so getting nullpointer exception.
Can anybody help me?
The way I accomplish similar setup is to use an array list to hold the data and pass it around from fragment to fragment using Intent.
Alternatively create a class on the host Activity with public access method so it can be get and set from the Fragment or better still implement a callback that updates the class on the Activity from the Fragment.
This way you can have a text view for firstname input in one fragment and each time and when a text is inputed in that text view you will update the Class.Firstname property in the Activity and the when you go to the details Fragment you can get the Class.Firstname and display it.
Thanks
Scenario:
I have a fragment which has a ViewPager which contains 5 instances(different) of a single Fragment with different values, each having a listivew that contains some sort of items.
Problem:
The flow goes smooth when I click an item of listView(say on page1) but the moment I come back from there on pressing back (overridden OnBackPressed in the main_activity (will discuss later)), the viewPager fails to load the subFragments.
But when I switch to the 2nd or 3rd page, it gets displayed, and on going back to 1st page, it now gets displayed.
OnBackPressed():
I am maintaining a manual stack here. When I click on an item of ListView, the current Fragment Instance (of the parent Fragment ofcourse) goes into stack. And when user comes back , that instance gets popped off the stack and I replaces it on the activities FrameLayout container.
References:
Fragments not getting recreated by FragmentStatePagerAdapter after coming back to the main Fragment instance
Guys, I am really pissed off here, please help me out.
Frankly, I haven't worked much with fragments.
But it seems like the page is being thrown off the memory every time you switch to other pages. I am not sure but if there is a way for android to manage this and keep the page in its memory persistently, it might solve the problem
Finally I am able to get a workaround for this issue, which included two things:
while replacing the fragment make a call to
ft.replace(R.id.content_frame, frag).addToBackStack(null);
while initiating the ViewPager, the fragment manager to be used is :
mAdapter = new myAdapter(getChildFragmentManager());
Actually the problem is that, android fails to recognise and load the older instance of the fragment unless you explicitly add that instance to the backstack. So after adding it to BackStack, there is practically no need to maintain your own manual Stack, but you still can do that.
I am building an app that consists of actionbar with tabs, and each tab consists of a fragment. One of the fragments has data that need to be loaded. So I want the data to be loaded only the first time. When the user changes the tabs and goes back to the one containing that fragment, doesn't wait for the data to be loaded again. I assume I have to use some kind of saving the data so that it can be used the next time the user goes back to that particular fragment. But I just do not know what method I should use. If someone knows or can suggest an example of saving fragment state, please let me know. Thank u!
Try this:
myFragment.setRetainInstance(true);
It'll mean the fragment isn't recreated every time it's added.