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.
Related
Why Is onCreate() Preferred To Do All The Main App Tasks? Why not onResume() or onStart()? Why only onCreate()? I tried to do the main tasks like binding findViewById() setting text to text views and a lot more. They all work fine. When why do we always are preferred to do that task in onCreate()?
OnCreate serves as the first entry point into your activity, so logically it makes sense to do as much of the initialization here as possible. Often times there are cases when things need to get configured with higher priority - crash reporting services, dependency injection etc. where this would get escalated to a custom application class.
according to the docs
protected void onCreate (Bundle savedInstanceState)
Called when the activity is starting. This is where most initialization should go: calling setContentView(int) to inflate the activity's UI, using findViewById(int) to programmatically interact with widgets in the UI...
so, I suppose it is fair to say that most initialization will get done inside of onCreate, which usually means that if you were to place this into a lifecycle method which could get executed repeatedly, that could be considered redundant as you'd be assigning the same values to variables repeatedly, unless that's something you actually want to do.
However, lazy initialization is also a concept to keep in mind, being able to initialize something inside of onCreate doesn't always mean that you should, it is often times better to delay initialization until you actually need the instance.
regarding
I tried to do the main tasks like binding findViewById() setting text to text views and a lot more. They all work fine.
they definitely would, findViewById can always be used and isn't limited to being inside of onCreate, in fact the result of findViewById doesn't even have to be assigned to a variable for you to be able to use it
It looks like anything that occurs in onResume() wont have to be placed in onCreate since onResume() is always run after onCreate()? I'm asking this because i have an algorithm that must be executed every time a user comes back to the main activity. If it is already in onResume() then do i have to have it in onCreate()?
No, you don't need it. onResume is always called after onCreate so if onCreate runs so will onResume.
You are correct. onResume is always called after onCreate.
Basically so. but you need to think what pause/resume and start/stop mean for your app. Also when you are in OnCreate, the activity is not yet created, where as in onStart is it. This may have impact in properties of the activity that you seek to use/change.
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.
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.
I added TextToSpeech to my app, following the guidelines in the following post:
http://android-developers.blogspot.com/2009/09/introduction-to-text-to-speech-in.html
and now my onDestroy is no longer called when the back button is pressed.
I filed a bug report regarding this: http://code.google.com/p/android/issues/detail?id=7674
Figured i should also ask here if someone else has seen this, and found a solution?
It seems that it is the intent that causes the problem, i.e. the following:
Intent checkIntent = new Intent();
checkIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
startActivityForResult(checkIntent, MY_DATA_CHECK_CODE);
If I skip this intent, and just go ahead and create a tts-instance, it works fine.
Any clues to what is wrong with this intent?
Think I've figured this one out.
My problem is that I was assuming that onDestroy would be called when my activity finished, so that I could store the state (and preferences, et c). And I assumed that onDestroy would always happen before a new instance of the activity was created, so that the new instance in onCreate could load the state stored by the old instance.
This does not hold in general. It does not even hold true for onStop.
The solution for me was simply to save what I wanted in onPause. It seems I can count on this one being called before any new instance can be created. But since onPause is called in many cases where I don't need to save, I also check isFinishing(). I.e. if isFinishing() in onPause, then I save.
Note that it didn't seem to matter if I launched my activity in singleTop mode, I would still get two "alive" instances. One which was on its way to being destroyed (onPause was called but was yet to enter onStop or onDestroy) and one which was in onCreate.
Anyway, I hope I've solved it now.
Reproduced.
It appears that the key is where you call your initTTS() method (or equivalent) from.
If it is called from onCreate(), I also see the behaviour above (onDestroy never called).
Hinted by the doc for startActivityForResult (where calling from onCreate is a special case), i tried invoking the intent via a delayed message to my own Handler.
Now, onDestroy is called again!
(also commented this on your bug report)