I have fragments in a FragmentActivity, that has an ActionBar and Tab navigation. My problem is that I want to call a method on the Fragment. I cannot use findFragmentBy[Id|Tag], because the fragments are initiated and attached by the adapter, and it assigns a custom tag. I saw several solution based on keeping references to fragments, but I really want to avoid keeping any reference to my fragments, as they are handled by the FragmentPagerAdapter. Now I have everything in my Fragment (which is a workaround in my view), but the question still bothers me.
What is the best way to access a fragment instance from my activity, not added by my to FragmentManager, but the FragmentPagerAdapter?
I solved this problem by writing my own Adapter. Since they are pretty straightforward, they are easy to be implemented.
Related
As the title suggests, I want to retain my fragments. I have enforced my app orientation to always be in landscape mode in the manifest file so that there will be no rotations.
I have read:
Understanding Fragment's setRetainInstance(boolean)
and
Why use Fragment#setRetainInstance(boolean)?
However, I am not sure if they apply to my situation.
My project consists of a ViewPager with swipe tabs. How can I ensure that the fragments used in the ViewPager are the same ones as created when the MainActivity first starts? Would I use the same tagging method and findfragment by tag?
Additionally, where would it be best to check for the tagged fragment, else create a new fragment?
Just a side question related to ViewPager: what can I do to immediately create all fragments used in the ViewPager when the mainactivity is started, rather than to wait for a swipe event to occur?
To answer your last question view pager will by default create the fragments around the current fragment so you don't need to worry about that part. What I would recommend is let the view pager manage your fragments for you rather changing the behaviour since you might face performance issues.
SetRetainInstance simply keeps the instance of your fragment when its detached so it's up to you to assess whether you need to use it or not.. is there anything you want to maintain about that fragment? if not then do not use it, free your memory as much as possible.
For the last question, why do you want the same fragments created from the first time to be retained ? all the time? if you NEED to do that then rethink your structuring. gracefully recreate your fragments and maybe have some caching of your data on another layer if that is what you are worried about.
I have an Activity which uses several Fragments. One of my fragments have an ExpandableListView which uses an Adapter of self made. What is the good strategy to keep(or should I keep) and recreate/retain when screen orientation changes:
reference of the Fragment in Activity
ExpandableListView in the fragment
ExpandableListView's states (such as collapsed/opened groups)
What I am currently doing is: reCreating the Fragment,Adapter, ExpandableListView inside Fragment each and everytime. (Not keeping states yet, but I guess I will use SharedPreferences).
PS: to minus voters: please elaborate what I am doing wrong. Apparently I am missing some points.
If you don't have nested fragments, you can use setRetainInstance(true). Or you can override onSaveInstanceState(Bundle) and save all your data, that you'll repopulate in onCreate / onCreateView. Note that the data has to be Serializable or Parcelable (preferred).
I think the downvote is because this question has been asked 10000 times :)
I have some Fragments in a List Collection to be used for a Viewpager with a FragmentStatePagerAdapter. Im using only getItem(){return list.get(index;} and getCount(){return list.size()} in it. If i wanna replace that List in the adapter, i load another List with different Fragments, call notifyDataSetChanged() and set the adapter to the viewpager again to make it happen.
That is workin fine.
But if i do this fragmentset change the previous Fragments are gettin destroyed with a call of onDestroy() through the PagerAdapter. Now i would like to know if its possible to prevent the onDestroy call? Or i HAVE TO use onSavedInstanceState() to load the previous Fragments and their state again?
Im just searchin some different methods to restore the state of the fragments which are gone because of the change in the adapter. If anyone know something about this it would be nice to share some thoughts. I can give code if its necessary.
Maybe a good Info: I dont use the PagerAdapter to instantiate the Fragments. I do this before i load the Lists to the Adapter.
Now i would like to know if its possible to prevent the onDestroy call?
The complete and entire point of using FragmentStatePagerAdapter is to allow Android to destroy fragments that are not in use. You should be using FragmentPagerAdapter instead.
I suggested same problem the ather day.
so I found another solution.
I override destroyItem(ViewGroup container, int position, Object object) in Adapter class.
and changed not working super.destroyItem ..
I'm not sure that solution is better solution, but it's definitely help for me.
In my app I have the dropdown enabled in the ActionBar. The user has two elements to choose from. Depending on the choice I want a ViewPager to show different contents from different FragmentPagerAdapters. The user has to have the ability to switch all the time.
I've tried to set two different PagerAdapters in the listener with no luck. The Pager would just reload the previous fragments every time. Similar problems are described here and here. However in my approach I don't want to change the content of one adapter. I really just want to switch between two separate adapters.
This whole thing seems really confusing to me. Are there any known workaround for this or is there an alternative solution to my problem?
If it is possible for your application, I would suggest using FragmentStatePagerAdapter instead of FragmentPagerAdapter. Each adapter should be able to store a separate list of fragments, and should keep the saved state of your fragments when you switch to the other adapter.
I've tried to set two different PagerAdapters in the listener with no luck. The Pager would just reload the previous fragments every time.
I am assuming that you are using FragmentPagerAdapter or FragmentStatePagerAdapter. Both of those store their fragments by tag name in the FragmentManager, and therefore this switching approach will not work.
Are there any known workaround for this or is there an alternative solution to my problem?
One approach would be to fork FragmentPagerAdapter or FragmentStatePagerAdapter, replacing the makeFragmentName() method to use an alternative tag syntax, and use the revised adapter class for one of your two.
I am trying to make a sudoku application. It's a fragment based design, in which a fragment hosts a custom view which is a board. I am trying to learn how to build an effective communication within FragmentActivity, Fragment and View
Although a view is created using the FragmentActivity context and I can catch a reference to that context within the current view and then call methods inside FragmentActivity I don't want to tie views so directly to a fragment activity. Instead I want to tie the view to use methods inside a fragment. How can i do that, I can I capture a reference to a fragment and call methods inside that fragment from a view?
First of all, I strongly recommend that you read through the documentation on Fragments, as you clearly don't understand the whole concept/purpose of using Fragments in the first place (which is OK, because they are confusing the first time you learn them :P).
A lot of your question doesn't make very much sense to me, because I'm not sure what the View you are speaking of refers to. What I can tell you is that Fragments have their own UI/layout, along with their own separate lifecycle. So it sounds like you don't want your FragmentActivity to interact with the Fragments layouts/methods at all... instead, you should implement the UI's behavior and layout inside of the Fragment itself. That is, the Fragment will be in charge of updating the UI, receiving click/touch events, displaying information on the screen etc. The FragmentActivity will simply hold a reference to the current Fragment(s), and will be in charge of displaying/swapping in and out new Fragments as necessary (via the activity's FragmentManager).
Hope that answers the question somewhat... read through the documentation a couple times, it sounds like you are just misunderstanding the theory/purpose behind Fragments.