How to hold data of fragments while navigating to other screens - android

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

Related

Data pass between viewpager fragments

I am created one viewpager activity. It have three fragments. Each Fragment have one editText and last fragment have button. When I click Submit button I want to display first two fragments detail which I entered using model class.
You should store the values in the container Activity and then, update the last Fragment with the information.
Hope it helps.

I am not able to find callback in first two fragments of viewpager in activity

Scenario: I have three fragments in ViewPager in activity.
Fragment One
FragmentTwo
FragmentThree
I noticed that switching from fragment one to three onPause() method is called
but when I switched from fragment one to two onPause is not called
Problem: I want to show some data in FragmentOne's TextView with a webservice on button click.I clicked that button and data is shown But I noticed when I switch from one to two and then come back to fragment one data is shown. I want to hide that data when I come back to one. Is there any callback . ?
You need to understand Viewpager first. viewpager creates fragment in advance before and keeps so that there is smooth flow when you switch, like if you are on Fragment 2. viewpager creates 1 and 3 and keeps.
and when you are on Fragment 3 it retains Fragment 1.
Anyways you can increase the limit of viewpager with this
viewPager.setOffscreenPageLimit(int num)
you can understand all the lifecylce of fragment in Viewpager by putting Logs in onPause and onResume in viewpager fragments.
Anyways coming back to your prob. you need to have custom interface like this . answer helped me too
Its happening because Fragment One is in backstack. Dont keep FragmentOne in backstack while switching to FragmentTwo and on Back press of FrgamentTwo call switchContent for FragmentOne again .

Sharing data within Fragments in Tabs using ViewPager

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

Viewpager pass bundles between fragments

I have a custom Viewpager in which it disables the touch events. So I have buttons 'Next' and 'Back' that control the viewpager. My question is how to pass data or bundles between the fragments in the viewpager. Normally it would work but sense the fragments are created even though they are not shown. That's because of the viewpager slide effect, it has to make the fragments ahead and before for the effect to work. So that means I can't use bundles since the fragment is already created. This is what I'm trying to do
Fragment 1 -> Fragment 2 -> Fragment 3
Fragment 1 is created and so is Fragment 2. When I press 'Next' Fragment 2 is shown. I want to pass a bundle to Fragment 3 when I press 'Next' again but Fragment 3 is already created so it won't work.
Another way I thought of is to call a method in each Fragment when the Viewpager sets it as its current Item.
Why don't you create an interface that all of your fragments implement? This interface will have two methods: getParameterData() and setParameterData(). In your ViewPager, when they press next or previous, call getParameterData() on the current fragment, then call setParameterData() on the fragment to be displayed.
You could share/pass your objects between fragments by holding them in the host activity.

Android:Saving Fragment State/contents of each views in each Fragment when using ViewPager

I really,really need a serious help over here.
I am creating an android app which uses PagerAdapter to create Fragments in an activity. Different Fragment consists of different views according to need which are created during run time. In the last fragment, I have created a sort of "Submit button", which when clicked, is supposed to get user entered values from each views(like EditText) from all Fragments.
I am using following command to get the views(int the above mentioned button clicklistener):
EditText e = (EditText) (getActivity().findViewById(i));
But its not geting the view with that ID except of last two fragments.
So, I am assuming that, it is saving the state of only last two fragments in that activity.
So, How can I save the 'view states' of all the fragments in the activity??
And Isn't it so that, when a view is created in a Fragment, and is added in its layout , that view is also added in the main activity layout?? Am I understanding it in the wrong way??Please let me know.
To simplify it, my question is:
How can we save the contents, entered by users, in dynamically created EditText in Fragments, created using the ViewPager (So that it can be accessed later)??
When you add a fragment to a FragmentPagerAdapter (assuming you haven't written a custom pager) it is added to the FragmentManager. The Fragment manager is independent of the activity lifecycle so it retains the fragment state.
If you take a look here: http://code.google.com/p/ratebeerforandroid/source/browse/trunk/JakeWharton-ActionBarSherlock/library/src/android/support/v4/app/FragmentPagerAdapter.java?spec=svn42&r=42
You can get an idea of how it works. If you want to save the fragment state for another session you'll need to pull back each fragment (getItem) and either use the onSaveInstanceState or write your own custom function
Hope that helps

Categories

Resources