Reputation: 107
I won't replace my fragment, but I have one problem,
when I call replace
after beginTransaction()
Android Studio doesn't give me an error.
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager
.beginTransaction().replace(R.id.l, fragment);
However, when I do something like this:
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction()
fragmentTransaction.replace(R.id.l, fragment);
Android Studio tells me that fragmentTransaction
doesn't have this method.
When I do something like this I am having error that commit()
return int
and AS proposes to transfer fragmentTransaction
to int
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager
.beginTransaction().replace(R.id.l, fragment).commit();
Upvotes: 0
Views: 1090
Reputation: 1696
FragmentManager fragmentManager = getFragmentManager(); FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction() fragmentTransaction.replace(R.id.l, fragment);
Android Studio say me that fragmentTransaction dont have this method
In the above section you are missing ;
after beginTransacton()
so please add that. Also make sure you have View R.id.l and fragment of type Fragment bcoz method definition is as follows replace(int containerViewId, Fragment fragment)
Alternatively you can use replace(int containerViewId, Fragment fragment, String tag)
For more info refer doc
Coming to other Question
FragmentManager fragmentManager = getFragmentManager(); FragmentTransaction fragmentTransaction = fragmentManager .beginTransaction().replace(R.id.l, fragment).commit();
When i do something like this i am having error that commit() return int and AS proposes to transfer fragmentTransaction to int
commit is method with FragmentTransaction who's return type is int you can refer it's signature here So you can do
int commitValue = = fragmentManager
.beginTransaction().replace(R.id.l, fragment).commit();
Also make sure you with Fragment and FragmentManager both belong to same package either are from android.app.* OR both are from android.support.v4.app.* that is support library
Upvotes: 1
Reputation: 2654
You have simply to do:
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.l, fragment);
ft.commit();
that's all. Hope it helps you!
Upvotes: 0
Reputation: 9047
In the first two snippets, you are missing the commit() on the transaction, so nothing is replaced.
The last one is mostly correct, just don't expect the method to return a FragmentTransaction. This should work:
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.l, fragment).commit();
Upvotes: 0
Reputation: 9803
Try
getFragmentManager().beginTransaction().replace(R.id.l, fragment).commit();
Upvotes: 0