我的xamarin android app流程是Splash Screen Activity - >登录活动 - >家庭活动
当我在Home Activity时,按主页按钮。我希望家庭活动仍然开放。但事实上,Splash Screen再次打开 - >登录活动 - >家庭活动。
那么当我回到我的应用程序时,如何保存当前的应用程序状态呢?
非常感谢你,
答案 0 :(得分:0)
在你的"启动画面活动"您可以设置活动NoHistory = true
,假设您从不想重新显示应用的启动画面。
您可以对登录活动执行相同的操作,但如果用户未登录,您可能希望返回登录活动,因此在您的" Home Activity"覆盖OnBackPressed()
:
示例:强>
public override void OnBackPressed()
{
if (YourUsersLoginStatus = false)
{
base.Onbackpressed
// Finish this activity unless you need to keep it for some reason
finish();
} else {
// Do nothing as you want to stay on this activity as your
// User is still logged in
}
}
NoHistory属性:
Android风格:
[android:noHistory="true"] attribute
Xamarin风格:
[Activity (NoHistory = true)]
Android.App.ActivityAttribute.NoHistory Property
是否应从活动堆栈中删除活动 并在用户导航时完成。
语法
public Boolean NoHistory {get;组; }
值:一个布尔值,指定活动是否应该是 从活动堆栈中删除并在用户导航时完成 程。
参考:http://developer.xamarin.com/api/property/Android.App.ActivityAttribute.NoHistory/
参考:http://developer.xamarin.com/guides/android/application_fundamentals/activity_lifecycle/
参考:http://developer.android.com/reference/android/R.styleable.html#AndroidManifestActivity_noHistory
<强>更新强>
一个启动屏幕活动,NoHistory
属性设置为true,MainLauncher设置为true
[Activity(Theme = "@style/Theme.Splash", MainLauncher = true, NoHistory = true)]
public class SplashActivity : Activity
{
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
StartActivity(typeof(MainActivity));
}
}
SplashActivity
启动MainActivity
即可开始SpecialActivity
[Activity (Label = "MainActivity", Icon = "@mipmap/icon")]
public class MainActivity : Activity
{
protected override void OnCreate (Bundle savedInstanceState)
{
base.OnCreate (savedInstanceState);
SetContentView (Resource.Layout.Main);
Button button = FindViewById<Button> (Resource.Id.myButton);
button.Click += delegate {
//Fake a login status
var editor = GetSharedPreferences ("Login", Android.Content.FileCreationMode.Private);
var edit = editor.Edit ();
edit.PutBoolean ("login", true);
edit.Apply();
StartActivity(typeof(SpecialActivity));
};
}
}
后退按钮不会返回SplashActivity
,因为启动不在堆栈历史记录中。如果单击按钮而不是SpecialActivity
开始。
[Activity (Label = "SpecialActivity")]
public class SpecialActivity : Activity
{
ISharedPreferences myPrefs;
Button logoutButton;
protected override void OnCreate (Bundle savedInstanceState)
{
base.OnCreate (savedInstanceState);
SetContentView (Resource.Layout.Special);
logoutButton = FindViewById<Button> (Resource.Id.logoutButton);
myPrefs = GetSharedPreferences ("Login", Android.Content.FileCreationMode.Private);
logoutButton.Click += delegate {
var edit = myPrefs.Edit ();
edit.PutBoolean ("login", false);
edit.Apply();
logoutButton.Text = "Logout / Back Button will work now";
};
}
public override void OnBackPressed()
{
var isLoggedIn = myPrefs.GetBoolean("login", false);
if (!isLoggedIn)
{
base.OnBackPressed ();
Finish ();
} else {
logoutButton.Text = "Click me to log out first!";
}
}
}
后退按钮将无法运行,直到您退出&#39;通过单击按钮,退出后退按钮将返回MainActivity
一旦返回MainActivity
,后退按钮将退出应用,因为没有堆栈历史记录,我们没有覆盖OnBackPressed
。