Reputation: 1780
I creating my own Fragment that I wanna activate from my activity. My mainActivity file is a pure Activity (not a FragmantActivity or anything else). Why I can't use my custom Fragment as a parameter?
if (savedInstanceState == null) {
// Add the fragment on initial activity setup
myFragment = new CustomFragment();
getFragmentManager()
.beginTransaction()
.add(android.R.id.content, myFragment )
.commit();
I am getting a compile error telling me that the 2nd parameter on .add() method must be a Fragment, but 'myFragment' is a CustomFragment that derives Fragment class. So how to activate this fragment from a normal Activity?
Upvotes: 1
Views: 114
Reputation: 8956
Make sure that you're consistent with your Fragment imports. Use either import android.app.Fragment in all classes or import android.support.v4.app.Fragment; in all classes. Don't mix-and-match
FragmentActivity or any child class of FragmentActivity only can hold the fragment from the support library. Hence change your activity's parent class to FragmentActivity, it will work. As AppCompatActivity inherits from FragmentActivity it also will work.
Upvotes: 1