티스토리 뷰

반응형

Stack Overflow에 자주 검색, 등록되는 문제들과 제가 개발 중 찾아 본 문제들 중에서 나중에도 찾아 볼 것 같은 문제들을 정리하고 있습니다.

Stack Overflow에서 가장 먼저 확인하게 되는 가장 높은 점수를 받은 Solution과 현 시점에 도움이 될 수 있는 가장 최근에 업데이트(최소 점수 확보)된 Solution을 각각 정리하였습니다.

 

아래 word cloud를 통해 이번 포스팅의 주요 키워드를 미리 확인하세요.

Android Fragment onAttach() deprecated

Android Fragment onAttach()가 지원 중단 됨

 문제 내용 

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.

최신 지원 라이브러리(버전 23.0.0)를 사용하도록 앱을 업데이트했는데, 그들이 Fragment 클래스의 onAttach() 기능을 더 이상 지원하지 않는다는 것을 알게 되었습니다.

 

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!

API가 23 미만인 장치를 실행하면 새로운 onAttach()가 호출되지도 않습니다. 나는 이것이 그들이 의도한 것이 아니길 바란다!

 

UPDATE 2:

업데이트 2:

 

Issue has been resolved with the latest updates to the SDK.

SDK에 대한 최신 업데이트로 문제가 해결되었습니다.

 

I have tested on my API 22 device and onAttach(Context) is being called.

나는 API 22 기기에서 테스트를 했고 onAttach(Context)가 호출되고 있다.

 

Click here to follow the bug report I've opened a couple of weeks ago and the answers from the guys at Google.

몇 주 전에 열어본 버그 보고서와 구글 직원들의 답변을 보려면 여기를 클릭하세요.

 

 

 

 높은 점수를 받은 Solution 

Activity is a context so if you can simply check the context is an Activity and cast it if necessary.

액티비티는 컨텍스트이므로 단순히 컨텍스트가 액티비티인지 확인하고 필요한 경우 캐스트할 수 있습니다.
@Override
public void onAttach(Context context) {
    super.onAttach(context);

    Activity a;

    if (context instanceof Activity){
        a=(Activity) context;
    }

}

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 the Activity and the Context versions of onAttach 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.

업데이트: 일부에서는 새 컨텍스트 재정의가 호출되지 않는다고 주장합니다. 나는 몇 가지 테스트를 해 보았지만 이것이 사실이고 소스 코드에 따르면 절대로 사실이 되어서는 안 되는 시나리오를 찾을 수 없다. 내가 테스트한 모든 경우, SDK23 이전 버전과 이후 버전 모두에서 onAttach의 Activity 버전과 Context 버전이 호출되었습니다. 그렇지 않은 시나리오를 발견할 수 있다면 문제를 설명하는 샘플 프로젝트를 만들어 안드로이드 팀에 보고하는 것을 제안합니다.

 

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.

업데이트 2: 나는 안드로이드 지원 라이브러리 Fragment만 사용한다. 버그가 그곳에서 더 빨리 수정되기 때문이다. 오버라이드가 올바르게 호출되지 않는 위의 문제는 프레임워크 Fragment를 사용하는 경우에만 나타나는 것 같습니다.

 

 

 

 가장 최근 달린 Solution 

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:

대부분의 경우 onAttach(Context)에 있는 것으로 충분해 보이지만, 어떤 디바이스(즉, 샤오미 Redme Note 2)에서는 호출되지 않는 경우가 있으므로 NullPointerExceptions이 발생합니다. 따라서 안전을 위해 지원 중단된 함수도 그대로 두는 것이 좋습니다.

 

// onAttach(Activity) is necessary in some Xiaomi phones
@SuppressWarnings("deprecation")
@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    _onAttach(activity);
}

@Override
public void onAttach(Context context) {
    super.onAttach(context);
    _onAttach(context);
}

private void _onAttach(Context context) {
    // do your real stuff here
}

 

 

출처 : https://stackoverflow.com/questions/32083053/android-fragment-onattach-deprecated

반응형
댓글
공지사항
최근에 올라온 글