简体   繁体   中英

In android - how to I make an activity launch when someone closes an app?

I'd like to add an activity to my app asking users to rate it, but I'd rather have it only launch when they are exiting out of the app using the back button (so that it doesn't interfere with usefulness). I have a few apps that if I exit out by repeatedly hitting the back button, I get a toast that says 'Tap back to exit ______' so I'm pretty sure that this is possible.

You can start another activity using intent on your onBackPressed method. With this approach you can fire an intent to google play. But i think it would be frustrating for user. In my opinion better way of achieving what you want is using some kind of popup. This library might help you out.

As far as I see I would do it like in the following code(not tested, just a snippet):

boolean exitApp = false;

@Override
public void onBackPressed() {
    if (exitApp && (isBackStackEntryEmpty())) {
        super.onBackPressed();
        return;
    }
    //put here a dialog check
    checkDialog();  
}

private void checkDialog(){
  //show a dialog with an ok or a cancel.
  //in the cancel it will ask you for a second press.
  //if ok is pressed go to rate else, cancel, see below.
  this.exitApp = true;
  showToast();
  postDelayed();
}

private Handler handler = new Handler();
private boolean isBackStackEntryEmpty(){
   return isBackStackEntryEmpty;// check for that in code.
}

private void postDelayed()
{
   handler.postDelayed(new Runnable()
   {
      @Override
      public void run() {
           this.exitApp = false;                      
      }
   }, 3000);
}

public void showToast(){
   Toast.makeText(this, "Please click again to exit", Toast.LENGTH_SHORT).show();
}


Similar:
Clicking the back button twice to exit an activity

Just a thought...record whether or not a user was shown the popup asking for a review in shared preferences so that it prevents the app from asking again. But you can't force people to rate your app, only choose not to show them the reminder again

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM