6

I have the following scenario. A tabhost manages four ListFragmnet. For every ListFragment I have several fragment. For instance associated with the ListFragmnet 1 I have the fragments A, B, C and can happens that fragment A "launch" fragment B which can launch fragment C. Is correct to allow fragments to start each other or is there a more correct way?

Thanks

Edit:

TabFragmentActivity:
    tab1(ListFragment): fragment 1 -> fragment 2 -> .... -> fragment N
    tab2(ListFragment): fragment 1 -> fragment 2 -> .... -> fragment N
    tab3(ListFragment): fragment 1 -> fragment 2 -> .... -> fragment N
    tab4(ListFragment): fragment 1 -> fragment 2 -> .... -> fragment N

this is want to achieve. So, my question is what is the best way to manage transaction from fragment 1 to fragment N per tab?

Again thanks

Blackbelt
  • 156,034
  • 29
  • 297
  • 305
  • 2
    Possible duplicate of [How to start Fragment from Activity?](http://stackoverflow.com/questions/30412748/how-to-start-fragment-from-activity) – ItsOdi1 Jan 26 '17 at 19:15
  • @ItsOdi1, did you noticed that this question is 4 years older than the other? – Blackbelt Jan 26 '17 at 20:12
  • Yes I noticed. And I also noticed that my question has no relation to you "duplicate" – ItsOdi1 Jan 26 '17 at 20:40

1 Answers1

6

The point of having fragments is to separate the logic ! try to think of than like a pieces that doesn't know the existent of anything else except the activity in which they are placed.

how the things should works: -the activity implements interface let say that interface have method startFragmentB(); -than from the fragmentA you can do this ((myInterface)getActivity()).startFragmentB(); -all of the transactions logic should be in the activity -keep reference to all of the fragments in the activity...

this should give you a nice starting point with fragments, if you have any question just ask, i comment, as a general answer to you question:

NO it is not correct from one fragment to start other, everything should go through the activity,YES you can decide if you want to start another fragment or not but you should not do the actual starting there(in the fragment).

Lukap
  • 31,523
  • 64
  • 157
  • 244
  • so instead of managing directly ListFragment in the tabhost should I start different FragmentActivity, each for every tabhost and, using this activity to manage all the logic associated with the ListFragment? – Blackbelt Sep 23 '11 at 14:25
  • no no no :), it is not the point to have many fragmentsActivity, the point is to have one FragmentActivity and many fragments..., (one FragmentActivity per screen). Sorry for my glossary I used activity instead FragmentActivity in my answer, when I said activity I was refering to FragmentActivity – Lukap Sep 23 '11 at 14:43
  • And yes you should let the FragmentActivity to manage transactions of the fragments – Lukap Sep 23 '11 at 14:44
  • I started with this it is very useful http://android-developers.blogspot.com/2011/02/android-30-fragments-api.html – Lukap Sep 23 '11 at 15:08
  • ok.. I ve alread a FragmentActivity (the FragmentActivity which manages the tabhost) and this manage the transaction from e to 4 different ListFragment. Than, every list fragment migth launch a new Fragment and this, can launch another fragment too.. please give a hint to clear my design.. thanks :) – Blackbelt Sep 23 '11 at 15:23
  • @Lupak do u meant something like explaned here: http://stackoverflow.com/questions/6987334/tabs-in-android-using-fragments/7089151#comment-8892690 ? – Blackbelt Sep 23 '11 at 15:45
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/3749/discussion-between-blackbelt-and-lukap) – Blackbelt Sep 24 '11 at 11:22
  • can you please help me to start fragment 2 from tab1 in tab2 ? but it should be open in tab2. Is it possible?? – Akhil Dec 17 '12 at 07:18