java - how to get value of switch button from secondActivity? -


in project have 2 activity , in second activity have switch button .

i have 2 problems :

1: want value of switch button second activity , pass first activity using shared preferences show toast in first activity not value of switch button toast not work correctly .

2:when press or close application , value of switch button change default , how can save value of switch button ?

this first activity :

 public class mainactivity extends appcompatactivity {   @override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_main);      sharedpreferences settings = preferencemanager.getdefaultsharedpreferences(this);     boolean ischecked = settings.getboolean("status" , false);      if (ischecked){         toast.maketext(this, "enabled", toast.length_short).show();     }else {         toast.maketext(this, "disabled", toast.length_short).show();     }   }  @override public boolean oncreateoptionsmenu(menu menu) {     menuinflater menuinflater = getmenuinflater();     menuinflater.inflate(r.menu.menu, menu);     return super.oncreateoptionsmenu(menu); }  @override public boolean onoptionsitemselected(menuitem item) {     switch (item.getitemid()){         case r.id.action_setting:             intent settingintent = new intent(getapplicationcontext(), main2activity.class);             startactivity(settingintent);             return true;     }     return super.onoptionsitemselected(item); } 

}

and second activity :

 public class main2activity extends appcompatactivity {  switch aswitch; edittext edittext; button back;  @override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_main2);      edittext = (edittext) findviewbyid(r.id.edittext);     aswitch = (switch) findviewbyid(r.id.switch1);        = (button) findviewbyid(r.id.btnback);     back.setonclicklistener(new view.onclicklistener() {         @override         public void onclick(view view) {             intent swintent = new intent(main2activity.this, mainactivity.class);             startactivity(swintent);         }     });  }  @override public void onbackpressed() {     super.onbackpressed(); }  @override protected void onsaveinstancestate(bundle outstate) {     outstate.putboolean("myswitch", true);     super.onsaveinstancestate(outstate); }  public void enable_disable(view view) {     if (aswitch.ischecked()) {          sharedpreferences.editor editor = getsharedpreferences("switch", mode_private).edit();         editor.putboolean("status", true);         editor.commit();     } else {         sharedpreferences.editor editor = getsharedpreferences("switch", mode_private).edit();         editor.putboolean("status", false);         editor.commit();     } } 

}

and layout of second activity :

 <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:layout_centerinparent="true" android:gravity="center" tools:context="a.switchtest.main2activity">    <switch     android:id="@+id/switch1"     android:layout_width="match_parent"     android:layout_height="wrap_content"     android:onclick="enable_disable"     android:text="switch" />  <edittext     android:id="@+id/edittext"     android:layout_width="match_parent"     android:layout_height="wrap_content" />  <button     android:id="@+id/btnback"     android:text="back"     android:layout_width="wrap_content"     android:layout_height="wrap_content" /> 

use switch onchangedlistener. retain switch state in main2activity

public class main2activity extends appcompatactivity {  switch aswitch; button back;  @override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_main2);      aswitch = (switch) findviewbyid(r.id.switch1);     sharedpreferences sharedpreference = getsharedpreferences("switch", mode_private);     boolean ischecked = sharedpreference.getboolean("status",false);     log.d("shriyansh",ischecked+"");     aswitch.setchecked(ischecked);     aswitch.setoncheckedchangelistener(new compoundbutton.oncheckedchangelistener() {         public void oncheckedchanged(compoundbutton buttonview, boolean ischecked) {             // something, ischecked             // true if switch in on position             sharedpreferences.editor editor = getsharedpreferences("switch", mode_private).edit();             editor.putboolean("status", ischecked);             editor.commit();             log.d("shriyansh1",ischecked+"");         }     });      = (button) findviewbyid(r.id.btnback);     back.setonclicklistener(new view.onclicklistener() {         @override         public void onclick(view view) {             intent swintent = new intent(main2activity.this, mainactivity.class);             startactivity(swintent);         }     });  } 

now show toast in first activity can use preference in onresume() method of mainactivity

public class mainactivity extends appcompatactivity {  @override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_main); }  @override protected void onresume() {     super.onresume();     sharedpreferences sharedpreference = getsharedpreferences("switch", mode_private);     boolean ischecked = sharedpreference.getboolean("status",false);     if (ischecked){         toast.maketext(this, "enabled", toast.length_short).show();     }else {         toast.maketext(this, "disabled", toast.length_short).show();     }  }  @override public boolean oncreateoptionsmenu(menu menu) {     menuinflater menuinflater = getmenuinflater();     menuinflater.inflate(r.menu.menu, menu);     return super.oncreateoptionsmenu(menu); }  @override public boolean onoptionsitemselected(menuitem item) {     switch (item.getitemid()){     case r.id.action_setting:         intent settingintent = new intent(getapplicationcontext(),                 main2activity.class);         startactivity(settingintent);         return true;     }     return super.onoptionsitemselected(item);  } } 

layout of main2activity remove enable_disable method switch

<linearlayout  xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:layout_centerinparent="true" android:gravity="center" tools:context="a.switchtest.main2activity">    <switch     android:id="@+id/switch1"     android:layout_width="match_parent"     android:layout_height="wrap_content"     android:text="switch" />  <edittext     android:id="@+id/edittext"     android:layout_width="match_parent"     android:layout_height="wrap_content" />  <button     android:id="@+id/btnback"     android:text="back"     android:layout_width="wrap_content"     android:layout_height="wrap_content" /> 

in mainactivity need replace

preferencemanager.getdefaultsharedpreferences(this); 

with

sharedpreferences sharedpreference = getsharedpreferences("switch", mode_private); 

also remove main2activity

@override protected void onsaveinstancestate(bundle outstate) {     outstate.putboolean("myswitch", true);     super.onsaveinstancestate(outstate); } 

Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -