How to reuse of fragment with multi-tabs - android

I have developed project using Fragments and Tabs at Bottom,
I use just one activity and all fragemnts and tab click is managed by tabHost and stack of fragment
I use this code github link fragment_tab_study for setting tabs and maintaining stack of fragment
Now the issue is that we just push fragment(i.e replace fragment) so every time it call its onCreateView and all other method, I think this is a very big issue in this demo, as we click second time on tab1 it should only call its onResume method as happen in activity
is any solution for this ?

If you use replace of then in any case you have to start with onCreateView. If you dont want to recycle you can choose add method of transaction. But to replace is better in fragment terminology. You can even save transaction between fragment in back stack.
Please let me know if you have some other concern regarding fragment.
Thanks
Ankur

Related

Android Fragments switch without destroy

I have an activity with 3 fragments. Every fragment has a question.
When i create the activity I'm calling .add(...) method for the first fragment. For the others i call .replace(...) method with addToBackStack(...) method.
So i have this situation in the backstack:
.add(Frag1)
--> Frag 1
.replace(..., Frag2, ...)
-->Frag2
-->Frag1
.replace(..., Frag3, ...)
--> Frag3
--> Frag2
--> Frag1
Now, when I'm at the third fragment and then go back I'm popping it from the stack. I want to preserve the third fragment so if i go back to the second and then go next to the third i want to have the question answered and not to call again another onCreate().
I want to create a sort of switch of the fragments. Is it possible? Can someone help me?
You can create a fragment once and Show/Hide it depending on your needs.
See hide & show from FragmentTransaction docs
Also see this question
How about you use viewpager instead, you will need to:
Disable paging by swipe, so you can change the page programmatically
Load all pages at once (set setOffscreenPageLimit your pages count), so that each page keeps its state

How to create fragment dynamically in viewpager

when we use fragment in viewpager, we alway add fragment when adapter call the constructor.so when the actvitiy call onCreate(), all the fragment would construct and call onCreate() at the same time.
but I want to construct the fragment when we slide in.what should I do?
For example:
I have 4 fragment: fragment1, fragmeng2, fragment3, fragment4.
at first, the viewpager would create fragment1, and when we slide into next fragment(right slip), the viewpager would create fragment2, and the fragment2 would call it onCreate().
How can I make it?
Thanks a lot for your help,so sorry about my pool English and I don't know have I explain the problem clarify.
Thanks again.
You can just move the creation of the fragment to the getItem() method in your adapter and return the newly created fragment.
My answer might be a bit lacking but I'm sure someone can complete it.
So what you can do is just create one fragment in the onCreate() of the activity. Implement the function onScrollChanged() and try to keep track there with getScrollX() where the user is exactly is in relation to the screen and just make some "if" statements that removes/adds the fragments as you are scrolling to the side.
As you have noted, viewpager actually loads the fragments at once (onCreate and onResume). If your intention is to do something before the next page is displayed, this link might give you an alternate way of doing this.
https://looksok.wordpress.com/2013/11/02/viewpager-with-detailed-fragment-lifecycle-onresumefragment-including-source-code/

Navigate up to last shown fragment

I've implemented a NavigationDrawer within the MainActivity which containts two Fragment. The second Fragment contains a ListView that opens a new Activity (onItemClick) that shows the according detailed data. (I guess that's pretty much the master/detail flow).
When I use the up button to navigate back I got shown the first fragment and not the second fragment with the ListView.
Any ideas how to solve that problem?
Make method in MainActivity for example setFragment(int whichFragment);
and set fragment you want in it, you should already have code that do that and than call that method in onBackPressed() method.
For your question about another fragment, well it depends on how your master/detail flow is suppose to work, it is not a problem to use another activity if you dont need left menu any more, but if you do need left menu then use another fragment.
Best regards

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

FragmentStatePagerAdapter fails to load the fragments again when I get back to old instance of the parent Fragment

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.

Categories

Resources