In my application, i have made login activity as the main launcher. When user opens app first time user need to enter credentials to go to main activity. At the time user logs in, i have saved some information that main activity need in Preferences.
Now what i want is, when ever user opens app again, assume user has not logged out, user should be sent directly to main activity with the help of information saved in the preferences.
Below is the code in login activity which creates new session.
if (res.carExists != true)
{
MyMessageBox.SetAlertBox("Opps!!!!!!!!", "This Car Number Was Wrong!!!!", "OK", m_context);
}
else
{
string carType = res.carType;
string seatNum = res.numOfSeats.ToString();
// MainActivity act = new MainActivity( result.driverId );
session = new SessionManger(m_context);
session.createLoginSession(result.driverId.ToString());
var mact = new Intent(m_context, typeof(MainActivity));
mact.PutExtra("driverID", result.driverId.ToString());
MyMessageBox.SetAlertBox("Comfirm!", "Your car is a: " + carType + " with " + seatNum + " seats??", "Yes", "No", mact, m_context);
}
Below is the code for Session Manager which saves the user information in login.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.Preferences;
namespace NorthStar.Driver
{
public class SessionManger
{
ISharedPreferencesEditor editor;
ISharedPreferences pref;
// Context
Context _context;
// Shared pref mode
int PRIVATE_MODE = 0;
// Sharedpref file name
private static readonly String PREF_NAME = "AndroidHivePref";
// All Shared Preferences Keys
private static readonly String IS_LOGIN = "IsLoggedIn";
// User name (make variable public to access from outside)
public static readonly String KEY_NAME = "driver";
// Constructor
public SessionManger(Context context)
{
this._context = context;
pref = _context.GetSharedPreferences(PREF_NAME, Android.Content.FileCreationMode.Private);
editor = pref.Edit();
}
public void createLoginSession(String driverID)
{
// Storing login value as TRUE
editor.PutBoolean(IS_LOGIN, true);
// Storing name in pref
editor.PutString(KEY_NAME, driverID);
editor.Commit();
}
public void checkLogin()
{
if (!this.isLoggedIn())
{
Intent i = new Intent(_context, typeof(Activity1));
i.AddFlags(ActivityFlags.ClearTop);
i.SetFlags(ActivityFlags.NewTask);
_context.StartActivity(i);
}
}
public string getDriver()
{
return pref.GetString(KEY_NAME, "");
}
public Boolean isLoggedIn()
{
return pref.GetBoolean(IS_LOGIN, false);
}
}
}
Can somebody give me some hint how can i direct user to main activity with the help of saved informationin Shared Preferences.
As you are saving some value in shared preference when user logs in, next time when app opens, check for that shared preference value in Login activity(before doing login stuff). If its true(say for example), start main activity(finish login activity so that user should not come back to login activity after pressing back button) else continue with login activity. When user logs out, again replace shared preference value to false so that when next time app opens control should come to Login activity first.