Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android Android Fragments Tablet Time Side by Side

Binyamin Friedman
Binyamin Friedman
14,615 Points

Why your Smells Like Bakin app might be crashing when you rotate the phone

Just wanted to point this out in case someone notices the problem.

The isTablet boolean will be set everytime in onCreate and depending on your phone screen the phone might be considered a tablet when it is on it's side. This will crash the app because MainActivity will try to find a GridFragment with the LIST_FRAGMENT tag or vise versa.

You need to make the isTablet variable set only once so put the assignment in an if block checking for savedInstanceState to be null.

1 Answer

Boban Talevski
Boban Talevski
24,793 Points

Hi Binyamin.

I noticed the issue with using the same Tag LIST_FRAGMENT for creating either an actual ListFragment or a GridFragment. But a crash shouldn't happen because of it.

The thing is, we are using the qualifier Smallest Screen Width which takes in account both the height and width of the device screen and determines the smallest value of the two when deciding whether a device is considered a tablet or not. So, rotating the device doesn't change anything when using this qualifier. Unless you manage to physically shrink or enlarge the screen while the app is running and then rotate the device. Joking of course :).

You are probably using a qualifier such as Screen Width or Screen Height which I guess does take in account the current orientation of the device and in that case, we would have the scenario you mentioned and the app will crash because of mismatching tags.

In my current version, using the Smallest Screen Width modifier, the app will just either create a ListFragment or a GridFragment (never both) and will give it the tag of LIST_FRAGMENT, which isn't a problem, since we'll never have the two different fragment types co-exist in the same app. Maybe, for better readability, we could add another tag for GRID_FRAGMENT, but it isn't necessary.

Binyamin Friedman
Binyamin Friedman
14,615 Points

Good point. My mistake :D Well, I guess that was the problem, though my answer did fix it.