简体   繁体   English

Android活动生命周期:在OnDestroy之后调用OnRestart

[英]Android Activity Life Cycle: OnRestart being called after OnDestroy

I have come across an unusual problem. 我遇到了一个不寻常的问题。 In my Android App, I log all the activity lifecycle events as a matter of routine. 在我的Android应用程序中,我记录所有活动生命周期事件作为例行事项。 One of my activities should be finished when another one opens. 当另一个活动打开时,我的一个活动应该完成。 However, occasionally I get bugs occurring because it has not closed. 但是,偶尔我会发现错误,因为它还没有关闭。 Upon investigation it seems that the activity is being destroyed, but then restarted - below is the relevant portion of my app's log, most recent event first: 经过调查,似乎活动正在被销毁,但随后重新开始 - 以下是我的应用程序日志的相关部分,最近的事件是:

dt="16:37:47:982" (1) Activity [Job Details] resumed dt =“16:37:47:982”(1)活动[工作细节]恢复

dt="16:37:47:980" (1) Activity [Job Details] restarted dt =“16:37:47:980”(1)活动[作业详细信息]重新启动

dt="16:34:54:689" (1) Activity [Job Details] on destroyed dt =“16:34:54:689”(1)被破坏的活动[工作细节]

dt="16:34:54:686" (1) Activity [Job Details] stopped dt =“16:34:54:686”(1)活动[工作细节]停止

According to the activity life cycle OnRestart cannot be called after OnDestroy, see http://developer.android.com/reference/android/app/Activity.html All the relevant activity life cycle events are logged - eg OnCreate would result in an entry in my log, but there is no evidence for this! 根据活动生命周期OnRestart在OnDestroy之后无法调用,请参阅http://developer.android.com/reference/android/app/Activity.html所有相关活动生命周期事件都会被记录 - 例如OnCreate会导致输入在我的日志中,但没有证据证明这一点! Has anyone seen something similar? 有没有人见过类似的东西? I don't know what's going on! 我不知道发生了什么事! This happened on Android 5.0 这发生在Android 5.0上

My log statements are in a base class for each activity. 我的日志语句位于每个活动的基类中。 My Activity is declared thus (it is written in C# / Xamarin): 我的Activity被声明(它是用C#/ Xamarin编写的):

[Activity(Label = "Job Details")]
 public class WebViewer : BaseActivity

Where BaseActivity contains overrides of all the lifecycle events which I log. 其中BaseActivity包含我记录的所有生命周期事件的覆盖。 For example: 例如:

public class BaseActivity : Activity, IForceLogOff, ISettingsUpdated,  IJobUnassigned, IDisplayUpgradeWarning
    {
protected override void OnDestroy()
    {
        base.OnDestroy();
        Log.WriteLog("Activity [" + this.Title + "] on destroyed", ELogType.GUI);
        AppStorage.GetAppStorageInstance().LastGUIActivityDT = TConverting.GetCurrentDT();
        NoInstancesManager.DecrementCount(this.GetType().FullName);
    }

    protected override void OnRestart()
    {
        base.OnRestart();
        Log.WriteLog("Activity [" + this.Title + "] restarted", ELogType.GUI);
        AppStorage.GetAppStorageInstance().LastGUIActivityDT = TConverting.GetCurrentDT();
    }

etc
}

There's nearly 3 minutes between the destruction of your Activity and the restart of it. 在销毁活动和重新启动活动之间差不多有3分钟。 I guess you restarted it yourself ? 我猜你自己重启了吗?

PS : Code of your Activity might help identify the problem, if there's one. PS:您的活动代码可能有助于识别问题,如果有的话。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM