12

I want to show a "Thanks for using" message when the application close.

What is the event that handles application closing?

Billie
  • 8,938
  • 12
  • 37
  • 67
  • @user1479606 No - Just got it too late. – Billie Dec 09 '12 at 08:21
  • Please refer to this post on Android activity life cycle: http://stackoverflow.com/questions/8515936/android-activity-life-cycle-what-are-all-these-methods-for – live-love Jul 28 '15 at 21:06

3 Answers3

19

Suppose Your activities are in such as A->B->C->D. When user press back button from D activity then goes to C then press back button from C Activity he will go to B activity again press back button user will comes on A Launcer Activity. So on press back button you can give message to user.

@Override
public void onBackPressed() {
    Toast.makeText(context,"Thanks for using application!!",Toast.LENGTH_LONG).show()l
    finish();
    return;
}   

or

When press direct home key from any activity for that you need to

@Override
public void onAttachedToWindow() {
    super.onAttachedToWindow();
    this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);           
}

After overriding above method, now you can easily listen HOME Key press in your activity using onKeyDown() method.

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {     

    if(keyCode == KeyEvent.KEYCODE_HOME)
    {
       Toast.makeText(context,"Thanks for using application!!",Toast.LENGTH_LONG).show();
    }
});
Ajay S
  • 48,003
  • 27
  • 91
  • 111
12

you can use onDestroy() or onBackPressed() callbacks on your main/launcher activity

Arash GM
  • 10,316
  • 6
  • 58
  • 76
2

Assuming you want the dialog to display only when the app is completely closed(removed from background as well), You could just override onDestroy() method in your first activity.

Muhammad Muazzam
  • 2,810
  • 6
  • 33
  • 62