0

I have a app, and when the activity is tabbed out and "swiped" then I want the app to toast a message. "Oof". I was looking through some stackoverflow questions, and I found this as an answer:

public void onDestroy() {

    Toast.makeText(this, "Thanks my dude",
            Toast.LENGTH_LONG).show();
    super.onDestroy();

}

However this does not trigger upon my actions, nor when I force stop the app.

The toast, eventually will be replaced with a service call.

Jackson Ennis
  • 37
  • 1
  • 7

2 Answers2

1

Are you sure that onDestroy() was called? onDestroy() is called when

  • The system is low on resources(memory, CPU time and so on) and makes a decision to kill your activity/application
  • Somebody calls finish() on your activity
  • An Activity is not reachable anymore
  • You rotate the device
  • Some similar situations

There is no guarantee that onDestroy() was even called.

Try this:

public void onDestroy() {
    Log.d("TAG", "onDestroy called");
    Toast.makeText(this, "Oof",Toast.LENGTH_LONG).show();
    super.onDestroy();
}

In your logcat, put a filter: TAG, check if any log is generated. If this doesnt work, go with onStop()

BlueLeaf
  • 210
  • 3
  • 15
0

Override onBackPressed and show the toast there ...

  @Override
    public void onBackPressed() {
        Toast.makeText(getContext(), "Activity exiting!",Toast.LENGTH_SHORT).show();

}
MezzDroid
  • 560
  • 4
  • 11
  • Unfortunately that won't really work for me because I'm making a personal security app and I need to make sure certain actions occur when someone attempts to close the app, especially if its "armed". – Jackson Ennis Apr 06 '18 at 18:28