0

I am referring the big nerd ranch android book to learn android.

When I reached chapter 7 which starts explaining fragments, It said that if I wanted my app to support devices with API < 11, I needed to use Fragments of android.support.v4 library and have my classes extend FragmentActivity. But I decided to use Fragment in the default library and have my classes extend Activity... hope you get what I mean to say.

Then I reached chapter 13 which explains toolbar, it said that toolbar was introduced in Android 5.0, so here I decided to use the AppCompat library and have my classes extend AppCompatActivity (which ultimately extends FragmentActivity).

When I extended my classes with AppCompatActivity, I immediately got errors like: cannot resolve method getActivity(), setArguments(), getArguments()

So does that mean I have to use android.support.v4.library Fragment if I use AppCompat?? Since I am a beginner, I have a few more questions...

I don't think I'll ever have my apps support API < 16... so is this approach (use android.support.v4.library Fragment if I use AppCompat) recommended for long term use... or large projects? Or is there a better way, or a workaround?

Jeet Parekh
  • 740
  • 2
  • 8
  • 25

1 Answers1

2

So does that mean I have to use android.support.v4.library Fragment if I use AppCompat??

No you don't. You can still use the native fragment support even though you are extending AppCompatActivity. You will use getFragmentManager() instead of getSupportFragmentManager(). Still I strongly recommend that you use the support library support. With every update of the support library you get bugfixing and potentially new functionality, mostly available also on old versions of Android, that you otherwise can't use

Blackbelt
  • 156,034
  • 29
  • 297
  • 305
  • I have never used the support library. And surprisingly I don't have an error in `getFragmentManager()`. When I hover over an error, say `return new CrimeListFragment()`, it says required: `android.app.Fragment`, found `packagename.CrimeListFragment`... and here `CrimeListFragment` extends `AppCompatActivity`, do you know what this means? – Jeet Parekh Dec 13 '15 at 14:19
  • it means that CrimeListFragment is importing Fragment from the support library, but since you are using getFragmentManager, he expects Fragment from the native support. Hence the incompatibility. Use getSupportFragmentManager instead of getFragmentManager – Blackbelt Dec 13 '15 at 14:21
  • oh, that raises another confusion... while following the steps of the book, I took extra care that I do not use the support library, however I did add that into my dependencies... but I used getFragmentManager() all the way till the beginning of chapter 13 i.e. Toolbar and it worked perfectly... these errors came up just after I extended AppCompatActivity (which is just the 2nd page of chapter 13) ----- so is it, or is it not using Fragment of support? – Jeet Parekh Dec 13 '15 at 14:29
  • `AppCompatActivity` is a `FragmentActivity` which is an `Activity`. A `FragmentActivity` can use both, support and not support library for fragments. If you have compile time errors, Fragment related, it is probably because you have a mismatch in in your imports and type of Fragment manager you are using. Honestly I wouldn't spend much time on this topic and use always the support library – Blackbelt Dec 13 '15 at 14:32
  • 1
    I just realized that I was making 3-4 of my classes extend AppCompatActivity... but only 2 classes were needed to do it. Everything's working perfectly now – Jeet Parekh Dec 13 '15 at 16:59