Вопрос

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;

Это было полезно?

Решение

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.

Другие советы

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.

you can override finish() to avoid this:

@Override
public void finish() {
    //super.finish(); // do not call super
    moveTaskToBack(true); // move back
}

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

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.

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

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

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top