Android-API 23에 대해 onAttach (Context)가 호출되지 않음
SDK를 최신 버전 (API 23)으로 업데이트했으며 onAttach(Activity)조각 방법은 더 이상 사용되지 않습니다. 그래서 그 방법을 사용하는 대신 지금 사용하고 onAttach(Context)있지만 이것은 수명주기 동안 호출되지 않습니다. 활동은 AppCompatActivityv7의 인스턴스이고 조각은 Fragment ( android.app.Fragment) 클래스의 인스턴스입니다 .
onAttachAPI 23 에서 작업 하는 방법에 대한 아이디어가 있습니까?
해결책
이 문제를 이해하고 해결하는 데 도움이되는 몇 가지 답변을 찾았습니다.
해결책 :
getSupportFragmentManager ()를 사용하면 지원 라이브러리의 Fragment를 사용해야합니다. 따라서 첫 번째 해결책은 모든 조각을 지원 lib의 조각으로 바꾸고 getSupportFragmentManager ()를 사용하는 것입니다.
이미 구현 한 솔루션은 두 가지 가능성을 처리하는 것입니다 (1. 앱이 API <23 인 장치에서 실행되고 앱이 API> = 23 인 장치에서 실행 중).
곧 구현에서 프로젝트의 모든 조각에 대한 기본 클래스가 있으며 여기에이 코드를 추가했습니다.
/*
* 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) {
}
이제 필요한 모든 조각에서 onAttachToContext (Context) 메서드를 재정의합니다.
이 방법으로 문제를 해결했습니다.
@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) {}
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
I came up with the same issue. I have a minSdkVersion of 19 and a targetSdkVersion of 23 and I am using fragmentManager in an AppCompatActivity.
onAttach(Activity activity) is marked as deprecated but I ran into app crashes when I replaced it with onAttach(Context context).
So instead, I now have both versions present and when I run my app on API22, onAttach(Activity activity) is fired, even though it is marked as deprecated.
I have not tested it on an Android Marshmallow device, but my understanding is that it would run the onAttach(Context context) version, ignoring the other one.
Update to take into account @skaar's observation in the comments below: I could now test on API23 and can confirm that running on platform 23 causes both methods (with Activity and Context) to be called. Thanks for the heads up!
After I read your answer @David Rauca I think the second one can solve my problem. But when I look into the source code. I found that actually, onAttach(Context context) do nothing on Android M, just called the old method.
As a solution:
@SuppressWarnings("deprecation")
Just add it into the old method is the best solution.
I have encountered the same problem. What I have done to resolve it:
Change the argument type of onAttach callback from Activity to Context. For unknown reason, this modification results in the fact that the method onAttach(Context) is not called anymore during the fragment lifecycle. As a consequence, my app was crashing since the code that was in onAttach method has never been executed.
Move the code that was in onAttach method to onCreate one since it gets still executed.
With this modification, the app turns to functionate as before. No additional import statements were required.
@Override
public void onAttach(Context context) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
super.onAttach(context);
onAttachToContext(context);
} else {
Activity activity = getActivity();
super.onAttach(activity);
onAttachToContext(activity);
}
}
참고URL : https://stackoverflow.com/questions/32077086/android-onattachcontext-not-called-for-api-23
'Nice programing' 카테고리의 다른 글
| fbclid는 무엇입니까? (0) | 2020.10.30 |
|---|---|
| Android XML에서 여러 문자열을 연결하는 방법은 무엇입니까? (0) | 2020.10.30 |
| 이미지 src를 데이터 URL로 설정하면 즉시 사용할 수 있습니까? (0) | 2020.10.30 |
| app.config 파일과 XYZ.settings 파일의 차이점은 무엇입니까? (0) | 2020.10.30 |
| Java 열거 형 역방향 조회 모범 사례 (0) | 2020.10.30 |