For the More Curious - Tips


I thought some tips could help, if anyone is having a hard time with the challenge question at the end of Chapter 7. There are probably several ways to do this challenge, but here is one way.

  • First, if you are going to create a higher level compatible app (API level 11, or Honeycomb on up), it’s best to create a new project entirely. There are menu selections you will choose differently as you create certain files, and you also do not want to have the same names for files, as this would be very confusing since you need to have both projects open to compare and transfer code from one to the next.

  • I just named my project and files slightly differently, so I knew what kind of file they were, i.e. instead of, I named my new class file Instead of, I used Then of course some files will be the same, like strings.xml will auto-generate as strings.xml in your new one. So be very careful when you have them open and are transferring code, to make sure you are in the right one.

  • In one way, you just have to transfer code from CriminalIntent to your new project, but you will need to be aware of a few things. I will go over these things in accordance with the tips in the book.

  • When you are creating the new project, make sure to set your minimum API level at 11, and the target and maximum levels should be defaulted, so keep those.

  • In your main file (mine was make sure to use the Activity class, so that your file reads (in my case) public class ModernCrimeActivity extends Activity { }, since we will be using a method from the Activity class, and API 11 is prepared for fragments.

  • Create all your files first, then deal with errors. In total you will need, 3 classes in your package. You will need 2 .xml files in your res/layout folder, and everything else is standard, i.e. you’ll have 1 strings.xml file. Make sure to delete your menu folder file, as instructed in the book.

  • Once all your files are created and you copy and pasted code from the old project to the new one, then tackle errors. Most of the errors will be naming differences, since you have new class names that will be scattered amongst your files, but as the old class names from the old project. So ask yourself, is this name from a class? Find it in the old project, then see what your new project class names would be as their equivalents. For instance, I renamed my CrimeFragment class as ModernFragment, so in the method in my new file, I had to change the CrimeFragment() method to ModernFragment() to make it work.

  • Save your project often (including ALL the pages) to get rid of some errors. Also update your imports, to solve some errors. Do this, by using [shift]+[command]+[O].

  • Make sure that whenever you have the chance to select some of your Fragment imports, you always select just the plain Fragment one, do not use the one from the support library which looks like import; , instead edit this import so it looks like import There are at least 2 or 3 imports you have to change in this way. I don’t think those auto-update with the keyboard shortcut, so you have to change it manually.

  • And then as stated in the book, change your getSupportFragmentManager() method to getFragmentManager(), since we are not using the older library for the API lower levels.

  • Last thing, don’t forget to change your app name, if you have a new name for your project… so in my strings.xml, I changed it to: <string name="app_name">Modern Crime Activity</string> so that I would see that name on my Android app icon when running it in a device or emulator.

And that’s pretty much it! You use all the code of the old project, since we still need to build and implement a fragment, and so mostly it’s just renaming or getting your imports straight. I am a beginner of Java, as well, so if I can do it, you can do it! :slight_smile: