Prevent FragmentStatePagerAdapter to not destroy fragments - android

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.

Related

How to use an activity component in different fragments with viewpager

I have an activity with 5 fragments. i'm using different functionalities on the same component's onClick inside different fragments. because of viewpager's fragment preloading, functionality of next fragment is executed instead of current fragment. what should i do?
There is no way to avoid it because of view pager has minimim offset is 1.
So I would suggest to you using another way to archive your result:
Idea 1: If you are using FragmentStatePagerAdapter second parameter as FragmentStatePagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT, it means only one fragment inside your view pager will be in onResume state. Therefore you could start all of your networking/side effects job in onResume method.
Idea 2: Why do you using view pager, when you do not need swipe left/right effect? Probably you are in the wrong way. You could use for example just frame layout, then with your fragment manager attach and detach fragment when a user interacts how you expected. I would advise you to look better at FragmentTransaction. Here is a link.
Feel free to ask me about one of these approaches.

What is the strategy to use Fragments in Android?

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 :)

Android: Updating Fragments (that might not yet be created) from activity

Is there any way to do this?
I'm working on an application that uses multiple tabs to manage bluetooth connections, messages and other stuff.
I need my activity to inform each of these tabs (fragments) of certain events such as connection status, messages delivered.
I used an approach where i keep a reference to each of my fragments in the adapter. The problem is that fragments are not instantiated until o switch to the tab that uses it.
Is it possible to instantiate all four fragments so that i can update their view even though it has not been shown yet?
Thanks a lot,
and sorry for my english
If you use a ViewPager (you mentioned "tabs") just call setOffscreenPageLimit() in onCreate() of your App (after initializing ViewPager). Set the limit to the number of your fragments. With this all of your fragments will be instantiate.
I would suggest using frames, and then populating each one with a fragment when the time is right. When you get to the correct tab, do a null check to see if the frame is actually a part of the layout. If it is present
then place the fragment in the frame. That should do it.

Switching between two ViewPager contents

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.

Suggested way to retrieve Fragment managed by Adapter

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.

Categories

Resources