Handling multiple instances of same fragment - android? - android

Scenario :
I need to create tabs/with fragment, and populate views inside each fragments dynamically. Also user can navigate inside each fragment depending on the server response.
Now what I am trying to do is setting same fragment for all tabs and passing different model data according to the tab content. Its working. When i need to show a details page inside a tab again I am showing same fragment with new data. but it makes issues (view mix up) on other tabs since the container is same.
Can anyone suggest a flow by using single fragment for any new views and i need to pop it out from backstack also.

Related

Create a form where you can have several child fragments that send data back to the main fragment Kotlin

I’m new to kotlin and I am having a hard time trying to create a form in my app. The form uses a main fragment where you can click on different attributes where it opens a new fragment, where you can insert data. When you are done filling in the data in the child fragment, I want to go back to the main fragment. I have managed to pass data between them using an interface and navigational component/safe args, and I am using a model view in the activity which holds the different fragments to manage the data. As far I’m concerned my code has turned into spaghetti code, and something tells me I’m not doing it right. What is the best way to create a form where you can have several child fragments that send data back to the main fragment, and the main fragment retains the data?
I would appreciate it if someone could point me in the right direction.

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.

Android: Different fragment for each ViewPager

In Android, when creating Action Bar Tabs with ViewPager, what's a way of giving different fragment for each ViewPager? Let's say I want the first tab to be login form, the second a signup, in fragment_login.xml and fragment_signup.xml files respectively.
Where/how do I initialize these fragments and show as appropriate tabs are selected? I would prefer to do this all in one Fragment class, instead of creating individually for each one.
If you are using a small number of fragments then you can implement FragmentPagerAdapter. For showing larger number of fragments FragmentStatePagerAdapter is recommended. You can keep all the fragments in one class(Activity class) and make each fragment a subclass of that class but I think having different classes in respective .java files would make your code more elegant.
Initializing the fragment is generally done during fragmentTransaction and appropriate data are passed via Bundle.

Handling fragment transaction in 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.

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