In Activity lifecycle the onActivityResult method is called before the onCreate methode, where all my buttons etc. are initialized.
How is it possible to access them though? Thx for some background knowledge!
The only way to an onActivityResult event should happen is if you used startActivityForResult to create the new activity that generates that event.
Which means your calling activity should be in the stack and already created but just paused so your onCreate was already called.
Unless there is an orientation change in between which I have never tried and poses an interesting question.
Are you having an issue or just trying to understand the mechanism? If you are having and issue post some code.
Related
I'm actually using recreate() method to restart an Activity, but this method doesn't clear the EditTexts inside the Activity.
How can i solve this?
If you just want to clear your EditText without recreate the entire activity, you should use setText() method to clear it properly.
Like this:
yourEditText.setText("");
As per the documentation for recreate, the call to recreate Cause the Activity to be recreated with a new instance. This results in essentially the same flow as when the Activity is created due to a configuration change -- the current instance will go through its lifecycle to onDestroy() and a new instance then created after it.
So basically recreate() doesn't actually act the same way as totally recreating the activity.
For example: if you have any Fragments with setRetainInstance(true) they won't be recreated; merely paused and resumed.
One more catch with recreate API is it is supported from API Level 11 and Above. Hence, use of recreate is ok if your app is only targeting SDK level 11 and above.
Check if you are using the setRetainInstance(true) in your code.
You need to may be show more code to understand the specific problem!
I´ve just realised that android has another method called onActivityReenter.
What is this for? can it be used like onActivityResult?
Documentation says that is used for transitions, but not 100% what for.
according to the doc
The purpose of this function is to let the called Activity send a hint about its state so that this underlying Activity can prepare to be exposed. A call to this method does not guarantee that the called Activity has or will be exiting soon. It only indicates that it will expose this Activity's Window and it has some data to pass to prepare it.
so you can get data from the other activity even when it is still running. but for the onActivityResult we get the data when the activity is finished.
Is there a method of the Activity lifecycle that is guaranteed to get called when my app crashes?
I put logs in all methods and crashed my app but I didn't see anything.
I couldn't find anything in the docs too.
I basically want to save changes in a database but ideally I do not want to do it in every update but rather in a method like onPause or onStop etc.
None of the lifecycle methods will be called if your Activity crashes.
However, for child activities started using startActivityForResult you do get a RESULT_CANCELED code returned in onActivityResult on the parent Activity.
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 a a 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 activity in the manifest is singleTask. The onNewIntent method is called when the activity is called again. I do intialize the Textviews and other views in my onCreate but sometimes the app crashes in onNewIntent where I do not initialize any variables and directly use the method of that view. For eg, setText method of TextView. Sometimes it throws NullPointerException and do not know why does it do so. In other screen where I have done the same thing it never does that.
Do I need to instantiate the variables again in onNewIntent?
Did you try solution that proposed by user znq in this discussion? It should clarify the situation. Now it seems a bit strange. May be problem elsewhere.