android - Something went wrong with the Activity lifecycle, not normal behavior by pressing home or task switch button -


something not ok in code. activity lifecycle - @ starting app: oncreate --> onstart --> onresume - @ closing app: onpause --> onstop --> ondestroy - pressing home button or task switch button: onpause --> onstop - coming opened app app: onstart --> onresume

but reason app calls ondestroy when press home button or task switch button, , calls oncreate, when go opened app.

my app has 2 activites: - loginactivity, should appear first, if nobody logged on, or if logout button pressed - mainactivity handle main functions.

please me identify causes not normal behavior of lifecycle.

thanks!

mainactivity:

public class mainactivity extends appcompatactivity     implements navigationview.onnavigationitemselectedlistener {  public helper helper = new helper(this);  public static final string sp_data = "sp_data"; public static final string fb_login_status = "fb_login_status"; public static final string flrt = "flrt";  public sharedpreferences sharedpreferences;  public fragmentmanager fragmentmanager = getfragmentmanager();  @override protected void oncreate(bundle savedinstancestate) {     log.d(flrt, "oncreate");     helper.maketoast("oncreate");      sharedpreferences = getsharedpreferences(sp_data, mode_private);     boolean balreadyloggedin = sharedpreferences.getboolean(fb_login_status, false);      if (balreadyloggedin) {         log.d(flrt, "already logged in");     }     else {         log.d(flrt, "not logged in");          intent intent = new intent(mainactivity.this, loginactivity.class);         startactivity(intent);     }      super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_main);     toolbar toolbar = (toolbar) findviewbyid(r.id.toolbar);     setsupportactionbar(toolbar);       showfragment(new firstfragment(),"firstfragment");      button btnlogout = (button) findviewbyid(r.id.btnlogout);      btnlogout.setonclicklistener(new view.onclicklistener() {         @override         public void onclick(view v) {              logout();              showactivity(mainactivity.this, loginactivity.class);         }     });      floatingactionbutton fab = (floatingactionbutton) findviewbyid(r.id.fab);     fab.setonclicklistener(new view.onclicklistener() {         @override         public void onclick(view view) {             snackbar.make(view, "replace own action", snackbar.length_long)                     .setaction("action", null).show();         }     });      drawerlayout drawer = (drawerlayout) findviewbyid(r.id.drawer_layout);     actionbardrawertoggle toggle = new actionbardrawertoggle(             this, drawer, toolbar, r.string.navigation_drawer_open, r.string.navigation_drawer_close);     drawer.setdrawerlistener(toggle);     toggle.syncstate();      navigationview navigationview = (navigationview) findviewbyid(r.id.nav_view);     navigationview.setnavigationitemselectedlistener(this); }  private void showactivity(context context_fromactivity, class<?> class_toactivty){     intent intent = new intent(context_fromactivity, class_toactivty);     //intent.addflags(intent.flag_activity_clear_top);      startactivity(intent);      mainactivity.this.finish(); }  private void logout() {     log.d(flrt, "logging out...");      sharedpreferences = getsharedpreferences(sp_data, mode_private);     sharedpreferences.editor editor = sharedpreferences.edit();     editor.putboolean(fb_login_status, false);     editor.commit(); }  @override public void onbackpressed() {     drawerlayout drawer = (drawerlayout) findviewbyid(r.id.drawer_layout);     if (drawer.isdraweropen(gravitycompat.start)) {         drawer.closedrawer(gravitycompat.start);     } else if (getfragmentmanager().getbackstackentrycount() > 1 ){         getfragmentmanager().popbackstack();     } else {         super.onbackpressed();     } }  @override public boolean oncreateoptionsmenu(menu menu) {     // inflate menu; adds items action bar if present.     getmenuinflater().inflate(r.menu.main, menu);     return true; }  @override public boolean onoptionsitemselected(menuitem item) {     // handle action bar item clicks here. action bar     // automatically handle clicks on home/up button, long     // specify parent activity in androidmanifest.xml.     int id = item.getitemid();      //noinspection simplifiableifstatement     if (id == r.id.action_settings) {         return true;     }      return super.onoptionsitemselected(item); }  @suppresswarnings("statementwithemptybody") @override public boolean onnavigationitemselected(menuitem item) {     switch (item.getitemid()) {         case r.id.nav_first_layout:             showfragment(new firstfragment(), "firstfragment");             break;         case r.id.nav_second_layout:             showfragment(new secondfragment(), "secondfragment");             break;         case r.id.nav_third_layout:             showfragment(new thirdfragment(), "thirdfragment");             break;         case r.id.nav_share:              break;         case r.id.nav_send:              break;     }      drawerlayout drawer = (drawerlayout) findviewbyid(r.id.drawer_layout);     drawer.closedrawer(gravitycompat.start);     return true; }  private void showfragment(fragment fragment, string sfragmenttag) {     fragmenttransaction fragmenttransaction = fragmentmanager.begintransaction();      if (fragmentmanager.findfragmentbytag(sfragmenttag) != null) {         log.d(flrt, "fragment found, using existing one: " + sfragmenttag);         fragment = fragmentmanager.findfragmentbytag(sfragmenttag);     }      fragmenttransaction.replace(r.id.fragmentcontainer, fragment, sfragmenttag);     fragmenttransaction.addtobackstack(null);     fragmenttransaction.commit(); }  @override protected void onstart() {     super.onstart();     log.d(flrt, "onstart");     helper.maketoast("onstart"); }  @override protected void onresume() {     super.onresume();     log.d(flrt, "onresume");     helper.maketoast("onresume"); }  @override protected void onpause() {     super.onpause();     log.d(flrt, "onpause");     helper.maketoast("onpause"); }  @override protected void onstop() {     super.onstop();     log.d(flrt, "onstop");     helper.maketoast("onstop"); }  @override protected void ondestroy() {     super.ondestroy();     log.d(flrt, "ondestroy");     helper.maketoast("ondestroy"); } } 

loginactivity:

public class loginactivity extends appcompatactivity {  public static final string sp_data = "sp_data"; public static final string fb_login_status = "fb_login_status"; public static final string flrt = "flrt";  public sharedpreferences sharedpreferences;  private button btn_start;  @override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_login);      btn_start = (button) findviewbyid(r.id.btn_start);      btn_start.setonclicklistener(new view.onclicklistener() {         @override         public void onclick(view v) {              login();              showactivity(loginactivity.this, mainactivity.class);;         }     }); }  private void showactivity(context context_fromactivity, class<?> class_toactivty){     intent intent = new intent(context_fromactivity, class_toactivty);     //intent.addflags(intent.flag_activity_clear_top);      startactivity(intent);      loginactivity.this.finish(); }  private void login() {     log.d(flrt, "logging in...");      sharedpreferences = getsharedpreferences(sp_data, mode_private);     sharedpreferences.editor editor = sharedpreferences.edit();     editor.putboolean(fb_login_status, true);     editor.commit(); } } 

nothing surprising here,

the behavior of ondestroy() method

in situations system kill activity's hosting process without calling method. or system can directly destroy app if getting low memory situations app in onstop() event, , thing notice onstop() method may not called, in low memory situations.

see ondestroy() method in details.


Comments

Popular posts from this blog

Ansible - ERROR! the field 'hosts' is required but was not set -

customize file_field button ruby on rails -

SoapUI on windows 10 - high DPI/4K scaling issue -