0

I'm still new at java programming (for Android), usually I find everything that I need to know on the internet, but not this time, even if the problem doesn't seems that much complicated.

I would like to show an Ad (RevMob) when the user exits the application, an then kill the app.

If I do this, it kills the app before showing the ad:

public void onBackPressed() {   
  super.onBackPressed();

  RevMob revmob = RevMob.start(this); 
  revmob.showFullscreen(this);
  android.os.Process.killProcess(android.os.Process.myPid());    
}

So it thought about waiting 10seconds before killing the app, but I don't know how to do it correctly. I tried a lot of things, like this:

public void onBackPressed() {
    super.onBackPressed();
    onBackPressedExit();

    RevMob revmob = RevMob.start(this); 
    revmob.showFullscreen(this);
}

private void onBackPressedExit() {       
    try {
        Thread.sleep(10000);
        android.os.Process.killProcess(android.os.Process.myPid());
    }
    catch(InterruptedException ie) {
    }           
}

What should I do? Thanks

Hashbrown
  • 12,091
  • 8
  • 72
  • 95
tchayk
  • 1
  • 2

1 Answers1

1

Try this, first you finish up showing whatever ad you want and finally call finish, this will close the application. This code willl take care of the back key exit scenario.

@Override
public void onBackPressed() {
    //Do your stuff
    finish();
    return;
} 
Aadi Droid
  • 1,689
  • 4
  • 22
  • 46
  • Thanks a lot. Is the application killed so I don't have to use android.os.Process.killProcess(android.os.Process.myPid()); then ? – tchayk Oct 12 '14 at 14:42