java - Android: stay connected with shared preferences -


hi did android app several activities , login-logout phase used example of link:

http://www.tutorialspoint.com/android/android_session_management.htm

i want add checkbox in login-phase that, when clicked app remember user when exits without logout, when checkbox isn't clicked user access several activities when exits without logout there still logout. reason changed mainactivity in mode:

 public class mainactivity extends activity {       private edittext username,password;     public static final string mypreferences = "myprefs" ;     public static final string name = "namekey";     public static final string pass = "passwordkey";       sharedpreferences sharedpreferences;     private checkbox check;            @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_main);       username = (edittext)findviewbyid(r.id.edittext1);      password = (edittext)findviewbyid(r.id.edittext2);     username.settext("admin");     password.settext("admin");     check   = (checkbox) findviewbyid(r.id.checkbox1);         }      @override     protected void onresume() {      sharedpreferences=getsharedpreferences(mypreferences,context.mode_private);         if (sharedpreferences.contains(name))             {             if(sharedpreferences.contains(pass)){                     if (check.ischecked()) {                      intent = new intent(this,com.example.sessionmanagement.     welcome.class);                  startactivity(i);                     }                  else                     {          logout();                 }             }              }         super.onresume();         }  public void login(view view){              editor editor = sharedpreferences.edit();     string u = username.gettext().tostring();         string p = password.gettext().tostring();              if(u.equals("admin") && p.equals("admin")){              editor.putstring(name, u);              editor.putstring(pass, p);              editor.commit();              intent = new intent(this,com.example.sessionmanagement.welcome.class);              startactivity(i);              }      else{      toast.maketext(mainactivity.this, "ko", toast.length_short).show();           }         }       @override      public boolean oncreateoptionsmenu(menu menu) {          // inflate menu; adds items action bar if present.              getmenuinflater().inflate(r.menu.main, menu);              return true;          }        public void onpause() {    super.onpause();     this.finish();         }        public void closing() {          finish();         }        public void logout(){      sharedpreferences sharedpreferences = getsharedpreferences      (mainactivity.mypreferences, context.mode_private);      editor editor = sharedpreferences.edit();       editor.clear();      editor.commit();      movetasktoback(true);      this.finish();         }       } 

and in file.xml

<checkbox        android:id="@+id/checkbox1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignleft="@+id/textview1"        android:layout_centervertical="true"        android:checked="true"        android:onclick="onresume"        android:text="checkbox" /> 

this code doesn't work because when checkbox isn't checked there directly logout, user can't access other activities. how can write condition when checkbox isn't checked? how can solve problem?


Comments

Popular posts from this blog

How to provide Authorization & Authentication using Asp.net, C#? -

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

How to use Authorization & Authentication in Asp.net, C#? -