Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

android.support.v4.content.FileProvider拓展类在React Native中的执行时机 #1

Open
1uokun opened this issue Apr 17, 2020 · 0 comments
Labels
Changelog 由于版本更新造成的差异性

Comments

@1uokun
Copy link
Member

1uokun commented Apr 17, 2020

在FileProvider中获取MainApplication Context

public class FileProvider extends android.support.v4.content.FileProvider {
    @Override
    public boolean onCreate() {
        
        System.out.println(getContext());//返回com.xx.MainApplication对象

        return super.onCreate();
    }
}

React Native 0.55.4版本之前

MainApplication初始化时会执行所有AndroidManifest.xml中注册的FileProvider拓展类,
但是在0.55.4这个版本之后不会执行。

所以

不要通过FileProvider获得MainApplication对象Context

这里给了几个方案:

  1. 使用RN提供的getApplicationContext()

  2. MainApplicationonCreate中初始赋值

    在自定义package中定义init之类的方法将获得的this存储到静态变量中。

    常见于第三方UI库

    public class MainApplication extends Application implements ReactApplication {
        @Override
        public void onCreate() {
            super.onCreate();
            //add here
            CustomPackage.init(this);
        }
    }
@1uokun 1uokun added the bug Something isn't working label Apr 17, 2020
@1uokun 1uokun changed the title React Native 'java.lang.String android.content.Context.getPackageName()' on a null object reference android.support.v4.content.FileProvider拓展类在React Native中的执行时机 Apr 18, 2020
@1uokun 1uokun added Changelog 由于版本更新造成的差异性 and removed bug Something isn't working labels Apr 18, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Changelog 由于版本更新造成的差异性
Projects
None yet
Development

No branches or pull requests

1 participant