I\'m learning Android development. I get stuck at something that should be very easy.
Im creating an App with one Activity, 2 fragments and 1 interface.
Simply use getSupportFragmentManager(); , after you added the support library successfully.
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.
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.
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()
.