Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: correct way of jumping between fragments

This is a design question, rather than a technical one.

General case: I want an UI event in a Fragment to make Activity-wide changes.

Specific case: I have two fragments, hosted in the same activity. When the user clicks a button in one of those fragments, I want it to be replaced by the other.

I don't want, however, my Fragments touching my activity. I may want to change the behavior later (maybe, in a bigger screen, show both fragments instead of replacing the first), and I don't want my Fragment code to have that logic.

What I did was implement a Listener class in my fragments, that reports events back to the Activity. This way, if I want to use another Activity class with different display behavior, I can just change the listener and leave the Fragment code untouched.

Is this a good way to go about it? Is there a standard good practice, or a better design pattern?

like image 633
slezica Avatar asked Sep 06 '11 15:09

slezica


People also ask

How does the lifecycle of fragments work?

Each Fragment instance has its own lifecycle. When a user navigates and interacts with your app, your fragments transition through various states in their lifecycle as they are added, removed, and enter or exit the screen.

What is the fragmentation in Android?

Fragmentation in Android means that the organization will have to deal with multiple security issues due to the difference between Android versions. This makes app management and security management complicated and tedious.

What is fragment back stack in Android?

The user can later reverse the transaction and bring back the previous fragment by pressing the Back button. If you added or removed multiple fragments within a single transaction, all of those operations are undone when the back stack is popped.


2 Answers

Using listeners is the recommended way of communicating between Fragment and your activity.

See this Android documentatin section for infromation. Long story short they just implement a listener interface by the Activity class and cast getActivity() result in a fragment to a listener.

From my personal experience this is very convenient because lets you to:

  1. Easilly switch underlying activity (e.g. you host entire fragment in a wrapper activity for compatibility in pre-3.0 and host this fragment along with others in 11+)
  2. Easilly control if the wrapper activity supports callbacks or not. Just check is it does implement the listener and do your app specific actions if it doesn't.
like image 50
EvilDuck Avatar answered Oct 18 '22 01:10

EvilDuck


You are right on about using a Listener. This is something I also had to deal with in a project at work. The best way to handle it is to make the Fragment stand-alone in nature. Anything wishing to interact with the Fragment should use its public API and/or set listeners for specific events. If you are familiar with Design Patterns, this is the Observer pattern. The events can be general or specific as well as contain data or no data.

As an example of my project, I had two Fragments. A ListFragment and an InfoFragment that displayed the selected ListItem. The ListFragment already has a Listener interface for my Activity to hook into, but the InfoFragment does not since its your basic Fragment. I added a Listener interface to the InfoFragment that would be notified when the Fragment wanted to close. For the Fragment, this could be by a button press, or specific action occured, but as far as my Activity is concerned, when the Event is triggered, it would close up the Fragment view.

Don't be afraid to use a lot of Listeners for Fragments, but also try to group them by a specific action using data parameters to individualize them. Hope this helps!

like image 2
Spidy Avatar answered Oct 18 '22 02:10

Spidy