3

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.

Yusuf K.
  • 4,195
  • 1
  • 33
  • 69
Mahdi
  • 6,139
  • 9
  • 57
  • 109

2 Answers2

2

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:
.
.
.
.

    }
}
Mahdi
  • 6,139
  • 9
  • 57
  • 109
0

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

How to detect application exit on android?

Community
  • 1
  • 1
StephenG
  • 2,851
  • 1
  • 16
  • 36