Doing some testing but cannot fabricate debug environment
so maybe someone can answer this.
In my Activity i start an AsyncTask.
This AsyncTask is writing to Sqlite and it take long time.
I thought i could start the AsyncTask when user press the “Send” Button
and then hit the back button to finish() the Activity.
I know AsyncTask will keep on running even do Activity is finish() right.
The question is how do keep the fields alive?
What happen with the localArrayPeople in this code if parent finish()?
private class AsyncTaskDoStuff extends AsyncTask<Long, Integer, Integer>
{
ArrayList localArrayPeople;
@Override
protected Integer doInBackground(Long... params) {
this.localArrayPeople = arrayPeople;
// Do stuff...
}
}
I know i can/should use a Service but the amount of data/Object’s to send to the Service
is really much.
I know this is a bad ide so please for education purpose just answer the question and dont warn me
To answer yor question. Objects in java programs never just “go away”, so fields don’t loose references, e.g. suddenly start pointing to null. Objects are only removed by GC when nobody references them.
In your case, when Activity stops, the fields in background thread will not be affected.
The only problem could be that Android OS decides to remove your app from memory and kills all it’s threads. This can happen if you have a really long running background thread and your app is inactive (= activity not showing).