简体   繁体   中英

Save App state in Android Xamarin

My xamarin android app flow is Splash Screen Activity -> Login Activity -> Home Activity

When I'm at Home Activity, I press Home button. I expect that the Home Activity still opens. But in fact, the Splash Screen opens again -> Login Activity -> Home Activity.

So how could I save the current app state when back to my app?

Thank you so much,

On your "Splash Screen Activity" you can set the activity's NoHistory = true assuming you never want to redisplay the app's splash screen.

You could do the same thing for the login activity, but you might want to return to the login activity if the user if not logged in, so in your "Home Activity" override the OnBackPressed() :

Example:

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 attribute:

Android style:

 [android:noHistory="true"] attribute

Xamarin Style:

[Activity (NoHistory = true)]

Android.App.ActivityAttribute.NoHistory Property

Whether or not the activity should be removed from the activity stack and finished when the user navigates away.

Syntax

public Boolean NoHistory { get; set; }

Value : A Boolean specifying whether or not the activity should be removed from the activity stack and finished when the user navigates away.

Ref: http://developer.xamarin.com/api/property/Android.App.ActivityAttribute.NoHistory/

Ref: http://developer.xamarin.com/guides/android/application_fundamentals/activity_lifecycle/

Ref: http://developer.android.com/reference/android/R.styleable.html#AndroidManifestActivity_noHistory

Update:

A Splash Screen Activity with the NoHistory attribute set to true and MainLauncher set to 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 starts MainActivity which can start 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));
        };
    }
}

Back button will not return to SplashActivity since splash is not in stack history. If you click the button than the SpecialActivity starts.

[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!";
        }
    }
}

The back button will not work till you 'logout' by clicking the button, once logged out the back button will return to the MainActivity

Once returned to the MainActivity the back button will exit the app as there is no stack history and we are not overriding the OnBackPressed .

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