[英]Android Application Crash Management using Xamarin
一般情況:
在使用xamarin開發android應用程序時,我們傾向於使應用程序多次崩潰,並且這種行為是完全可以接受和正常的。
但是有一段時間,當我從第一活動轉到第二活動時。 如果應用程序崩潰,則在第二個活動中,然后在下一次運行時,應用程序從第二個活動開始,而第二個活動應從第一個活動開始。 請注意第一個活動:mainLauncher屬性設置為TRUE。
應用范圍:
1 st Activity = Splash Screen;
2 nd Activity = Login Screen; [sets a global appication variable User ID]
3 rd Activity = User Profile [fetch data from database using User ID];
4 th Activity = preference page, settings which saves username n password from login form.
================================================== ===========================
問題:
在運行應用程序時,流程應根據給定的活動進行,當我在登錄頁面上輸入正確的憑據時,它將保存我的(int)UserID 。 並使用此UserID獲取用戶的詳細信息,並將其顯示在3rd Activity上。
但是有時如果應用程序在第3個活動上崩潰,並且在我重新啟動應用程序時,它不是從啟動屏幕啟動,而是從該活動( 第3個活動的 調用者 )或上一個活動( 第3個活動的調用者 )崩潰了,但並不總是調用活動的位置的LoginForm )。
現在的問題是,由於我從未收到過LoginPage,所以從未輸入登錄詳細信息,因此(int)UserID設置為0 / NULL ,因此沒有從數據庫中獲取任何數據 。 將第三個活動(配置文件活動)的所有字段顯示為空白
我的處理解決方案:
為了處理此問題,我將覆蓋OnResume()並檢查UserID ,即, 如果userID為0,則加載LoginScreen 。 這工作得很好。
但是現在當我通過殺死processID手動退出應用程序時,仍然保留Profile屏幕,它調用OnResume();。 再次。 當我注銷后,再次加載登錄屏幕,....
這繼續循環。 現在,我無法通過殺死processID
來退出應用程序,請幫助我找到此問題的解決方案
很長的問題,很抱歉,但是我沒有找到比這更好的解釋的方法了,希望我的朋友們不會介意。
(由OP在問題編輯中回答。轉換為社區Wiki答案。請參閱無答案的問題,但問題已在評論中解決(或擴展為聊天) )
OP寫道:
我設法解決了這個問題,並將通過一個簡單的示例進行說明。
clsMainProjectLibrary類:
這是我們定義了可以在任何活動中調用的函數的主庫,以便我們可以重用代碼。
clsMainProjectLibrary
{
//This variable is set by the calling activity to check if user has opt for exiting the application or actually application has crashed.
public static bool isExit;
// My Global Application variable which get assigned when user successfully log-ins else is
public static int intUserID;
public Intent ApplicationCrashHandler(Activity actForm)
{
if (clsFunctionaLibA.isExit == false)
{
Intent intentMyFirstForm = new Intent(actForm,typeof(actMyFistForm));
intentMyFirstForm.SetFlag(ActivityFlags.ClearTop & ActivityFlags.NewTask);
return intentMyFirstForm;
}
else
{
Intent blankIntent = new Intent();
blankIntent.SetFlags(ActivityFlags.ClearTop);
int intPID = Android.OS.Process.MyPid();
Android.OS.Process.KillProcess(intPID);
return null;
}
}
}
** actMyCustomActivity.cs:**
這是我的自定義活動,在此活動中,我們將調用
ApplicationCrashHandler()
。 因此,無論何時該活動崩潰或默認情況下啟動,它都應該加載我的actMyFirstForm
[Activity(Label = "Profile Details",MainLauncher=false)]
public class actMyCustomActivity : Activity
{
clsMainProjectLibrary objMainProjectLibrary = new clsMainProjectLibrary();
protected override void OnCreate(Bundle bundle)
{
// Put your all code here... what u want
}
protected override void OnResume()
{
if (clsMainProjectLibrary.intUserID == 0)
{
StartActivity(objMainProjectLibrary.ApplicationCrashHandler(this));
}
base.OnResume();
}
public override bool OnCreateOptionsMenu(IMenu menu)
{
//here you have your exit Menu.
}
public override bool OnOptionsItemSelected(IMenuItem item)
{
//code for user clicking on Exit.
:
:
case 'Exit' :
clsFunctionaLibA.isExit = true;
int intPID = Android.OS.Process.MyPid();
Android.OS.Process.KillProcess(intPID);
:
:
}
}
================================================== ==================
**此代碼對我有用... **
它檢查
UserID = 0
或具有某些值,以及用戶是否單擊了退出菜單。 我們需要完全退出應用程序而不加載我們的“必需”屏幕。這可能不是最佳解決方案,也不是推薦的解決方案,但這對我有用。
如果有解決此問題的合法方法,請不要讓我,以便我可以相應地修改我的代碼。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.