I have two fragments in same activity class.
On first fragment I made a public object which have two lists inside it.
and populate using gson library from a json string.
then I showed first list on listview in first fragment.
Now I want to show second list on another fragment.
how can I get the same object in second fragment, so that I can use its second list.
Second fragment will open after clicking on a button which is in first fragment.
You probably could create whatever objects you need and are common for your fragments in your activity and access them from fragment by ((YourActivity)getActivity()).yourObject
Sorry that is not possible way you are trying to do.
Because it violates the OOPS principle that one child can not have multiple parent.
So how to do this.
1) Create a common fragment with listview.
2) From activity load data as you are loading.
3) And pass data to fragment and that you want show in first and second and so on...
4) No need of two listview
5) Just change the data for listvew
Related
It's more like a conceptual question. Actually i have three fragments let's say A,B and C all are hosted in same activity. Fragment A and B are having List. In both fragments , on click of list item i am loading Fragment C. Now how to identify whether Fragment C is loaded from Fragment A or Fragment B ? I have to make changes in Fragment C's UI when it is loaded on click of any list item in Fragment A.
You would need to detect clicks in Fragment A and B. In their OnClickListeners, you could create two different setup methods for Fragment C.
on click of list item i am loading Fragment C
Since it's you who handles the click, simply add whatever you want to make other elements recognize the source of the click. Yet, I'd still say this is wrong approach - you should not really care what fragment it was literlay but rather, assuming fragments A, B reflect different data types, what data type it was. But still - you need to add that data to your bundle (or whatever you use to pass the data to fragment C)
I have a ListView that represents a list of folders and when I click one item, I want to load another list that shows the content of this folder. How can I link these views together and to be able to go back to the first one with the back button ?
Well, since you didn't provide a code in your question, I will try giving an answer in a descriptive manner.
You can use fragments to do this. Your base Activity's layout must have a fragment container which you will use to display your fragment containing the first ListView data. Once after you click on a cell, you call the constructor of the second ListView, and replace the current content of the fragment container with the newly created fragment.
You may implement a back feature by implementing an ArrayList in your Activity and appending the fragments into that array list as the user navigates through the list. onBack pressed you can call the top most fragment from that Array list and assign it to the fragment container.
This should work well, given there are not too many types of ListViews that you may want to implement.
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.
Im trying to create a form in Android in which I have a form with list of items, these items are dynamic so you can add and remove them.
The question is regarding the adding. I allow the user to select from a list of things to add. Let say are products
so the user see in the form the products he/she has added but if the user wants to add more clickc a button and gets a screen with the products that can add to his form.
Now Im handling this with Fragments.
The form is in a fragment where we have all the logic of it, and the "add products" its another fragment but this is a list that have a checkbox on it so the user can select more than one at a time. so Im ding this with an ArrayAdapter and on the getView of the adapter we add the check listener to add or remove the item from the "main list"
This approach is giving me problems, passing the values back and fort from the "form fragment" to the "select products" fragment is not clear.
I wonder if is a better approach have the list on another activity and call startActivityForResult.
I think that way I would have better control over the list even the layout of the list because being a fragment and not a listFragment takes me out some possibilities.
I wanted to ask what would be the best practice or the better way to do this.
Create another activity and use startActivityForResult or try to do it with fragments.
and if by any chanse you have reference to some code sample that I can use as a how to.
for example gmail you get a big nice clean list where you can select more than one.. I want something like that behavior could that be in a fragment or has to be on a listFragment
The ListFragment you use to list products should propose an interface to send selected items. This interface would be implemented by activity or whatever to receive the list.
Then two ways to handle these item lists :
- Create an activity for results. That way, the activity implements the interface to return the list as result.
- Add the ListFragment to current activity. The fragment which want the item list implement the interface. The current activity gives the interface to the ListFragment.
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.