I am new to Android and learning to create fragments in Android by following this
example: Fragment Navigation Drawer
The code between N
In Fragment it is best to use onAttach() method to get the instance of an Activity attached to it.
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
}
An Activity has no getActivity()
method.
Fragments have.
Because getActivity()
says: "return the Activity which contains me".
And while Framents are contained in Activities, Activities themselves aren't.
You can use:
this Or `MainActivity.this`
Instead of:
getActivity()