0

this thread says :

FragmentActivity is for use with the backport of fragments found in the support-v4 and support-v13 libraries. The native implementation of fragments was added in API Level 11

I am using a min sdk of level 15 and I was only able to make the import of fragment work with FragmentActivity and getSupportFragmentManager, instead of extending Activity or AppCompatActivity and using getFragmentManager :

public class MainActivity extends FragmentActivity implements TaskFragment.OnFragmentInteractionListener {

private TaskFragment mTaskFragment;
private static final String TAG_TASK_FRAGMENT = "task_fragment";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    FragmentManager fm = getSupportFragmentManager();

Would someone know why, and have some details about the sdk levels that should be used with v4 or v7, or v13(?)

Thanks

Paul
  • 6,108
  • 14
  • 72
  • 128
  • If you cannot add `TaskFragment` via `getFragmentManager` it means it extends `android.support.v4.app.Fragment` instead of native `android.app.Fragment`. Therefore your activity needs to extend `FragmentActivity`. Or `AppCompatActivity` if you want that material design on all platforms. – Eugen Pechanec Apr 18 '16 at 21:42

1 Answers1

1

You should extend you activity from AppCompatActivity and use getSupportFragmentManager.

I don't know what TaskFragment is – something third party I presume.

Remember to add the Google Android support library com.android.support:appcompat to your project.

totoro
  • 2,469
  • 2
  • 19
  • 23
  • `appcompat` is part of the "Support Library", see http://developer.android.com/tools/support-library/features.html. About `FragmentActivity`: it is legacy now. It served as a predecessor to `AppCompatActivity`. – totoro Apr 18 '16 at 18:47
  • Thanks, so just to understand, why extending Activity and doing getFragmentManager() does not work, as it is said in the documentation? (http://developer.android.com/reference/android/app/Fragment.html) I am using the API 14 or 15. The error in the code says it expects the import android...v4...FragmentManager, while I am extending Activity, so I don't know why. If I add the import, the error is the same. If fragments were introduced at API 11, why do I get this error? – Paul Apr 18 '16 at 19:53
  • @totoro FragmentActivity is by no means legacy. It takes regular activity and adds support for support fragments. – Eugen Pechanec Apr 18 '16 at 21:41
  • @EugenPechanec It's not marked as deprecated or anything, but why would you use it? `AppCompatActivity` extends it. – totoro Apr 18 '16 at 21:51
  • @Paul It's past bedtime for me. I will elaborate when I awake, unless someone beats me to it :-) – totoro Apr 18 '16 at 21:51
  • @totoro I'd use it instead of `Activity` in case I only need support for support fragments but not all that material styling that `AppCompatActivity` does. Never did that so far but I'm glad I have the option. – Eugen Pechanec Apr 18 '16 at 21:54
  • @Paul The question has been raised before: http://stackoverflow.com/questions/29797172/whats-the-enhancement-of-appcompatactivity-over-actionbaractivity and http://stackoverflow.com/questions/31297246/activity-appcompatactivity-fragmentactivity-and-actionbaractivity-when-to-us to name a few. Android "Support Library" can be confusing. – totoro Apr 18 '16 at 22:01
  • @Paul Also, are you also targeting API 15? In other words: What is your min and target API? Could be you don't need the support libraries if everything you need is supported by API 15. – totoro Apr 18 '16 at 22:12
  • 1
    @totoro thanks, yes, I saw the topics, I quoted one sentence from it. So support v7 is to make an app using an API lower than api 7 to be working as another one with a higher API than API 7. Mine is 15, so extending Activity and using getFragmentManager should work... I talked about the problem in the previous comment. – Paul Apr 19 '16 at 07:54
  • @Paul The v7 is to support newer features introduced after API 7 on devices running API 7 or higher. – totoro Apr 19 '16 at 08:47
  • @Paul "Mine is 15, so extending `Activity` and using `getFragmentManager` should work" Yes, just be careful not to use anything from the support libraries. Easiest way would be to remove them. – totoro Apr 19 '16 at 17:27