简体   繁体   中英

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. 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:980" (1) Activity [Job Details] restarted

dt="16:34:54:689" (1) Activity [Job Details] on destroyed

dt="16:34:54:686" (1) Activity [Job Details] stopped

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! Has anyone seen something similar? I don't know what's going on! This happened on 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(Label = "Job Details")]
 public class WebViewer : BaseActivity

Where BaseActivity contains overrides of all the lifecycle events which I log. 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. I guess you restarted it yourself ?

PS : Code of your Activity might help identify the problem, if there's one.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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