简体   繁体   中英

Activity life cycle confusion

The life cycle of an activity is documented in many places but I couldn't find the thing I need. This is my activity, it have a constructor and the onCreate method. In my project I have also a logging in this methods and every time when I go from portrait to landscape I see that both methods are executed. Why my the constructor is called ? isn't the activity in the stack and the instance of my activity is in the memory so when the configuration change is happen, then only the oncreate and on retainistancestate should happen (of course the onResume). Why the constructor is called every time, who is calling ? Is it every time when something get changed from the configuration both methods are guaranteed to be called (one after another, in this same sequence).

public TestActivity()
{
    super(R.menu.main_menu, tag);
}
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

I was playing with my sample app but I want to know more details, can someone clarify me the scenario when the constructor is included ?, I founded aa lot of documentation about life-cycle but none explains the details when the constructor is included

Edit1: I read in some places that there is stack in witch the activities are putted in so the next time they go up and running faster, but what when the configuration get changed ? Is it must to to call the constructor and the oncreate methods ?

On rotation your activity will be restarted complete. You can prevent that with android:configChanges="keyboardHidden| orientation" in your manifest.

As @rekire answered, the activity is restarted on screen rotation. Here restart means that the framework creates another instance of the activity, that's why the constructor of your activity class is called and then the onCreate(). The new activity instance replaces the old one which will be finally recycled by GC, if its reference isn't held by others.

If you want to avoid activity restart on screen rotation, please read this question .

I've drawn an UML diagram to describe the Android activity life cycle .

Hence there are no reason having constructor to invoke activity unless you have constructor with params(onCreate invoke it for us anyway...). However basically it seems like a java thing onCreate probably calling activties's default constructor which is

public ActivityName(){} // This might get call because onCreate somewhere under the hood invoking Activity :)

Try same thing with constructor with param like

public ActivityName(String s){}// This wouldn't be call unless you explicitly call it.

Hope this will help,

I would wait for more expert answer though :)


Edit : So when you rotate your phone which calls onCreate as it will get created again and onCreate probably calls default constructor to invoke instance of your activity :)... I forgot to mention this earlier.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM