티스토리 뷰

React Native를 사용하여 모바일 앱을 개발 중에, 외부 라이브러리를 사용할 일이 있어서 안드로이드 React Native Module을 만들 던 중 에러가 발생하였다.

외부 라이브러리에서 자체적인 UI를 띄우기 위해 Activity 객체를 필요로 하였다. 해당 파라미터 위치에 reactApplicationContext.baseContext와 getReactApplicationContext()를 캐스팅해 보았지만,  MainApplication cannot be cast to android.app.Activity라는 에러를 만났다. 

에러 발생 로그

 

해결방안

리액트 네이티브에서는 MainActivity 단 하나의 Activity가 있으며, 다음과 같이 MainActivity에서 static 함수를 만들어 activity를 반환해 주어야 한다.

public class MainActivity extends ReactActivity {

    private static Activity mCurrentActivity = null;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mCurrentActivity = this;
        ... 
    }
    
    ...
    
    public static Activity getActivity(){
        Activity activity = new Activity();
        activity = mCurrentActivity;
        return activity;
    }  
 }

 

리액트 네이티브 모듈 안에서는 MainActivity.getActivity()라고 호출하면 Activity를  정상적으로 가져올 수 있다.

 

[ 참고 자료 ]

 

How to access Activity from a React Native Android module?

I'm attempting to bridge over the Android functionality of keeping the screen on to React Native. I figured I could do this with a simple module, however I don't know how to get access to the current Android Activity from said module. I need the Activity r

www.py4u.net

 

댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함