Android Dialog Showing in 2 activities instead of one -
i relatively new android studio , have problem dialog box. want display dialog box once after application installed in profile_activity. shows in main activity afterwards , believe has context in mainactivity passing context of static class. here's code i`d appreciate if explain why , if there's way fix this. mainactivity extends profile_activity:
public class mainactivity extends profile_activity { private static context mcontext; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); mcontext = this.getapplicationcontext(); //---setting property greeting message , styling bit---// textview textout; textout = (textview) findviewbyid(r.id.textview2); textout.settextcolor(color.parsecolor("#ffffff")); textout.setgravity(gravity.center); textout.settextsize(getresources().getdimension(r.dimen.textsize)); textout.settypeface(null, typeface.bold_italic); textout.settext("hello " + edittext.gettext() + ", pick 1 of zodiac symbols below " + "to find out stars holding today!"); //---initiallize properties of buttons used in main activity page ---/ imagebutton aquabutton; aquabutton = (imagebutton) findviewbyid(r.id.baquarius); imagebutton piscesbutton; piscesbutton = (imagebutton) findviewbyid(r.id.bpisces); imagebutton ariesbutton; ariesbutton = (imagebutton) findviewbyid(r.id.baries); imagebutton taurusbutton; taurusbutton = (imagebutton) findviewbyid(r.id.btaurs); imagebutton geminibutton; geminibutton = (imagebutton) findviewbyid(r.id.bgemini); imagebutton cancerbutton; cancerbutton = (imagebutton) findviewbyid(r.id.bcancer); imagebutton leobutton; leobutton = (imagebutton) findviewbyid(r.id.bleo); imagebutton virgobutton; virgobutton = (imagebutton) findviewbyid(r.id.bvirgo); imagebutton librabutton; librabutton = (imagebutton) findviewbyid(r.id.blibra); imagebutton scorpiobutton; scorpiobutton = (imagebutton) findviewbyid(r.id.bscorpio); imagebutton sagittariusbutton; sagittariusbutton = (imagebutton) findviewbyid(r.id.bsagittarius); imagebutton capributton; capributton = (imagebutton) findviewbyid(r.id.bcapricorn); //---setting actions each button when pressed---/ aquabutton.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { intent aquaactivity = new intent(mainactivity.this, aqua_activity.class); startactivity(aquaactivity); } }); piscesbutton.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { intent piscesactivity = new intent(mainactivity.this, pisces_activity.class); startactivity(piscesactivity); } }); ariesbutton.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { intent ariesactivity = new intent(mainactivity.this, aries_activity.class); startactivity(ariesactivity); } }); taurusbutton.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { intent taurusactivity = new intent(mainactivity.this, taurus_activity.class); startactivity(taurusactivity); } }); geminibutton.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { intent geminiactivity = new intent(mainactivity.this, gemini_activity.class); startactivity(geminiactivity); } }); cancerbutton.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { intent canceractivity = new intent(mainactivity.this, cancer_activity.class); startactivity(canceractivity); } }); leobutton.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { intent leoactivity = new intent(mainactivity.this, leo_activity.class); startactivity(leoactivity); } }); virgobutton.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { intent virgoactivity = new intent(mainactivity.this, virgo_activity.class); startactivity(virgoactivity); } }); librabutton.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { intent libraactivity = new intent(mainactivity.this, libra_activity.class); startactivity(libraactivity); } }); scorpiobutton.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { intent scorpioactivity = new intent(mainactivity.this, scorpio_activity.class); startactivity(scorpioactivity); } }); sagittariusbutton.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { intent sagittactivity = new intent(mainactivity.this, sagittarius_activity.class); startactivity(sagittactivity); } }); capributton.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { intent capriactivity = new intent(mainactivity.this, capricorn_activity.class); startactivity(capriactivity); } }); } //--initializing static methods navigation--// private static void gotocapriact() { intent capriacti = new intent(mcontext, capricorn_activity.class); mcontext.startactivity(capriacti); } private static void gotoaquaact() { intent aquaacti = new intent(mcontext, aqua_activity.class); mcontext.startactivity(aquaacti); } private static void gotopiscact() { intent piscacti = new intent(mcontext, pisces_activity.class); mcontext.startactivity(piscacti); } private static void gotoariesact() { intent ariesacti = new intent(mcontext, aries_activity.class); mcontext.startactivity(ariesacti); } private static void gototauract() { intent taursacti = new intent(mcontext, taurus_activity.class); mcontext.startactivity(taursacti); } private static void gotogeminiact() { intent geminiacti = new intent(mcontext, gemini_activity.class); mcontext.startactivity(geminiacti); } private static void gotocancact() { intent cancacti = new intent(mcontext, cancer_activity.class); mcontext.startactivity(cancacti); } private static void gotoleoact() { intent leoacti = new intent(mcontext, leo_activity.class); mcontext.startactivity(leoacti); } private static void gotovirgoact() { intent virgoacti = new intent(mcontext, virgo_activity.class); mcontext.startactivity(virgoacti); } private static void gotolibraact() { intent libraacti = new intent(mcontext, libra_activity.class); mcontext.startactivity(libraacti); } private static void gotoscorpact() { intent scorpacti = new intent(mcontext, scorpio_activity.class); mcontext.startactivity(scorpacti); } private static void gotosaggact() { intent saggacti = new intent(mcontext, sagittarius_activity.class); mcontext.startactivity(saggacti); } //--setting datepickerfragment--// public static class datepickerfragment extends dialogfragment implements datepickerdialog.ondatesetlistener { calendar mycalendar = calendar.getinstance(); @override public dialog oncreatedialog(bundle savedinstancestate) { // use current date default date in picker int year = mycalendar.get(calendar.year); int month = mycalendar.get(calendar.month); int day = mycalendar.get(calendar.day_of_month); // create new instance o f datepickerdialog , return int theme; if (build.version.sdk_int < 23) theme = alertdialog.theme_holo_dark; else theme = android.r.style.theme_holo_dialog; return new datepickerdialog(getactivity(), theme, this, year, month, day); } //--checks every single sign , navigation--// string message; public void ondateset(datepicker view, int year, int month, int day) { // date chosen user month = month + 1; if ((month == 12 && day >= 22 && day <= 31) || (month == 1 && day >= 1 && day <= 19)){ gotocapriact(); } else if ((month == 1 && day >= 20 && day <= 31) || (month == 2 && day >= 1 && day <= 17)){ gotoaquaact(); } else if ((month == 2 && day >= 18 && day <= 29) || (month == 3 && day >= 1 && day <= 19)) { gotopiscact(); } else if ((month == 3 && day >= 20 && day <= 31) || (month == 4 && day >= 1 && day <= 19)) { gotoariesact(); } else if ((month == 4 && day >= 20 && day <= 30) || (month == 5 && day >= 1 && day <= 20)) { gototauract(); } else if ((month == 5 && day >= 21 && day <= 31) || (month == 6 && day >= 1 && day <= 20)) { gotogeminiact(); } else if ((month == 6 && day >= 21 && day <= 30) || (month == 7 && day >= 1 && day <= 22)) { gotocancact(); } else if ((month == 7 && day >= 23 && day <= 31) || (month == 8 && day >= 1 && day <= 22)) { gotoleoact(); } else if ((month == 8 && day >= 23 && day <= 31) || (month == 9 && day >= 1 && day <= 22)) { gotovirgoact(); } else if ((month == 9 && day >= 23 && day <= 30) || (month == 10 && day >= 1 && day <= 22)) { gotolibraact(); } else if ((month == 10 && day >= 23 && day <= 31) || (month == 11 && day >= 1 && day <= 21)) { gotoscorpact();} else if ((month == 11 && day >= 22 && day <= 30) || (month == 12 && day >= 1 && day <= 21)) { gotosaggact();} else { system.out.println("illegal date");} toast toast = toast.maketext(getcontext(), message, toast.length_short); toast.show(); } } public void showdatepickerdialog(view v) { dialogfragment newfragment = new mainactivity.datepickerfragment(); newfragment.show(getsupportfragmentmanager(), "datepicker"); } public view.onclicklistener exitbuttonlitener = new view.onclicklistener() { public void onclick(view v) { notificationcall(); finish(); system.exit(0); } }; public view.onclicklistener aboutbuttonlistener = new view.onclicklistener() { public void onclick(view v) { intent about_activity = new intent(mainactivity.this, aboutactivity.class); startactivity(about_activity); } }; public void maketag(string tag) { string or = savedname.getstring(tag, null); sharedpreferences.editor preferenceseditor = savedname.edit(); preferenceseditor.putstring("tag", tag); preferenceseditor.apply(); } public view.onclicklistener savebuttonlistener = new view.onclicklistener() { public void onclick(view v) { //---setting requirements edittext property---// if (edittext.gettext().length() > 0) { maketag(edittext.gettext().tostring()); ((inputmethodmanager) getsystemservice(context.input_method_service)) .hidesoftinputfromwindow(edittext.getwindowtoken(), 0); intent main_activity = new intent(mainactivity.this, mainactivity.class); startactivity(main_activity); } } };}
and here profile_activity:
public class profile_activity extends appcompatactivity { //---setting public proerty variables inherited in main activity---// private context cnt; private string m_text = ""; public edittext edittext; sharedpreferences savedname; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_profile_activity); cnt = this; if(isfirsttime()){ alertdialog.builder builder = new alertdialog.builder(profile_activity.this); builder.settitle("title"); // set input final edittext input = new edittext(cnt); // specify type of input expected; this, example, sets input password, , mask text input.setinputtype(inputtype.type_text_variation_person_name); builder.setview(input); // set buttons builder.setpositivebutton("ok", new dialoginterface.onclicklistener() { @override public void onclick(dialoginterface dialog, int which) { m_text = input.gettext().tostring(); } }); builder.setnegativebutton("cancel", new dialoginterface.onclicklistener() { @override public void onclick(dialoginterface dialog, int which) { dialog.cancel(); } }); builder.show();} //---initiallizing properties used in profile activity---// edittext = (edittext) findviewbyid(r.id.name); savedname = getsharedpreferences("notes", mode_private); imagebutton btn1 = (imagebutton) findviewbyid(r.id.btn1); imagebutton btn2 = (imagebutton) findviewbyid(r.id.btn2); imagebutton btn3 = (imagebutton) findviewbyid(r.id.btn3); edittext.settext(savedname.getstring("tag",null)); //---initiallizing functions each button--// btn1.setonclicklistener(savebuttonlistener); btn2.setonclicklistener(aboutbuttonlistener); btn3.setonclicklistener(exitbuttonlitener); } //---creating public function notifications---// public void notificationcall(){ //---setting parameters notification---// notificationcompat.builder notifbuilder = (notificationcompat.builder) new notificationcompat.builder(this) .setdefaults(notificationcompat.default_all) .setsmallicon(r.drawable.notificaion_icon) .setticker("alert new message") .setcontenttitle("goodbye!") .setcontenttext("it nice having today! come tomorrow new horoscope!") .setautocancel(true); //---giving funcionality notification---// intent resultintent = new intent(this, mainactivity.class); //---back stack supporting navigation of notification---// taskstackbuilder stackbuilder = taskstackbuilder.create(this); stackbuilder.addparentstack(mainactivity.class); stackbuilder.addnextintent(resultintent); pendingintent resultpendingintent = stackbuilder.getpendingintent( 0, pendingintent.flag_update_current ); notifbuilder.setcontentintent(resultpendingintent); //---building application---// notificationmanager notificationmanager = (notificationmanager) getsystemservice(context.notification_service); notificationmanager.notify(1, notifbuilder.build()); } private boolean isfirsttime() { sharedpreferences preferences = getpreferences(mode_private); boolean ranbefore = preferences.getboolean("ranbefore", false); if (!ranbefore) { // first time sharedpreferences.editor editor = preferences.edit(); editor.putboolean("ranbefore", true); editor.apply(); } return !ranbefore; } public view.onclicklistener exitbuttonlitener = new view.onclicklistener(){ public void onclick(view v){ notificationcall(); finish(); system.exit(0); } }; public view.onclicklistener aboutbuttonlistener = new view.onclicklistener(){ public void onclick(view v){ intent about_activity = new intent (profile_activity.this, aboutactivity.class); startactivity(about_activity); } }; public void maketag(string tag){ string or = savedname.getstring(tag, null); sharedpreferences.editor preferenceseditor = savedname.edit(); preferenceseditor.putstring("tag",tag); preferenceseditor.apply(); } public view.onclicklistener savebuttonlistener = new view.onclicklistener(){ public void onclick(view v) { //---setting requirements edittext property---// if (edittext.gettext().length()>0){ maketag(edittext.gettext().tostring()); ((inputmethodmanager) getsystemservice(context.input_method_service)) .hidesoftinputfromwindow(edittext.getwindowtoken(),0); intent main_activity = new intent(profile_activity.this, mainactivity.class); startactivity(main_activity); } } };
in mainactivity
class inside oncreate()
method have super.oncreate()
call. when profile_activity
called it's oncreate()
gets executed once, comes call mainactivity
call. that's why getting 2 dialog boxes instead of once. because of alertdialog.builder object.show()
inside profile_activity
oncreate()
.
so if remove super
call mainactivity's oncrreate()
, should resolve problem.
Comments
Post a Comment