How to prevent call of onDestroy() after onPause()?
Asked Answered
C

7

3

In my android application I have noticed that on press of back key, after onPause() automatically, onDestroy() is getting called.

How do I prevent the application from calling onDestroy() after onPause()?

I dont want to destroy the instances after back key press.

On press of Back key, my webview object is getting destroyed. So, I am not able to access the webview again,once I press back key.

I have got two URLs. Imagine them as URL1 and URL2.

When I launch URL1 in the webview and press back key the flow is onKeyDown() -> onPause()

When I launch URL2 in the webview and press back key the flow is onKeyDown() -> onPause() -> onDestroy()

Why is there a difference in the behaviour? Has it got anything to do with cookies?

For URL1 -- cookies.isSecure=true;

For URL2 -- cookies.isSecure=false;

Canzona answered 19/12, 2011 at 4:55 Comment(0)
S
3

Check the declaration of your activity in Manifest. Declare it in such a manner that it can store the data when minimized.

Activity should have the capability to restore the content.

Sundry answered 20/12, 2011 at 12:7 Comment(0)
I
3

That's how the Activity lifecycle is designed in Android and you should not interfere with it. You app can be destroyed by runtime at any point of time regardless of you want it or not :) So a better approach would be to accommodate the lifecycle in your application's logic.

Interbreed answered 19/12, 2011 at 4:58 Comment(3)
But this does not happen always. Some times it calls and sometimes it doesn't call. So not able to decide the behavior.Canzona
The behavior is defined in the docs developer.android.com/reference/android/app/Activity.htmlInterbreed
I think one way will be to bring you activity to the foreground somehow.Phosphorylase
S
3

Check the declaration of your activity in Manifest. Declare it in such a manner that it can store the data when minimized.

Activity should have the capability to restore the content.

Sundry answered 20/12, 2011 at 12:7 Comment(0)
Q
2

you can override onBackPressed() in your activity, but that should be the last resort,

get a hang of these links before you start

activity lifecycle

developer blog

Quartered answered 19/12, 2011 at 6:44 Comment(0)
C
2

you can override finish() to avoid this:

@Override
public void finish() {
    //super.finish(); // do not call super
    moveTaskToBack(true); // move back
}
Crewelwork answered 5/8, 2013 at 12:43 Comment(0)
S
0

Normal android application behaviour states that the Home button hides the app to the background (you'll be shown the home screen), and Back button finishes the application (goes through onPause, onStop, onDestroy, in that order).

If you want to retain instances/states of your application when back button is pressed, I suggest you do the saving in onPause - save the states in SharedPreferences or in a place in your sqlite db. But mind that the saving process should be as quick as possible, because your application will wait until onPause executes completely before exiting.

Then load your instances/states in onResume.

Sapphera answered 19/12, 2011 at 6:53 Comment(5)
To be precise, What happens in my application is onPause and onDestroy the Webview object is getting destroyed. But I want it to be there even after clicking the back button. So, storing the webview object for the next traversal is becoming difficult.Canzona
there's this method called moveTaskToBack(true) which mimics the home button functionality. you can use that in your onBackPressed() method to hide your application without calling onDestroy.Sapphera
Thanks a lot Josephus. This way I am able to push webview behind. But it is totally closing the application. is there anyway I can give which screen to launch after this method call? I tried startactivity() but it again calls onPause() and onDestroy().Canzona
try calling startActivity(intent) before moveTaskToBack. or maybe you can place moveTaskToBack in onPause. calling startActivity automatically triggers the current activity's onPauseSapphera
In that case too complete application is minimized rather than just the webview activity. It would be very good if only webview is minimized and application's previous screen is intact.Canzona
C
0

You can know that onDestroy() will be called after onPause() using isFinishing() so you can avoid some code.

Cowskin answered 15/6, 2013 at 17:0 Comment(0)
R
-1

The answer here specifies that we have to configure the manifest in such a way that it should not get destroyed, but it does not answer how to configure. I arrived at this post when I had a similar problem for, the solution is pretty simple. Just mention

android:persistent="true"

Reference : - https://developer.android.com/guide/topics/manifest/application-element.html

This should prevent your activity getting destroyed when you click back button

I explained in detail with a use case in another post,

Prevent activity from being destroyed as long as possible

I hope this helps others who arrive at this place for the same problem

Riordan answered 4/3, 2016 at 6:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.