java - Click countries name and save opened Activity, every time open app , show to last selected countries activity, i want to add change the countries -
there lot of countries in app including india, pakistan, uae, south africa..etc. when clicking on name of country, new activity opened. saved share preference , when open app, last activity opened. created app this. want add 1 more thing app.
i want go countries list view button opened activity. user can change country here , when opening app, changed country's activity must opened. if countries opened this, there must option choosing country. hope me.
the project code done me given below.
the share preference work on it. want option change country option.
** countries listview (mainactivity)**
public class mainactivity extends appcompatactivity { cardview ind,pak,uae,south; string clickedcard; sharedpreferences prefs; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); getsupportactionbar().hide(); ind = (cardview) findviewbyid(r.id.ind); pak = (cardview) findviewbyid(r.id.pak); ind.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { intent cltbutton = new intent(mainactivity.this, india.class); clickedcard = "button 1"; cltbutton.putextra("frommain", clickedcard); startactivity(cltbutton); } }); pak.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { intent mgbutton = new intent(mainactivity.this,pak .class); clickedcard = "button 2"; mgbutton.putextra("frommain2", clickedcard); startactivity(mgbutton); } }); } private void checkpreferences() { ////india preference prefs = getsharedpreferences("pref", mode_private); if (prefs.getstring("txt", "").equals("") || prefs.getstring("lastactivity", "").equals("")) { } else { string txt = prefs.getstring("txt", ""); string activity = prefs.getstring("lastactivity", ""); intent cltbutton = new intent(mainactivity.this, india.class); cltbutton.putextra("frommain", txt); startactivity(cltbutton); finish(); } ////pakistan preference prefs = getsharedpreferences("pref2", mode_private); if (prefs.getstring("txt2", "").equals("") || prefs.getstring("lastactivity2", "").equals("")) { } else { string txt2 = prefs.getstring("txt2", ""); string activity = prefs.getstring("lastactivity", ""); intent mgbutton =mainactivity new intent(mainactivity.this, pak.class); mgbutton.putextra("frommain2", txt2); startactivity(mgbutton); finish(); } } }
india activity code
public class india extends appcompatactivity { string s; sharedpreferences prefs; button buttonind; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_india); buttonind = (button) findviewbyid(r.id.buttonind); buttonind.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { intent mgbutton = new intent(india.this, main2activity.class); startactivity(mgbutton); } }); intent intent = getintent(); bundle bundle = intent.getextras(); if (bundle == null) { s = "no data received"; } else { s = bundle.getstring("frommain"); } } @override protected void onpause() { super.onpause(); prefs = getsharedpreferences("pref", mode_private); sharedpreferences.editor editor = prefs.edit(); editor.putstring("txt", s); editor.putstring("lastactivity", getclass().getname()); editor.apply(); } }
pak activity code
public class pak extends appcompatactivity { string s; sharedpreferences prefs; button buttonpak; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_pak); buttonpak = (button) findviewbyid(r.id.buttonpak); intent intent = getintent(); bundle bundle = intent.getextras(); if (bundle == null) { s = "no data received"; } else { s = bundle.getstring("frommain2"); } buttonpak.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { intent mgbutton1 = new intent(pak.this, main2activity.class); startactivity(mgbutton1); } }); } @override protected void onpause() { super.onpause(); prefs = getsharedpreferences("pref2", mode_private); sharedpreferences.editor editor = prefs.edit(); editor.putstring("txt2", s); editor.putstring("lastactivity2", getclass().getname()); editor.apply(); } }
Comments
Post a Comment