3

I am learning android development right now and i came across the fragment class.

Here I found out that we can use two imports for the Fragment Class, namely

  1. android.support.v4.app.Fragment : Can be used for API < 11 as well.
  2. android.app.Fragment : Used for API >= 11 ONLY.

However I found that when I use support fragment in an activity, it crashes when I extend the Activity class. The Support Fragment works fine when I extend FragmentActivity or ActionbarActivity.

Please help me understand why does this happen.

Ashhar Jawaid
  • 132
  • 1
  • 8
  • 1
    Its easy `ActionbarActivity` extends `FragmentActivty` which are made to deal with the `fragments` but activity is the basic kind of activity its mainly made for one view kind of things, also you don't have `getSupportFragmentManager` in the normal `Activity` which deals with the `android.support.v4.app.Fragment ` also refer to http://stackoverflow.com/questions/15318518/android-activity-vs-fragmentactivity for more info on the subject – M090009 May 19 '15 at 10:05

1 Answers1

2

You have to choose whether you use classes from the support library or not. If you do, you have to use classes that are compatible with each other. FragmentActivity and ActionBarActivity are part of the support library, hence they support android.support.v4.app.Fragment. Activity is not from the support lib, so it supports android.app.Fragment.

Basically, Activity and ActionBarActivity do the same things. There are minor differences between the 2, the main one being the method getFragmentManager() in Activity being replaced by getSupportFragmentManager() in the support library. Other methods that differ are usually prefixed with 'support' in ActionBarActivity.

2Dee
  • 8,609
  • 7
  • 42
  • 53
  • Sorry I cant upvote your answer because of low rep. Thanks for the help. Please tell me one more thing. What is better to use ? My personal opinion is that I should use the native library as mostly all the phones in the world right now are having API >=11. Please correct me if you think I am wrong. – Ashhar Jawaid May 19 '15 at 10:35
  • Glad I could help, don't worry about the vote, I don't do this for reputation ;) Which you should use mainly depends on details, for example if you need to use nested fragments, it's API 17+ or support lib. You would also have to consider your market : are your (future) users from a market where Android 2.3 still sees some use ? If so, no way around the support lib. Related links [here](https://www.google.be/search?q=support%20library%20vs%20normal). – 2Dee May 19 '15 at 12:04