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)
Related
I have two fragments A and B.
A is a form which contains EditTexts and one value need to be selected from fragment B.
When using navigation component after selecting item from B and when conditionally navigating to A , EditTexts values are gone. The fragment is recreated. How to prevent this?
My intention is to get value from B to A without recreating A
Without seeing your code its difficult to give you a straight answer, but here are some general ideas
1) Add fragment A to the backstack this will persist it when you return from Fragment B
2) Find fragment A by its tag and if it exists do not create a new Fragment A when returning to it
My situation is like this I have an activity home activity inside it there are three buttons to switch and a container. In each button click I have to replace the container with fragment say A,B,C. In these fragment have a common container which will be replaced by fragments X,Y.
so my problem is that when I want to display data inside say X fragment I want to know which is the parent fragment A or B or C. So how to make this problem solved
Thanks
if you are a simple record data, you can remember a variable only when switch(replase) fragment
Right now I've Fragments A with Recyclerview - where I've categories (Image+text).
I want to make Fragment B with Recyclerview - where I've types (Image+text). Same layout, same everything except text/image.
Like this:
https://img.exs.lv/e/z/ezeliitis/frags.png
For instance, I click on Fragments A - first picture (Cars) and it opens Fragments B - in same layout as fragment A, which contains (AUDI, BMW, OPEL ect...). Should I just make copies of fragment A (adapters/viewholders ect.) changing db names/pictures or is there some way to "DRY" the code? Also, isn't it bad having two recyclerviews (performance) ?
Also, movement from one fragment to another is called fragments "..."(what exactly?)
Same layout, same everything except text/image
You must need to replace the RecyclerView adapter, then. No need to start another Fragment, but you're more than welcome to.
isn't it bad having two recyclerviews (performance) ?
Not that I know of. You'd only have one at a time, from what I understand anyways.
movement from one fragment to another is called fragments "..."(what exactly?)
If you do need to switch Fragments, then you want the FragmentTransaction class of the host Activity. That's how you switch. Documentation is pretty good with its example.
https://developer.android.com/training/basics/fragments/communicating.html
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
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