简体   繁体   English

为什么(以及如何修复)我的 Android 应用程序在从内存中删除时触发 onCreate (ON_CREATE)?

[英]Why is (and how to fix) my Android application triggering an onCreate (ON_CREATE) when removed from memory?

I manage an Android app for a client and am trying to detect when the app is removed from memory.我为客户端管理一个 Android 应用程序,并试图检测该应用程序何时从内存中删除。 I've noticed via logcat that when the app is removed from memory an ON_CREATE Lifecycle.Event is being sent.我通过 logcat 注意到,当应用程序从内存中删除时,正在发送 ON_CREATE Lifecycle.Event。 I do get an ON_STOP when the app is closed, but swiping to remove it completely from memory for some reason only generates an ON_CREATE that is additional to the original one at app launch.当应用程序关闭时,我确实得到了一个 ON_STOP,但由于某种原因,将其从内存中完全删除只会在应用程序启动时生成一个 ON_CREATE,它是对原始应用程序的附加。

I have some code that runs in Application object's ON_CREATE and can prevent it from being executed a second time if I want, but would rather prevent this extraneous event from firing.我有一些在 Application 对象的 ON_CREATE 中运行的代码,如果我愿意,可以阻止它第二次执行,但宁愿阻止这个无关的事件触发。 This seems wrong and I want to fix it, and get an ON_DESTROY if I can, although I've seen here on SO that sometimes it is not fired.这似乎是错误的,我想修复它,如果可以的话,得到一个 ON_DESTROY,尽管我在这里看到它有时不会被解雇。

My Application class code looks like this:我的应用程序类代码如下所示:

public class ThisApplication extends Application implements LifecycleObserver
{    
    @Override
    public void onCreate() {
        super.onCreate();
        ProcessLifecycleOwner.get().getLifecycle().addObserver(this);
        Log.e("ThisApplication", "Inside onCreate()");
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_STOP)
    public void onAppStop() {
        Log.e("ThisApplication", "ON_STOP()");
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_START)
    public void onAppStart() {
        Log.e("ThisApplication", "ON_START()");
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
    public void onAppDestroy() {
        Log.e("ThisApplication", "ON_DESTROY");
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
    public void onAppResume() {
        Log.e("ThisApplication", "ON_RESUME");    
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_CREATE)
    public void onAppCreate() {
        Log.e("ThisApplication", "ON_CREATE");
    }

}

As requested here is the manifest:按照这里的要求是清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="com.neimander.locus_android">

    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

    <uses-feature
        android:name="android.hardware.camera"
        android:required="true"/>
    <uses-feature android:name="android.hardware.camera.autofocus"/>

    <uses-permission android:name="android.permission.CAMERA"/>

    <application

        android:hardwareAccelerated="true"
        android:allowBackup="false"
        android:icon="@drawable/ail_logo"
        android:label="@string/app_name"
        android:theme="@style/MainTheme"
        android:name="ThisApplication">

        <service android:name="com.amazonaws.mobileconnectors.s3.transferutility.TransferService" android:enabled="true" />
        <activity
            android:windowSoftInputMode="stateVisible"
            android:name=".LoginActivity"
            android:excludeFromRecents="true"
            android:label="Login"
            android:screenOrientation="portrait"/>
        <activity
            android:name="com.microsoft.aad.adal.AuthenticationActivity"
            android:label="Authentication" >
        </activity>
        <activity
            android:name=".HomeScreenActivity"
            android:label="@string/title_activity_home_screen"
            android:screenOrientation="portrait">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
        <activity
            android:name=".GradeAssessmentActivity"
            android:label="@string/title_activity_grade_assessment"
            android:noHistory="true"
            android:parentActivityName=".HomeScreenActivity"
            android:screenOrientation="portrait"/>
        <activity
            android:name=".ScanningActivity"
            android:label="@string/title_activity_scanning"
            android:parentActivityName=".GradeAssessmentActivity"
            android:screenOrientation="portrait"/>

    </application>

</manifest>

In the ProcessLifecycleOwner doc, it says:ProcessLifecycleOwner文档中,它说:

ON_CREATE will be dispatched once and ON_DESTROY will never be dispatched ON_CREATE将被分派一次,而ON_DESTROY将永远不会被分派

So if ON_CREATE is dispatched more than once, it is a bug and you should report it to the Android Issue Tracker .因此,如果ON_CREATE被多次分派,这是一个错误,您应该将其报告给Android 问题跟踪器

But before jumping on the bug report train, you should double check that you are registering and observing the lifecycle properly.但是在跳上错误报告火车之前,您应该仔细检查您是否正确地注册和观察生命周期。 It is more than likely that there is a silly error in the code.代码中很可能存在愚蠢的错误。

Also, it is impossible to track when your app is removed from memory from within the app itself.此外,无法从应用程序本身的内存中跟踪您的应用程序何时从内存中删除。 Android may keep your application's process even when nothing is in it. Android 可能会保留您的应用程序的进程,即使其中没​​有任何内容。 Opposite to that, Android may kill your application's process with no warning.与此相反,Android 可能会在没有任何警告的情况下终止您的应用程序进程。

Why are you bothering yourself with app closing?你为什么要为关闭应用程序而烦恼? Docs describe lifecycle somewhat, when you close it you're done and you can't assume the app is working.文档在某种程度上描述了生命周期,当你关闭它时你就完成了,你不能假设应用程序正在运行。 There is even continous working mode with some drabbacks.甚至还有带有一些缺点的连续工作模式。 On PC - applications that hide its instance or don't really want to close itself is called shitware.在 PC 上 - 隐藏其实例或不想关闭自身的应用程序称为垃圾软件。 Here it is somewhat solved with lots of nonsense in the background.在这里,在背景中有很多废话,它在某种程度上得到了解决。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我的Android应用程序在我的视图中启动睡眠而不是从主屏幕启动时调用onCreate()? - Why does my android app call onCreate() when sleep initiates in my view but not from home screen? Android Application onCreate,什么时候调用 - Android Application onCreate, when is it called Android:在oncreate加载listview时应用程序崩溃 - Android: Application Crashing when loading listview oncreate android,在onCreate()中创建线程时出错 - android, error when create a thread in onCreate() 从最近的应用程序列表中删除后,我的应用程序被杀死 - My application is getting killed when removed from recent apps list 从最近的任务中删除应用程序时,Android静态BroadcastReceiver无法正常工作 - Android Static BroadcastReceiver not working when Application is removed from Recent Tasks 在Android onCreate下匿名登录时,为什么Firebase控制台无法创建匿名用户? - Why doesn't Firebase Console create anonymous user when sign in anonymously under Android onCreate? 启动应用程序时,如何强制在SQLiteOpenHelper中运行onCreate? - How to force onCreate in my SQLiteOpenHelper to run when I start my application? 为什么在onCreate()中编写代码会导致我的android程序崩溃,但在onCreateOptionsMenu()中使用相同的代码会起作用? - Why does writing code in onCreate() crash my android program, but the same code works when in onCreateOptionsMenu()? 恢复应用程序时如何防止onCreate? - How to prevent onCreate when application is resumed?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM