I have to send some requests to my server before app exits.
But as my app close all asynctasks are killed and the requests aren't being sent to my web service.
I have to send some requests to my server before app exits.
But as my app close all asynctasks are killed and the requests aren't being sent to my web service.
I finally solve this by put exit function in my main activity and passing and extra so it can find out I open it for closing whole app. this is very good solution.
call main activity like this:
Intent i = new Intent(G.currentActivity, MainActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
i.putExtra("isExit", true);
and get extra like this:
private void getExtras() {
Intent myIntent = getIntent();
if (myIntent.getBooleanExtra("isExit", false)) {
showExitDialog(MainActivity.this);
}
}
Update: It can be handled better with Eventbus. I have main activity that loads fragments with ChangePageEventMessage event that contains eventType that Enum.
public onEvent(ChangePageEventMessage event){
switch(event.getType()){
case appExit: dosomething();
System.exit(0);
break;
case detail:
.
.
.
.
}
}
Make a YourBasicActivity and override its OnPause()
method and extend every Activity from YourBasicActivity
Run code when Android app is closed/sent to background
You can also try onTaskRemoved