1

I want when user close my app it update SQLite data then close app. I used onPause(), onStop() and onDestroy() method in different time on MainActivity. All methods work when app close.

But The problem is, onStop(), onPause() and onDestroy() are called when I change activity. But I don't want to do it when I change activity, only when I close my app.

How I can do that?

Thank you for your response.

shariful islam
  • 389
  • 4
  • 18

1 Answers1

4

The app will be closed

1.When user pressed back button in Main activity

2.When you call finish() method programmatically

Hence, You can use onBackPressed() method ,

 @Override
    public void onBackPressed() {
        //do your action
        finish();
        
    }

If you are finishing main activity programmatically, Then do your task before calling finish()

Edit

As you mentioned,

3.when the app is closed from recent activities,

Activity will be closed instantly. Activity Life cycle is not considered. Even onDestroy() method will not be invoked. Hence You can use service to detect that.

There is a method called onTaskRemoved() which can be used in your service

 @Override
    public void onTaskRemoved(Intent rootIntent) {
        //do your task;
    }

You can get more info from this source

Community
  • 1
  • 1
Jyoti JK
  • 2,141
  • 1
  • 17
  • 40
  • It's a good answer. But also have a limitation. If I click **close all** (pause app) button of my mobile then this method don't run. It run when I press back button to close app. How I can run it when user close **pause app**? – shariful islam Mar 24 '18 at 06:37
  • when you are closing app from recent activities, it will be killed instantly. None of the life cycle method will be executed – Jyoti JK Mar 24 '18 at 06:48
  • @sharifulislam try edited solution and refer that link – Jyoti JK Mar 24 '18 at 07:05
  • `onDestroy()` method work when I close app form recent activities. But `onDestroy()` also run when I change activity. This is the main problem. :( – shariful islam Mar 24 '18 at 07:07
  • `when I close app form recent activities` Then use service to do that – Jyoti JK Mar 24 '18 at 07:08
  • 1
    It's perfect. Lot of thanks :) <3 I used your both methods on activity and service. Thank you lot. – shariful islam Mar 24 '18 at 07:16
  • onDestroy() will not run when you change the activity, untill and unless you call finish() method after passing intent in startActivity(intent); @sharifulislam – Hitesh Sarsava May 16 '18 at 09:12
  • @HiteshSarsava when you are closing app, ondestroy() will be invoked. The question is not when app closed. – Jyoti JK May 16 '18 at 09:14
  • ondestroy() will be called only if you remove app from stack of Apps @JyotiJK – Hitesh Sarsava May 16 '18 at 09:16
  • Please check activity lifecycle @HiteshSarsava – Jyoti JK May 16 '18 at 09:22
  • i mean what will happen if user press home button the middle one? @JyotiJK – Hitesh Sarsava May 16 '18 at 09:25
  • @HiteshSarsava The app will be in background. It will be closed if you closed it from recent apps(stack of apps). Otherwise it will be in pause mode – Jyoti JK May 16 '18 at 09:28
  • i mean which method will be called when user press home button from mobile, the middle one ? @JyotiJK – Hitesh Sarsava May 16 '18 at 09:30
  • I don't think I've ever closed an app by pressing "back". I usually just click "home" and then actually quit the app if I feel like I have too many apps open (by closing all open apps). So this answer is just misleading. – birgersp Apr 17 '21 at 11:56