I have updated my app to use the latest support library (version 23.0.0), I've found out that they deprecated the onAttach() function of the Fragment class.
Instead of:
onAttach (Activity activity)
It's now:
onAttach (Context context)
As my app uses the activity passed before deprecation, a possible solution i think is:
@Override
public void onAttach(Context context) {
super.onAttach(context);
activity = getActivity();
}
Would that be the correct way to do it?
UPDATE:
If i run a device with API lower than 23, the new onAttach() is not even being called. I hope that this is not what they intended to do!
UPDATE 2:
Issue has been resolved with the latest updates to the SDK.
I have tested on my API 22 device and onAttach(Context) is being called.
Click here to follow the bug report I've opened a couple of weeks ago and the answers from the guys at Google.
you are probably using
android.support.v4.app.Fragment
. For this instead ofonAttach
method, just usegetActivity()
to get theFragmentActivity
with which the fragment is associated with. Else you could useonAttach(Context context)
method.Currently from the
onAttach
Fragment code, it is not clear if theContext
is the current activity: Source CodeIf you will take a look at
getActivity
you will see the same callSo If you want to be sure that you are getting the Activity then use
getActivity()
(inonAttach
in yourFragment
) but don't forget to check fornull
because ifmHost
isnull
your activity will benull
Although it seems that in most cases it's enough to have
onAttach(Context)
, there are some phones (i.e: Xiaomi Redme Note 2) where it's not being called, thus it causes NullPointerExceptions. So to be on the safe side I suggest to leave the deprecated method as well:Activity is a context so if you can simply check the context is an Activity and cast it if necessary.
Update: Some are claiming that the new
Context
override is never called. I have done some tests and cannot find a scenario where this is true and according to the source code, it should never be true. In all cases I tested, both pre and post SDK23, both theActivity
and theContext
versions ofonAttach
were called. If you can find a scenario where this is not the case, I would suggest you create a sample project illustrating the issue and report it to the Android team.Update 2: I only ever use the Android Support Library fragments as bugs get fixed faster there. It seems the above issue where the overrides do not get called correctly only comes to light if you use the framework fragments.
The answer below is related to this deprecation warning occurring in the Fragments tutorial on the Android developer website and may not be related to the posts above.
I used this code on the tutorial lesson and it did worked.
I was worried that activity maybe null as what the documentation states.
getActivity
FragmentActivity getActivity () Return the FragmentActivity this fragment is currently associated with. May return null if the fragment is associated with a Context instead.
But the onCreate on the main_activity clearly shows that the fragment was loaded and so after this method, calling get activity from the fragment will return the main_activity class.
getSupportFragmentManager().beginTransaction() .add(R.id.fragment_container, firstFragment).commit();
I hope I am correct with this. I am an absolute newbie.