首页 文章

Xamarin.Forms Android应用程序在启动外部活动后重新启动

提问于
浏览
3

我的Xamarin.Forms Android应用程序中的一个功能允许用户从Gallery中选择一张照片(它是来自XLabs的 IMediaPicker 自定义控件,显示本机库活动,就像您在任何其他常规Xamarin.Android应用程序中一样) .

问题是,有时设备内存不足(同样,正常的东西),当选择图像时,XF的 MainActivity 重新启动(按预期),应用程序在主页面中启动,而不是用户的页面以前是 .

我如何在Xamarin.Forms中处理这种情况?例如,在打开图库活动之前从用户所在的页面继续 .

1 回答

  • 2

    您可以尝试通过覆盖OnSleep method来保存应用程序的状态 .

    Xamarin Forms Samples中有一个实现示例 . 特别是,检查OnSleep和OnResume:

    protected override void OnSleep()
    {
        Debug.WriteLine ("OnSleep saving ResumeAtTodoId = " + ResumeAtTodoId);
        // the app should keep updating this value, to
        // keep the "state" in case of a sleep/resume
        Properties ["ResumeAtTodoId"] = ResumeAtTodoId;
    }
    
    protected override void OnResume()
    {
        Debug.WriteLine ("OnResume");
        if (Properties.ContainsKey ("ResumeAtTodoId")) {
            var rati = Properties ["ResumeAtTodoId"].ToString();
    
            Debug.WriteLine ("   rati="+rati);
            if (!String.IsNullOrEmpty (rati)) {
                Debug.WriteLine ("   rati = " + rati);
                ResumeAtTodoId = int.Parse (rati);
    
                if (ResumeAtTodoId >= 0) {
                    var todoPage = new TodoItemPage ();
                    todoPage.BindingContext = Database.GetItem (ResumeAtTodoId);
                    MainPage.Navigation.PushAsync (
                        todoPage, false); // no animation
                }
            }
        }
    }
    

相关问题