0

I have a ListView of people inside a Fragment and I want to show person details when clicking on a person item. Question is: What's the best practice when clicking on a list item? Is it good to open an activity or to use another fragment? In this second case it would be a fragment inside another fragment and I don't know if it is a good idea. In order to better understand my question, just open up the Mail app in Android Marshmallow and imagine (I think it's implemented like this) the list of emails are inside a fragment, when you click an email you open a fragment or activity?

Floern
  • 33,559
  • 24
  • 104
  • 119
Panda
  • 45
  • 9
  • http://stackoverflow.com/questions/20306091/dilemma-when-to-use-fragments-vs-activities – Lubomir Babev Jul 28 '16 at 14:29
  • http://simpledeveloper.com/how-to-communicate-between-fragments-and-activities/ which is referenced at http://stackoverflow.com/questions/14247954/communicating-between-a-fragment-and-an-activity-best-practices – Eenvincible Jul 28 '16 at 14:30

1 Answers1

1

Try the following pseudocode to implement your own solution:

  • Use either an interface or event inside FragmentOne.
  • Implement the interface in the Activity and Override its method
  • Inside that method, simply get the position of the selected item (pass this to the method inside first fragment) and use it to show details in FragmentTwo
  • The easiest way to do this would be to create a Bundle, set Extra as the position selected by the user, and finally, set the argumentsto the fragment and load to the view by replacing the currently displaying fragment.
  • When you load the fragment, you should let the Fragment itself load the details of the selected item. The activity will just be the mediator!

That is all you need to do this. Let me know if it helps! Good luck!

Eenvincible
  • 5,641
  • 2
  • 27
  • 46
  • I implemented exactly what you are saying, I got the logic, but then I started struggling with flexible UI patterns to properly show the fragments in different devices (https://developer.android.com/images/fundamentals/fragments.png). One of the choices of Android doc is to start another Activity when you click an item in case there is no room for another fragment. that's what's confusing me. – Panda Jul 28 '16 at 18:44
  • For tablets, you can have the list on the left and the details on the right! The easiest way to do that would be to detect of the details fragment is loaded (using findFragmentById) and if it is null, it means you are on a smaller device; others you are using a tablet – Eenvincible Jul 28 '16 at 18:46
  • ok, a simple if clause. In case you are in a smaller device then you open up a new activity containing details fragment or you replace the list fragment with details fragment? – Panda Jul 28 '16 at 18:50
  • If you are on small devices, an if statement should redirect to a details activity if details fragment is null; – Eenvincible Jul 28 '16 at 19:57