Handling fragment transaction in android. - android

Hi I am developing android application in which I am using one activity and two fragments. Consider same example which google explain like one list view and detail view. on click of list item we are rendering respective detail fragment.
So I learn how to do fragment transaction and i come up with two solutions. One which is standard way which google explain that make one interface and implement that interface into main activity. And do fragment transaction there inside main activity.
I tried with another way. when I click on list item inside click listener instead of calling interface I change fragment inside my list fragment only and its working fine.
So i want to know what is difference between those to methods. changing fragment from main activity and changing it from fragment only.
What kind of problem i will face if i implement with second method.i.e. changing from fragment only.
Need Help. Thank you.

What kind of problem i will face if i implement with second
method.i.e. changing from fragment only.
There isn't an actual problem, it's more of a design discussion. Using the second approach means you're making a very specific fragment, one that on a click on one of its rows will make a transaction with a specific fragment on a specific place of the holder activity. This would be a problem if you plan on reusing this fragment.
Suppose you have this ListFragment and you decided that it should be used in five other activities(with different data). Because it has a very precise action when clicking one of its rows, this fragment will always require the holder activity to have a specific container(where the transaction will be done) along the specific detail fragment with which it was initially used. The problem is that in each of those five activities you may want to use a different fragment when clicking a row of the ListFragment, this would require making changes to the class of the ListFragment.
Now suppose you have the same behavior with the interface approach. As the ListFragment doesn't know or care who handles that click event(as it passes it to whoever registers as the listener) you can simply put the ListFragment in the five activities with no problem(so no changes to it at all). In the interface method of the activity you could then implement the desired behavior with whatever fragment you want and in whatever container setup you want.

Related

Android: Viewpager with multiple fragments of the same class with fragment transaction

There are already some open threads about this subject but mine is a little different and I can't seem to find out how to do it. This is my problem:
I'm creating a listview (recyclerview) inside a fragment which I put inside my Viewpager. This listview represent a timeline/agenda of the current day. Whenever the user swipe left or right, the exact same listview fragment needs to pop up with different data (data from the day before or after). Whenever you click on one of the items in the listview, the fragment is replaced by a detailed fragment.
This doesn't sounds so hard but here comes the tricky part:
All the examples I see on the internet show different Fragment classes inside the Viewpager which makes it possible to do a transaction from the listfragment to the detailed fragment, and all the examples that use multiple fragments of the same class don't do a transaction to a detailed fragment.
Whenever I'm trying to do the transaction to the detailed fragment, it looks like it can't find the proper fragment container (since there are 3 of the same now). I'm pretty sure of it since the new fragment does appear but the old fragment stays visibile underneath (when I don't use the viewpager it works fine).
If anyone could provide me with a simple example of a viewpager with multiple fragments of the same class that have a transaction to a detailed fragment you would save my day.
Regards

use of multiple fragments inside a fragment in android

I have a requirement in my application. my application has 6 fragments in a Navigation drawer each fragment represents single item in navigation drawer list.
But in one of the fragment i need to implement a form which will be spread across 5 views one at a time, can any one suggest me how to approach this in android.
Initially i thought of doing this using ViewPager , but i dont need horizontal swiping but need to go to next screen when clicked on a button.
Kindly advice me how to approach this.
Thanks & Regards.
Nagendra
You can create main fragment and sub-fragments. Inside the main fragment, show the sub-fragments by replacing them, store the data by creating an entity object here and pass it to the sub-fragments. Inside sub-fragments, pass the data to the main fragment entity using listeners.

how to update a fragment from another fragment

I have fragemnt in which several buttons layaut and picture. In creating this fragment on top of the loaded data. in the lower part of the site for other fragments. One of the fragments contains a list. When I click on an item list, I need to update data on a fragment that contains this fragment. This hour I just create a new fragment the parent with the new parameters derived from the list. But I think it's not right. tell me how to update the fragment.ie I update the data and set to the desired form elements, but how to update the GUI itself
example may be easier
Using Interfaces for InterFragment communication should do the job for you.
https://www.youtube.com/watch?v=VyyGP_d0Ia8&index=8&list=PLonJJ3BVjZW4lMlpHgL7UNQSGMERcDzHo
Look into the below video, It might help you.
You should add interfaces to the fragment and implement them in your activity. Interfaces are good way to communicate between fragments. Take a look at here. In the implemented interface write code appropriately to change other fragments.

Implement Tabs with multiple fragments in each tab with seperate back stack

I want to use multiple fragments in each tab of Tab Host.
I am googling for last 5 days but nothing is working in my case. I got a good working solution
Seperate Back Stack and Sample Project Here .This maintain a separate Custom Back Stack for each tab having lots of fragments and store Fragment object in Custom Stack. But when ever I want to re-add any fragment that I already created and stored in its custom stack(as an object), all it's life cycle methods are called once again as are called first time. This is the problem. In this case all the views of fragment layout are recreated and it behaves like a new fragment.
I want to implement functionality like tabs having activity group(in which lots of activities are combined in a single tab using activity group) with the help of fragments.
Please help me in solving this issue........

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