java - OnCretae() doesn't count -
i have been working on these 2 methods in android studio problem comes when return activityone not add oncreate method, whereas if adds onstart, onresume , onrestart.
activityone
import android.app.activity; import android.content.intent; import android.os.bundle; import android.support.v7.app.appcompatactivity; import android.util.log; import android.view.view; import android.view.view.onclicklistener; import android.widget.button; import android.widget.textview; public class activityone extends appcompatactivity { private static final string restart_key = "restart"; private static final string resume_key = "resume"; private static final string start_key = "start"; private static final string create_key = "create"; // string logcat documentation private final static string tag = "lab-activityone"; // lifecycle counters // todo: // create counter variables oncreate(), onrestart(), onstart() , // onresume(), called mcreate, etc. // need increment these variables' values when // corresponding lifecycle methods called int mcreate = 0; int mrestart = 0; int mstart = 0; int mresume = 0; // todo: create variables each of textviews, called // mtvcreate, etc. textview mtvcreate; textview mtvstart; textview mtvresume; textview mtvrestart; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_one); // todo: assign appropriate textviews textview variables // hint: access textview calling activity's findviewbyid() // textview1 = (textview) findviewbyid(r.id.textview1); mtvcreate = (textview) findviewbyid(r.id.create); mtvstart = (textview) findviewbyid(r.id.start); mtvresume = (textview) findviewbyid(r.id.resume); mtvrestart = (textview) findviewbyid(r.id.restart); button launchactivitytwobutton = (button) findviewbyid(r.id.blaunchactivitytwo); launchactivitytwobutton.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { // todo: // launch activity 2 // hint: use context's startactivity() method // create intent stating activity start intent intent = new intent(activityone.this, activitytwo.class); // launch activity using intent startactivity(intent); } }); // check saved state if (savedinstancestate != null) { // todo: // restore value of counters saved state // need 4 lines of code, 1 every count variable mcreate = savedinstancestate.getint(create_key); mrestart = savedinstancestate.getint(restart_key); mstart = savedinstancestate.getint(start_key); mresume = savedinstancestate.getint(resume_key); } // update appropriate count variable mcreate++; // todo: emit logcat message log.i(tag, "entered oncreate() method"); // todo: // update user interface via displaycounts() method displaycounts(); } // lifecycle callback overrides @override public void onstart() { super.onstart(); // update appropriate count variable mstart++; // todo: emit logcat message log.i(tag, "entered onstart() method"); // todo: // update user interface displaycounts(); } @override public void onresume() { super.onresume(); // emit logcat message mresume++; log.i(tag, "entered onresume() method"); // todo: displaycounts(); // update appropriate count variable // update user interface } @override public void onpause() { super.onpause(); // emit logcat message log.i(tag, "entered onpause() method"); } @override public void onstop() { super.onstop(); // emit logcat message log.i(tag, "entered onstop() method"); } @override public void onrestart() { super.onrestart(); // emit logcat message mrestart++; log.i(tag, "entered onrestart() method"); // todo: displaycounts(); // update appropriate count variable // update user interface } @override public void ondestroy() { super.ondestroy(); // emit logcat message log.i(tag, "entered ondestroy() method"); } @override public void onsaveinstancestate(bundle savedinstancestate) { // todo: // save counter state information collection of key-value pairs // 4 lines of code, 1 every count variable log.i(tag, "saving instance state"); super.onsaveinstancestate(savedinstancestate); savedinstancestate.putint(create_key, mcreate); savedinstancestate.putint(restart_key, mrestart); savedinstancestate.putint(resume_key, mresume); savedinstancestate.putint(start_key, mstart); } // updates displayed counters // method expects counters , textview variables use // names // specified above public void displaycounts() { // todo - uncomment these lines mtvcreate.settext("oncreate() calls: " + mcreate); mtvstart.settext("onstart() calls: " + mstart); mtvresume.settext("onresume() calls: " + mresume); mtvrestart.settext("onrestart() calls: " + mrestart); } }
activitytwo
import android.app.activity; import android.content.intent; import android.os.bundle; import android.support.v7.view.menu.actionmenuitemview; import android.util.log; import android.view.view; import android.view.view.onclicklistener; import android.widget.button; import android.widget.textview; public class activitytwo extends activity { // use these keys when you're saving state between reconfigurations private static final string restart_key = "restart"; private static final string resume_key = "resume"; private static final string start_key = "start"; private static final string create_key = "create"; // string logcat documentation private final static string tag = "lab-activitytwo"; // lifecycle counters // todo: public integer mcreate = 0; public integer mrestart = 0; public integer mstart = 0; public integer mresume = 0; // create variables named // mcreate, mrestart, mstart , mresume // count calls oncreate(), onrestart(), onstart() , // onresume(). these variables should not defined static. // need increment these variables' values when // corresponding lifecycle methods called. // todo: create variables each of textviews textview mtvcreate; textview mtvrestart; textview mtvstart; textview mtvresume; // named mtvcreate, mtvrestart, mtvstart, mtvresume. // displaying current count of each counter variable button closebutton; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_two); // todo: assign appropriate textviews textview variables // hint: access textview calling activity's findviewbyid() // textview1 = (textview) findviewbyid(r.id.textview1); mtvcreate = (textview) findviewbyid(r.id.create); mtvstart = (textview) findviewbyid(r.id.start); mtvresume = (textview) findviewbyid(r.id.resume); mtvrestart = (textview) findviewbyid(r.id.restart); closebutton = (button)findviewbyid(r.id.bclose); closebutton.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { finish(); } }); // check saved state if (savedinstancestate != null) { // todo: // restore value of counters saved state // need 4 lines of code, 1 every count variable mcreate = savedinstancestate.getint(create_key); mrestart = savedinstancestate.getint(restart_key); mstart = savedinstancestate.getint(start_key); mresume = savedinstancestate.getint(resume_key); } // update appropriate count variable mcreate++; // todo: emit logcat message log.i(tag, "entered oncreate() method"); // todo: // update user interface via displaycounts() method displaycounts(); } // lifecycle callback methods overrides @override public void onstart() { super.onstart(); // emit logcat message mstart++; log.i(tag, "entered onstart() method"); // todo: displaycounts(); // update appropriate count variable // update user interface } @override public void onresume() { super.onresume(); // emit logcat message mresume++; log.i(tag, "entered onresume() method"); // todo: displaycounts(); // update appropriate count variable // update user interface } @override public void onpause() { super.onpause(); // emit logcat message log.i(tag, "entered onpause() method"); } @override public void onstop() { super.onstop(); // emit logcat message log.i(tag, "entered onstop() method"); } @override public void onrestart() { super.onrestart(); // emit logcat message mrestart++; log.i(tag, "entered onrestart() method"); // todo: displaycounts(); // update appropriate count variable // update user interface } @override public void ondestroy() { super.ondestroy(); // emit logcat message log.i(tag, "entered ondestroy() method"); } @override public void onsaveinstancestate(bundle savedinstancestate) { // todo: // save counter state information collection of key-value pairs // 4 lines of code, 1 every count variable log.i(tag, "saving instance state"); super.onsaveinstancestate(savedinstancestate); savedinstancestate.putint(create_key, mcreate); savedinstancestate.putint(restart_key, mrestart); savedinstancestate.putint(resume_key, mresume); savedinstancestate.putint(start_key, mstart); } // updates displayed counters // method expects counters , textview variables use // names // specified above public void displaycounts() { // todo - uncomment these lines mtvcreate.settext("oncreate() calls: " + mcreate); mtvstart.settext("onstart() calls: " + mstart); mtvresume.settext("onresume() calls: " + mresume); mtvrestart.settext("onrestart() calls: " + mrestart); } }
when return activityone not add oncreate method
because android lifecycle didn't call when return activitytwo
check life cycle
Comments
Post a Comment