Download Code

Xamarin User Session Example Android

Storing user credentials is an important factor in an application.This mechanism helps user from login again and again.In this tutorial you will learn how to maintain user session xamarin user session example android application using Preferences.You will use IShared Preferences to maintain user session.

Preferences

Basic Preference UI building block.This class provides the View to be displayed in the activity.Associates with a ISharedPreferences to store/retrieve the preference data.

IShared Preferences

Interface for accessing and modifying preference data returned by getSharedPreferences(String, int).

User Session

Presence of a particular user of using app.

Create new xamarin user session example android application project.Go to Main.axml and add EditText,Button.

xamarin user session,xamarin user session example,xamarin user session example android,user session example android, how to keep user login in xamarin android,xamarin keeping user login android,

Create new XML layout(home.axml) which display when you login.

xamarin user session,xamarin user session example,xamarin user session example android,user session example android, how to keep user login in xamarin android,xamarin keeping user login android,

Next go to MainActivity.cs.Do the following steps.

  • Create UI Component variables(EditText,Button).
  • Refer id’s of them.
  • Create Preferance Name variable.
  • Create variables for user credentials like email,pass etc.
  • Create a method which run first to check credentials of user.

MainActivity.cs

namespace XamarinUserSession
{
	[Activity(Label = "XamarinUserSession", MainLauncher = true, Icon = "@mipmap/icon")]
	public class MainActivity : Activity, Android.Views.View.IOnClickListener
	{
		EditText email, password;
		Button signIn;
		public static String userSessionPref = "userPrefs";
		public static String User_Email = "userEmail";
		public static String User_Password = "userPassword";
		ISharedPreferences session;
		String SESSION_EMAIL, SESSION_PASS;

		protected override void OnCreate(Bundle savedInstanceState)
		{
			base.OnCreate(savedInstanceState);

			// Set our view from the "main" layout resource
			SetContentView(Resource.Layout.Main);
			checkCredentials();
			initialize();
			session = GetSharedPreferences(userSessionPref, FileCreationMode.Private);
		}
		public void initialize()
		{
			email = (EditText)FindViewById(Resource.Id.userEmail);
			password = (EditText)FindViewById(Resource.Id.userPass);
			signIn = (Button)FindViewById(Resource.Id.signInBtn);
			signIn.SetOnClickListener(this);
		}
		public void OnClick(View v)
		{
			switch (v.Id)
			{
				case Resource.Id.signInBtn:
					SESSION_EMAIL = email.Text.ToString();
					SESSION_PASS = password.Text.ToString();
					ISharedPreferencesEditor session_editor = session.Edit();
					session_editor.PutString("email", SESSION_EMAIL);
					session_editor.PutString("pass", SESSION_PASS);
					session_editor.Commit();
					Intent n = new Intent(this, typeof(Home));
					StartActivity(n);
					Finish();
					break;
			}
		}
		// method to check existing credentials
		public void checkCredentials()
		{
			ISharedPreferences preferences = GetSharedPreferences(userSessionPref, FileCreationMode.Private);
			String email = preferences.GetString("email", "");
			Toast.MakeText(this, email, ToastLength.Short).Show();
			String pass = preferences.GetString("pass", "");
			if (!email.Equals("") && !pass.Equals(""))
			{
				Intent n = new Intent(this, typeof(Home));
				StartActivity(n);
				Finish();
			}

		}
	}
}

Create new activity/class for home layout.In this activity one button is used to logout user.
Home.cs

namespace XamarinUserSession
{
	[Activity(Label = "Home")]
	public class Home : Activity
	{
		Button signOut;
		protected override void OnCreate(Bundle savedInstanceState)
		{
			base.OnCreate(savedInstanceState);
			SetContentView(Resource.Layout.home);
			signOut = (Button)FindViewById(Resource.Id.outt);
			signOut.Click += SignOut_Click;
		}

		void SignOut_Click(object sender, EventArgs e)
		{
			ISharedPreferences session = GetSharedPreferences(MainActivity.userSessionPref, FileCreationMode.Private);
			ISharedPreferencesEditor editor = session.Edit();
			editor.Clear();
			editor.Commit();
			Intent m = new Intent(this, typeof(MainActivity));
			StartActivity(m);
			Finish();
		}
	}
}

Build and Run your xamarin user session example android application project.

xamarin user session,xamarin user session example,xamarin user session example android,user session example android, how to keep user login in xamarin android,xamarin keeping user login android,

That’s it for xamarin user session example android tutorial.

For Java example visit Android Codec.

Thanks.