login - My Android alert appears twice after logging in -


i'm testing out notification tells user of android app whether or not there internet connection. have inserted code onresume function, seen below:

@override protected void onresume(){     super.onresume();     alertdialog.builder builder = new alertdialog.builder(this);     builder.setpositivebutton(r.string.ok, new dialoginterface.onclicklistener() {            public void onclick(dialoginterface dialog, int id) {                dialog.dismiss();            }        });     if(isnetworkconnected()){         builder.setmessage(r.string.yes_internet)            .settitle(r.string.title);     }     else{         builder.setmessage(r.string.no_internet)            .settitle(r.string.title);     }     alertdialog dialog = builder.create();     dialog.show(); }  private boolean isnetworkconnected() {       connectivitymanager cm = (connectivitymanager) getsystemservice(context.connectivity_service);       networkinfo ni = cm.getactivenetworkinfo();       if (ni == null) {           // there no active networks.           return false;       }       else{           return true;       } } 

which works fine. except part after log in, see alert, , dismiss it, identical alert appears. happens after logging in, not happen if leave app , come it. login activity (which 1 automatically generated eclipse) called oncreate, run when app launched. so, appear login activity causing onresume called twice. have no idea be.

here code login activity:

/**  * activity displays login screen user, offering registration  * well.  */ public class loginactivity extends activity { /**  * dummy authentication store containing known user names , passwords.  * todo: remove after connecting real authentication system.  */ private static final string[] dummy_credentials = new string[] {         "foo@example.com:hello", "bar@example.com:world" };  /**  * default email populate email field with.  */ public static final string extra_email = "com.example.android.authenticatordemo.extra.email";  /**  * keep track of login task ensure can cancel if requested.  */ private userlogintask mauthtask = null;  // values email , password @ time of login attempt. private string memail; private string mpassword;  // ui references. private edittext memailview; private edittext mpasswordview; private view mloginformview; private view mloginstatusview; private textview mloginstatusmessageview;  @override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);      setcontentview(r.layout.activity_login);      // set login form.     memail = getintent().getstringextra(extra_email);     memailview = (edittext) findviewbyid(r.id.email);     memailview.settext(memail);      mpasswordview = (edittext) findviewbyid(r.id.password);     mpasswordview             .setoneditoractionlistener(new textview.oneditoractionlistener() {                 @override                 public boolean oneditoraction(textview textview, int id,                         keyevent keyevent) {                     if (id == r.id.login || id == editorinfo.ime_null) {                         attemptlogin();                         return true;                     }                     return false;                 }             });      mloginformview = findviewbyid(r.id.login_form);     mloginstatusview = findviewbyid(r.id.login_status);     mloginstatusmessageview = (textview) findviewbyid(r.id.login_status_message);      findviewbyid(r.id.sign_in_button).setonclicklistener(             new view.onclicklistener() {                 @override                 public void onclick(view view) {                     attemptlogin();                 }             }); }  @override public boolean oncreateoptionsmenu(menu menu) {     super.oncreateoptionsmenu(menu);     getmenuinflater().inflate(r.menu.login, menu);     return true; }  /**  * attempts sign in or register account specified login form.  * if there form errors (invalid email, missing fields, etc.),  * errors presented , no actual login attempt made.  */ public void attemptlogin() {     if (mauthtask != null) {         return;     }      // reset errors.     memailview.seterror(null);     mpasswordview.seterror(null);      // store values @ time of login attempt.     memail = memailview.gettext().tostring();     mpassword = mpasswordview.gettext().tostring();      boolean cancel = false;     view focusview = null;      // check valid password.     if (textutils.isempty(mpassword)) {         mpasswordview.seterror(getstring(r.string.error_field_required));         focusview = mpasswordview;         cancel = true;     } else if (mpassword.length() < 4) {         mpasswordview.seterror(getstring(r.string.error_invalid_password));         focusview = mpasswordview;         cancel = true;     }      // check valid email address.     if (textutils.isempty(memail)) {         memailview.seterror(getstring(r.string.error_field_required));         focusview = memailview;         cancel = true;     } else if (!memail.contains("@")) {         memailview.seterror(getstring(r.string.error_invalid_email));         focusview = memailview;         cancel = true;     }      if (cancel) {         // there error; don't attempt login , focus first         // form field error.         focusview.requestfocus();     } else {         // show progress spinner, , kick off background task         // perform user login attempt.         mloginstatusmessageview.settext(r.string.login_progress_signing_in);         showprogress(true);         mauthtask = new userlogintask();         mauthtask.execute((void) null);     } }  /**  * shows progress ui , hides login form.  */ @targetapi(build.version_codes.honeycomb_mr2) private void showprogress(final boolean show) {     // on honeycomb mr2 have viewpropertyanimator apis, allow     // easy animations. if available, use these apis fade-in     // progress spinner.     if (build.version.sdk_int >= build.version_codes.honeycomb_mr2) {         int shortanimtime = getresources().getinteger(                 android.r.integer.config_shortanimtime);          mloginstatusview.setvisibility(view.visible);         mloginstatusview.animate().setduration(shortanimtime)                 .alpha(show ? 1 : 0)                 .setlistener(new animatorlisteneradapter() {                     @override                     public void onanimationend(animator animation) {                         mloginstatusview.setvisibility(show ? view.visible                                 : view.gone);                     }                 });          mloginformview.setvisibility(view.visible);         mloginformview.animate().setduration(shortanimtime)                 .alpha(show ? 0 : 1)                 .setlistener(new animatorlisteneradapter() {                     @override                     public void onanimationend(animator animation) {                         mloginformview.setvisibility(show ? view.gone                                 : view.visible);                     }                 });     } else {         // viewpropertyanimator apis not available, show         // , hide relevant ui components.         mloginstatusview.setvisibility(show ? view.visible : view.gone);         mloginformview.setvisibility(show ? view.gone : view.visible);     } }  /**  * represents asynchronous login/registration task used authenticate  * user.  */ public class userlogintask extends asynctask<void, void, boolean> {     @override     protected boolean doinbackground(void... params) {         // todo: attempt authentication against network service.          try {             // simulate network access.             thread.sleep(2000);         } catch (interruptedexception e) {             return false;         }          (string credential : dummy_credentials) {             string[] pieces = credential.split(":");             if (pieces[0].equals(memail)) {                 // account exists, return true if password matches.                 return pieces[1].equals(mpassword);             }         }          // todo: register new account here.         return true;     }      @override     protected void onpostexecute(final boolean success) {         mauthtask = null;         showprogress(false);          if (success) {             finish();         } else {             mpasswordview                     .seterror(getstring(r.string.error_incorrect_password));             mpasswordview.requestfocus();         }     }      @override     protected void oncancelled() {         mauthtask = null;         showprogress(false);     } } 

i think possible scenario mainactivity's onresume called twice, assume start loginactivity in mainactivity's oncreate:

  1. mainactivity created , loginactivity launched. mainactivity still continues lifecycle - onresume called (and onpause right after etc..)...
  2. when finish loginactivity, mainactivity resumed again - onresume called second time.

you can consider saving boolean value (or whole dialog) preserve showing dialog second time, or can change workflow mainactivity started after successful login.


Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -