Вопрос

I got a problem with my app when the screen turns off (because of system screen timeout) my application finishes. I search but didn't find something helpful. Is it a common problem or does it have a fix?

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

Решение

you shud not call finish() in onPause. it could be called for variety of reasons(check doc.). why you want to kill your activity when user switches app? its not recommended. here are some posts, but there is no api available to detect app going in background.

How to detect when an Android app goes to the background and come back to the foreground

http://nathanael.hevenet.com/android-dev-detecting-when-your-app-is-in-the-background-across-activities/

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

I want when the user leaves this activity to finish but not the screen thnx

Take finish() out of onPause(). Put it wherever the user leaves the Activity. So, assuming you have code that starts a new Activity, put finish() after startActivity().

You also could use the flag android:noHistory in your <activity> tag of your maifest.xml so that the Activity is removed from the stack whenever it starts a new Activity. Both of these methods do the same job it just depends on how you want/need to implement it.

The reason it closes when the screen turns off is because your app calls onPause() at that time so removing the call from that method would keep it from closing when the screen turns off.

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