Android - onAttach(Context) not called for API 23
I've updated the SDK to the latest version (API 23) and the onAttach(Activity)
method for fragment is deprecated. So instead of using that method, now I'm using onAttach(Context)
but this one is not called during the lifecycle. The activity is an instance of AppCompatActivity
from v7 and the fragment is an instance of class Fragment (android.app.Fragment
).
Any ideas how to get the onAttach
working in API 23?
Solution
I found some answers that can help you to understand and fix this problem:
Solutions:
Using getSupportFragmentManager() will force you to use a Fragment from the support lib. So the first solution is to replace all fragments with the fragment from support lib and using getSupportFragmentManager().
Solution that I've already implemented is to handle 2 possibilities (1. the app is running on a device with API < 23, the app is running on a device with API >= 23).
Shortly, in my implementation I have a base class for all fragments from the project and I added this code there:
/*
* onAttach(Context) is not called on pre API 23 versions of Android and onAttach(Activity) is deprecated
* Use onAttachToContext instead
*/
@TargetApi(23)
@Override
public final void onAttach(Context context) {
super.onAttach(context);
onAttachToContext(context);
}
/*
* Deprecated on API 23
* Use onAttachToContext instead
*/
@SuppressWarnings("deprecation")
@Override
public final void onAttach(Activity activity) {
super.onAttach(activity);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
onAttachToContext(activity);
}
}
/*
* Called when the fragment attaches to the context
*/
protected void onAttachToContext(Context context) {
}
Now I simply override the onAttachToContext(Context) method on all fragments where I need this.
I solved the problem in this way:
@TargetApi(23)
@Override public void onAttach(Context context) {
//This method avoid to call super.onAttach(context) if I'm not using api 23 or more
//if (Build.VERSION.SDK_INT >= 23) {
super.onAttach(context);
onAttachToContext(context);
//}
}
/*
* Deprecated on API 23
* Use onAttachToContext instead
*/
@SuppressWarnings("deprecation")
@Override public void onAttach(Activity activity) {
super.onAttach(activity);
if (Build.VERSION.SDK_INT < 23) {
onAttachToContext(activity);
}
}
/*
* This method will be called from one of the two previous method
*/
protected void onAttachToContext(Context context) {}
You should try using the Support library version of Fragment.
You need to switch to import android.support.v4.app.Fragment;
instead of import android.app.Fragment;
. You'll also need to make sure you're using getSupportFragmentManager()
instead of getFragmentManager()
.
pleas post about this BUG here
ISSUE OPPENED ON GOOGLE AOSP PROJECT HOMESITE:
https://code.google.com/p/android/issues/detail?id=185465
ps. this is not your job to search for solution to this problem & to find any kind of workarounds .. but to force google to fix its broken API! to do this u need to complain as i said.
related issue - getContext() from fragment:
https://code.google.com/p/android/issues/detail?id=185848