Creating Dynamic UI with Android Fragments gets 5-Star Review

I’m not big on self-promotion but I’ll make an exception as this is an exciting day for me…

My book Creating Dynamic UI with Android Fragments received its first user review on Amazon and its a 5-Star Review

This book is just what I was looking for. The three pieces I was having issues with were static vs. dynamic fragments, dialogue fragments and backstack management. This book covers all three of these in a manner that has really cleared up my understanding and improved my application design as a result.

Thank you to kbp135 – Here’s the link to the review.

And just in case you’re wondering … the reviewer is not someone I know 🙂

Checkout Creating Dynamic UI with Android Fragments for yourself. I’d love to hear your feedback.

Creating Dynamic UI with Fragments

 

Also checkout Jim’s Android courses at Pluralsight.

Jim's Android courses on Pluralsight

Jim’s Android courses on Pluralsight

Advertisements

Android Developers Earn More Money than iOS Developers

According to recent data, senior Android developers average $131,000 per year versus $121,000 for the same skill level on iOS.

That’s some pretty serious cash!

So what are you waiting for … clearly, it’s time to get your Android skills tuned up! 🙂

The full story is available in this Tech Week article.

Checkout Jim”s Android courses on Pluralsight.

Jim's Android courses on Pluralsight

Jim’s latest book, “Creating Dynamic UI with Android Fragments” is now available on amazon.

Creating Dynamic UI with Fragments

Android for .NET Developers Series Complete!

Part 4 of my four-part series on Android programming for developers with a .NET background just went live today.

UnderstandingTheAndroidPlatform_WithLogo

 

This course delves into the unique features of the Android platform and how those features and the related components affect the way apps are created.

I hope you’ll check out this course and the whole series if you haven’t already seen it. I think you’ll be happy you did.

Here’s the complete series

Part 1: Getting Started

Android for ,NET Developer Series: Getting Started

 

Part 2: Building Apps with Android Studio

Apps with Android Studio

 

Part 3: Adopting the Android Mindset

AndroidMindsetBanner_WithLogo

 

Part 4: Understanding the Android Platform

UnderstandingTheAndroidPlatform_WithLogo

Sign Language Interpreter at Church

Most of my posts are technical but wanted to capture a personal event I’m very excited about.

As I mention in my bio I’m on the board of my church (technically its called being “on session”) and I am super happy to announce that we’ve finally achieved something we’ve been working on for a while…

SignLanguageInterpreter_50x50

Starting this coming Sunday (October 13th) we will have an American Sign Language Interpreter present at our 11:00am services!!

We’re located in Celebration, FL which is just 3 miles from Walt Disney World.  ( here’s our link on Google Maps )

This location puts us in a great position to serve the deaf community that may be vacationing or who come down to Florida for the winter … plus our year-round community.

Our church is Community Presbyterian Church in Celebration but don’t let the name fool you … we’re one of the most denominationally mixed (including folks with no denomination) churches you’ll ever find … Everyone is welcome!

Whether you’re a deaf or hearing person, If you’re in the Disney World area, I encourage you to come by and check us out. I truly believe it is one of the friendliest, most welcoming places you’ll ever visit.

Sign_Language_Interpretation_BlackOnWhite_480x448

Android Updatable Swipe Navigation with FragmentStatePagerAdapter

Android Studio makes adding swipe navigation to your Android apps easy through the “Scrollable Tabs + Swipe” option of the Navigation Type selection in the New Project Wizard. Choosing this option works great as long as you have a static list of screens that the user “swipes” between.

3095_05_03

When Static Screen Lists Aren’t Enough

The way swipe navigation works is that each screen is represented by an instance of a Fragment-derived class. These Fragment classes are then managed by a PagerAdapter-derived class which makes them, in effect, a scrollable list. Passing that PagerAdapter to a ViewPager presents that list in a way that the user can use a swipe-motion to move between screens.

The code generated by Android Studio provides a custom PagerAdapter class that derives from  FragmentPagerAdapter. Basically all one has to do is override the getItem method and return the desired Fragment for each screen position (you’ll also need to override getCount and getPageTitle but those are super simple). What happens though is that the FragmentPagerAdapter class is designed such that once a Fragment instance is returned for a given position that Fragment is permanently in that position. Once this happens, you can, of course, make changes to the contents of the Fragment but there’s no way to provide a different Fragment instance for that position which is often what’s necessary.

The comments in the generated class indicate that using FragmentStatePagerAdapter instead of FragmentPagerAdapter as the base class allows for more dynamic management of the Fragment instances. Reading the FragmentStatePagerAdapter documentation indicates that we can notify our FragmetStatePagerAdapter instance of a change in the list of screens (in other words that we’d like to use new Fragment instances) by calling the notifyDataSetChanged method. But that’s only part of the story.

Once you call this method what you’ll normally see is that screens that were previously visited still have the old Fragment instances but screens being visited for the first time have the new Fragment instances. If you have a large number of screens and scroll back and forth between them you may see some of the older screens eventually show a new Fragment instance.

Not really the consistent user experience we’re looking for 🙂

So what’s the problem?

What’s happening is that FragmentStatePagerAdapter is trying to be efficient and only create new Fragment instances when necessary. To determine when to request new Fragment instances after a call to the notifyDataSetChanged method, FragmentStatePagerAdapter calls its getItemPosition method to see if an existing Fragment can be used in its current or possibly a different position without having to recreate it. What we have to do is tell the FragmentStatePagerAdapter instance that we don’t want to use the existing Fragment instance.

To do that we need to override getItemPosition as follows

public int getItemPosition(Object object) {
 // Causes adapter to reload all Fragments when
 // notifyDataSetChanged is called
 return POSITION_NONE;
}

By returning POSITION_NONE we’re telling the FragmentStatePagerAdapter instance to discard that Fragment and just create new ones for every screen position.

Summary: How To Create Updatable Swipe Navigation

To summarize what to do, here’s the list of steps…

  1. Select “Scrollable tabs + swipe” as the Navigation Type when generating your project in Android Studio
  2. Change the FragmentPagerAdapter base class to FragmentStatePagerAdapter
  3. Override the getItemPosition method to return POSITION_NONE
  4. Call notifyDataSetChanged in your code when you’d like to load new Fragment instances.

And with that, you have the ease of swipe navigation with the ability to reload Fragments instances as needed

Adapted from Jim’s Pluralsight course Android for .NET Developers: Adopting the Android Mindset

AndroidMindsetBanner_WithLogo

Checkout Jim’s latest book: Creating Dynamic UI with Android Fragments

Creating Dynamic UI with Fragments