如何防止 Android OS 在离开时关闭我的 React Native 共享扩展?

How to prevent Android OS from closing my React Native share extension when navigating away?

我的 React Native Android 应用程序扩展会在我离开应用程序时自动关闭(转到主页,切换应用程序)。 iOS 上不存在此行为,因此我相当确定它与我拥有的本机 Android 代码有关。

熟悉 Android 开发的人知道如何解决这个问题吗?

我查看了 savedInstanceState 并将其包含在 Activity 文件的 onCreate 方法中,但我不太确定我是否正确地 and/or 如果我需要添加其他内容。

import android.os.Bundle;
import android.view.WindowManager;

import com.facebook.react.ReactActivity;


public class ShareActivity extends ReactActivity {
    @Override
    protected String getMainComponentName() {
        // this is the name AppRegistry will use to launch the Share View
        return "Test";
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    }
}

logcat 显示了一些关于 sessionId 未被持久化和 win.mRemoveonExit=true 的信息,但没有关于这些消息的任何文档。

5205-5448/? E/CustomizedTextParser: getCustomizedText Rule is empty. mRuleMap={}
10935-11009/? E/PBSessionCacheImpl: sessionId[48094197094119864] not persisted.
1262-5461/? E/WindowManager: win=Window{8f906be u0 com.factrmobile/com.factrmobile.share.ShareActivity EXITING} destroySurfaces: appStopped=false win.mWindowRemovalAllowed=true win.mRemoveOnExit=true win.mViewVisibility=0 caller=com.android.server.wm.AppWindowToken.destroySurfaces:870 com.android.server.wm.AppWindowToken.destroySurfaces:851 com.android.server.wm.WindowState.onExitAnimationDone:5366 com.android.server.wm.-$$LambdabPtngJg5AqEoOWfW3rWfV7MH4.accept:2 java.util.ArrayList.forEach:1262 com.android.server.wm.AppWindowToken.onAnimationFinished:2403 com.android.server.wm.AppWindowToken.setVisibility:551 
5205-5205/? E/OverviewCommandHelper: No closing app

有什么建议吗?

如果有人遇到这个问题,请检查您的 AndroidManifest.xml 并查看您定义 activity 的位置。确保您没有 android:noHistory="true"。默认值为 false,因此保持原样。