Reputation: 1600
I'm currently developing an Android app and I've encountered the following problem: Everytime I close the app or it crashes, a few seconds later, it opens again, all by itself.
I've narrowed the problem the PlanItApp class, wich inherits from Application. All the login process is done in that class and, when it finishes, I call de Main activity from this same class. Here's the troubling part (this an inner class inside PlanItApp wich is used to retrieve data from the server):
public class BaseAsyncDataRetriever extends AsyncTask<PlanItApp, Void, Void>
{
@Override
protected Void doInBackground(PlanItApp... params) {
setContacts();
PlanItApp app = params[0];
final CountDownLatch latch = new CountDownLatch(1);
Thread t1 = new Thread(new Runnable() {
@Override
public void run() {
invitedEventsAux = toLinkedList(PlanItApp.client.getUserInvitedEvents(PlanItApp.PIiD));
latch.countDown();
}
});
});
t1.start();
try {latch.await();
} catch (InterruptedException e) {e.printStackTrace();}
return null;
}
@Override
protected void onPostExecute() {
super.onPostExecute();
Intent i = new Intent(getApplicationContext(), Main.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);
}
}
And, if I comment out startActivity(i); the app won't open by itself. Even if onPostExecute() is called and Main activity is opened, if I close the app after, it will open itself a few seconds later.
Since I have absolutely no clue what the problem might be, I don't know what other piece of code might be relevant. Any insight could be useful.
Upvotes: 0
Views: 3002
Reputation: 1600
I finally found a solution. The problem was that on the onCreate() method from the PlanIt class I called an async class wich would, eventually, start the Main activity. The problem was that, when the application was closed, the OS would re open it (this happens to every other process) and call the onCreate() method again, and reopen the app. This is the reason why there is a launcher class.
The solution was to move the activity instancing logic to the launcher class, which doesn't get called everytime the app is closed, only when the user opens it.
Upvotes: 0
Reputation: 3022
It finishes the execution of doInBackground
and then moves into onPostExecute
which calls startActivity
eventually. You closing the app doesn't change anything, once the background thread finishes, onPostExecute
automatically gets called.
On top of my head, try to keep a global boolean for successful operation in doInBackground
and only if it's set then call startActivity which of course must be unset in onPause()
or onStop()
.
There might be better solutions though.
Update: Do you really need that new Thread? You know, AsyncTask
automatically creates a new Thread
for you, so everything you do in doInBackground()
is already on that thread.
If you need to have a new Thread
inside doInBackground()
, the proper way to do it is to wait for it to finish and call join()
for that thread.
Here is the example
Upvotes: 3