android - Changing MainActivity in Java -


i'm making toggle button changes activity changes once want make permanent user not need change again , again.

use shared preference . when user toggle button on (logged) application store login status sharedpreference , clear sharedpreference when user click on off(logged out).

check every time when user enter application if user status shared preference true no need login otherwise move login page.

to achieve first create 1 class, in class need write function regarding , set value in sharedpreference . please @ below code.

public class savesharedpreference  {     static final string pref_user_name= "username";      static sharedpreferences getsharedpreferences(context ctx) {         return preferencemanager.getdefaultsharedpreferences(ctx);     }      public static void setusername(context ctx, string username)      {         editor editor = getsharedpreferences(ctx).edit();         editor.putstring(pref_user_name, username);         editor.commit();     }      public static string getusername(context ctx)     {         return getsharedpreferences(ctx).getstring(pref_user_name, "");     } } 

now first check

if(savesharedpreference.getusername(mainactivity.this).length() == 0) {      // call login activity } else {      // call next activity } 

Comments

Popular posts from this blog

How has firefox/gecko HTML+CSS rendering changed in version 38? -

javascript - Complex json ng-repeat -

jquery - Cloning of rows and columns from the old table into the new with colSpan and rowSpan -