[TUTORIAL] Working with Fragments in a ViewPager



Hi to all, it’s time to post the first android tutorial in this blog and this time  I want to talk about the comunication mechanism between Fragments (you can find my other tutorials at alchemiaandroid.altervista.org).

In this tutorial we’ll focused on a communication between two Fragment (one in the role of sender and one in the role of receiver) across a FragmentActivity, that’ll work as container and channel between them.

You’ll find the project as attachment.

Difficulty level: 3/5

Terms and Documentation that you have to know:

  • Fragment: it’s a piece of code and layout that works as it is inside a container
  • FragmentActivity: it’s a container that can ingest fragments
  • MessageLoader: in this project it’s an interface that defines a method to send a message between fragments
  • ViewPager: it’s a laoyut manager that allows user to flip with his finger between “pages”
  • FragmentPagerAdapter: it’s the adapter that’ll we associate to our ViewPager


We want to create a communication channel between two Fragments allowing our developer to pass information between them.

  • FirstFragment: it’s our receiver; it has a TextView that contains received message and a Button to reset the view.
  • SecondFragmet: it’s our sender; it has an EditText to digit our message and a Button to send it.

This goals will satisfied creating a sort of Publisher and Subscriber model between them.


  1. Our ViewPagerActivity eats our Fragments and choses to visualize the sender as first visible fragment
  2. The FirstFragment hasn’t a message in its TextView
  3. You need to flip, with your finger or sender button, to SecondFragment
  4. Here you can write a simple message
  5. The sent message will arrive to our FirstFragment travelling through the container

Download the project related to this tutorial (link).

What do you think about this tutorial? It’s useful to solve your problems and/or curiosities?

See you next time,



About alchemiasoft

Android Developer at Bloomberg LP. Algorithms and performance passionate. Curious. Eager to learn.

Posted on February 15, 2012, in Android and tagged , , , , , . Bookmark the permalink. 6 Comments.

  1. I simply want to say I’m all new to weblog and seriously enjoyed your web site. Very likely I’m going to bookmark your site . You amazingly come with amazing article content. Regards for sharing with us your blog.

  2. Enjoyed studying this, very good stuff, thanks . “All things are difficult before they are easy.” by John Norley.

  3. Did you know you can watch true blood season 5 episode 1 online? it’s crazy!

  4. Thank you for sharing excellent informations. Your web-site is so cool. I’m impressed by the details that you¡¦ve on this blog. It reveals how nicely you perceive this subject. Bookmarked this web page, will come back for extra articles. You, my friend, ROCK! I found just the information I already searched everywhere and simply couldn’t come across. What an ideal web site.

  5. I visited a lot of website but I think this one has got something extra in it. “Acceptance of what has happened is the first step to overcoming the consequences of any misfortune.” by William James.

  1. Pingback: Tutorial android - Pagina 4 - Forum Android Italiano

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: