android.support.v4.app.FragmentManager OR android.app.FragmentManager?

ε祈祈猫儿з 提交于 2019-12-03 07:05:29

First of all: your activity should extend FragmentActivity.

About support libraries. They were introduced to add some functionalities to older Androids. For example Fragments were introduced in Android 3.0 (SDK nr: 11). In fact (according to documentation) in Androids 3.0 < support libary uses system implementation of Fragments.

Simply use getSupportFragmentManager(); , after you added the support library successfully.

OP was so close to having a solution that would have worked fine for API 11 and newer without needing support.v4.

He just needed to change his Fragment to also not use support.v4, in its import statement.

Summary of the two approaches. ALL your Activities and Fragments should have code that looks like exactly one of these; do not mix them! (Not all lines are needed in all files; use lines as needed.)

support-v4 approach

import android.support.v4.app.FragmentActivity;
import android.support.v4.app.Fragment;      <-- ".support.v4"
import android.support.v4.app.FragmentManager;

... MainActivity extends FragmentActivity ...

... = getSupportFragmentManager();

.... YourFragment extends Fragment ... <-- same in both approaches

API 11+ approach

import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;

... MainActivity extends Activity ...

... = getFragmentManager();

.... YourFragment extends Fragment ... <-- same in both approaches

So if you have a project that is written using one approach above, and you are integrating in code from elsewhere, be sure to look for these lines, and change them to match what you have.

suresh yadam

It's simple.

If you also want your app to run in older devices (below API level 11), use getSupportFragmentManager().

If you want your app to run in devices with API Level above 11, then use getFragmentManger().

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!