Android app calls onCreate() when coming from background

痴心易碎 提交于 2021-02-10 19:31:55

问题


I created an Android TV app, on the onCreate() the app handles a lot of data which take it few seconds to load up. When I press the home button and the app goes to background, from my experince the app should come up in the same state as it went to background. for some reason the helps get destroyed and onCreate called again which takes a bit of time to load.

the following logcat apears when the app goes to the background: onDestroy and onRestart both of them aren't called...

EDIT: After adding the permissions, now I get the following logcat:

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

回答1:


Read your logcat, it requires permission

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

Add

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

inside your manifest.xml file




回答2:


Add this permission in your AndroidManifest.xml :

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



回答3:


Ok,thanks to: onCreate() called while Activity is stopped (but not destroyed). Only after installation

Adding:

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

to onCreate() solved the problem!



来源:https://stackoverflow.com/questions/52414216/android-app-calls-oncreate-when-coming-from-background

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!