在Xamarin Android應用程序中,我有一個Activity在RetainInstance
片段中調用異步方法(網絡操作),以便操作不停止配置更改。操作完成後,界面被更改,進度對話框消失,新的片段被插入到佈局中,等等。C#異步方法在Activity暫停時繼續,導致IllegalStateException:無法在onSaveInstanceState後執行此操作
即使活動在配置更改中被破壞並重新創建,它也能正常工作。但是,如果活動在異步方法完成時暫停,則UI操作會拋出異常。如果用戶在網絡操作運行時關閉屏幕或切換到另一個應用程序,則會發生這種情況。
如果活動未暫停,是否有方法使異步方法正常繼續。但是,如果活動已暫停,請等到活動恢復後再繼續操作?
或者,處理活動暫停時完成的異步操作的正確方法是什麼?
代碼:
using System;
using System.Threading.Tasks;
using Android.App;
using Android.OS;
using Android.Widget;
namespace AsyncDemo {
[Activity(Label = "AsyncDemo", MainLauncher = true, Icon = "@drawable/icon")]
public class MainActivity : Activity {
const string fragmentTag = "RetainedFragmentTag";
const string customFragmentTag = "CustomFragmentTag";
const string dialogTag = "DialogFragmentTag";
protected override void OnCreate(Bundle savedInstanceState) {
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.Main);
var retainedFragment = FragmentManager.FindFragmentByTag(fragmentTag) as RetainedFragment;
if (retainedFragment == null) {
retainedFragment = new RetainedFragment();
FragmentManager.BeginTransaction()
.Add(retainedFragment, fragmentTag)
.Commit();
}
Button button = FindViewById<Button>(Resource.Id.myButton);
button.Click += delegate {
button.Text = "Please wait...";
var dialogFragment = new DialogFragment(); // Substitute for a progress dialog fragment
FragmentManager.BeginTransaction()
.Add(dialogFragment, dialogTag)
.Commit();
Console.WriteLine("Starting task");
retainedFragment.doIt();
};
}
void taskFinished() {
Console.WriteLine("Task finished, updating the UI...");
var button = FindViewById<Button>(Resource.Id.myButton);
button.Text = "Task finished";
var dialogFragment = FragmentManager.FindFragmentByTag(dialogTag) as DialogFragment;
dialogFragment.Dismiss(); // This throws IllegalStateException
var customFragment = new CustomFragment();
FragmentManager.BeginTransaction()
.Replace(Resource.Id.container, customFragment, customFragmentTag)
.Commit(); // This also throws IllegalStateException
}
class RetainedFragment : Fragment {
public override void OnCreate(Bundle savedInstanceState) {
base.OnCreate(savedInstanceState);
RetainInstance = true;
}
public void doIt() {
doItAsync();
}
public async Task doItAsync() {
try {
await Task.Delay(3000); // substitute for the real operation
(Activity as MainActivity).taskFinished();
} catch (Exception e) {
Console.WriteLine(e);
}
}
}
}
}
日誌:
Starting task
Task finished, updating the UI...
Java.Lang.IllegalStateException: Exception of type 'Java.Lang.IllegalStateException' was thrown.
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() [0x0000b] in /Users/builder/data/lanes/1978/f98871a9/source/mono/mcs/class/corlib/System.Runtime.ExceptionServices/ExceptionDispatchInfo.cs:61
at Android.Runtime.JNIEnv.CallVoidMethod (IntPtr jobject, IntPtr jmethod) [0x00062] in /Users/builder/data/lanes/1978/f98871a9/source/monodroid/src/Mono.Android/src/Runtime/JNIEnv.g.cs:554
at Android.App.DialogFragment.Dismiss() [0x00043] in /Users/builder/data/lanes/1978/f98871a9/source/monodroid/src/Mono.Android/platforms/android-22/src/generated/Android.App.DialogFragment.cs:284
at AsyncDemo.MainActivity.taskFinished() [0x00039] in /Users/csdvirg/workspaces/xamarin/AsyncDemo/AsyncDemo/MainActivity.cs:52
at AsyncDemo.MainActivity+RetainedFragment+<doItAsync>c__async0.MoveNext() [0x00094] in /Users/csdvirg/workspaces/xamarin/AsyncDemo/AsyncDemo/MainActivity.cs:73
--- End of managed exception stack trace ---
java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
at android.app.FragmentManagerImpl.checkStateLoss(FragmentManager.java:1323)
at android.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1341)
at android.app.BackStackRecord.commitInternal(BackStackRecord.java:597)
at android.app.BackStackRecord.commit(BackStackRecord.java:575)
at android.app.DialogFragment.dismissInternal(DialogFragment.java:292)
at android.app.DialogFragment.dismiss(DialogFragment.java:258)
at mono.java.lang.RunnableImplementor.n_run(Native Method)
at mono.java.lang.RunnableImplementor.run(RunnableImplementor.java:29)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5756)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1291)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107)
at dalvik.system.NativeStart.main(Native Method)
您是否嘗試過 'RunOnUiThread(()=> {});'? – choper
@choper作爲'await'的副作用,它已經在UI Thread上運行。但爲了確保,我使用了RunOnUiThread,並引發了相同的異常。 – imgx64
http://blogs.msdn.com/b/pfxteam/archive/2013/01/13/cooperatively-pausing-async-methods.aspx然後閱讀本文,在其中描述瞭如何暫停異步方法 – choper