来自后台的Android应用会调用onCreate()

时间:2018-09-19 21:05:55

标签: android oncreate

我创建了一个Android TV应用程序,在onCreate()上,该应用程序处理了许多数据,这些数据需要花费几秒钟来加载。 当我按下主屏幕按钮并且应用程序进入后台时,根据我的经验,该应用程序应处于与进入后台相同的状态。 由于某种原因,帮助被破坏并再次调用onCreate,这需要花费一些时间来加载。

当应用程序进入后台时,

以下logcat会消失: 都不叫onDestroy和onRestart ...

编辑: 添加权限后,现在我得到以下日志:

09-19 16:41:07.938 3456-3456/? E/DataLoader: Exception in onStartLoading() on registering content observer for content uri: content://tvlauncher.mic/farfield_mic_status
    java.lang.SecurityException: Failed to find provider tvlauncher.mic for user 0; expected to find a valid ContentProvider for this authority
        at android.os.Parcel.readException(Parcel.java:1943)
        at android.os.Parcel.readException(Parcel.java:1889)
        at android.content.IContentService$Stub$Proxy.registerContentObserver(IContentService.java:768)
        at android.content.ContentResolver.registerContentObserver(ContentResolver.java:1924)
        at android.content.ContentResolver.registerContentObserver(ContentResolver.java:1913)
        at com.google.android.tvlauncher.data.DataLoader.onStartLoading(DataLoader.java:75)
        at android.content.Loader.startLoading(Loader.java:290)
        at android.app.LoaderManagerImpl$LoaderInfo.start(LoaderManager.java:283)
        at android.app.LoaderManagerImpl.doStart(LoaderManager.java:783)
        at android.app.Fragment.onStart(Fragment.java:1735)
        at com.google.android.tvlauncher.home.HomeFragment.onStart(HomeFragment.java:310)
        at android.app.Fragment.performStart(Fragment.java:2637)
        at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1312)
        at android.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1549)
        at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1611)
        at android.app.FragmentManagerImpl.dispatchMoveToState(FragmentManager.java:3039)
        at android.app.FragmentManagerImpl.dispatchStart(FragmentManager.java:2996)
        at android.app.FragmentController.dispatchStart(FragmentController.java:189)
        at android.app.Activity.performStart(Activity.java:6998)
        at android.app.Activity.performRestart(Activity.java:7066)
        at android.app.Activity.performResume(Activity.java:7071)
        at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3620)
        at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3685)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1643)
        at android.os.Handler.dispatchMessage(Handler.java:105)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6541)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
09-19 16:41:07.949 3456-18708/? E/ActivityThread: Failed to find provider info for tvlauncher.mic

3 个答案:

答案 0 :(得分:2)

阅读您的logcat,需要权限

java.lang.SecurityException: Permission Denial: opening provider com.google.android.katniss.search.serviceapi.KatnissVoiceInteractionContentProvider from ProcessRecord{eed2c2d 2512:com.google.android.tvlauncher/u0a23} (pid=2512, uid=10023)
requires com.google.android.assistant.READ_HOTWORD_STATE or com.google.android.assistant.WRITE_HOTWORD_STATE

添加

<uses-permission android:name="com.google.android.assistant.READ_HOTWORD_STATE" />
<uses-permission android:name="com.google.android.assistant.WRITE_HOTWORD_STATE" />

在manifest.xml文件中

答案 1 :(得分:0)

在您的AndroidManifest.xml中添加此权限:

 <uses-permission android:name="com.google.android.assistant.WRITE_HOTWORD_STATE" />

答案 2 :(得分:0)

好的,感谢:onCreate() called while Activity is stopped (but not destroyed). Only after installation

添加:

if (!isTaskRoot()) {
  finish();
  return;
} 

到onCreate()解决了这个问题!