Skip to content
This repository has been archived by the owner on Aug 4, 2021. It is now read-only.

App Auto-Closing when device is locked. #4

Open
Dimitrees opened this issue Aug 31, 2017 · 3 comments
Open

App Auto-Closing when device is locked. #4

Dimitrees opened this issue Aug 31, 2017 · 3 comments

Comments

@Dimitrees
Copy link
Contributor

Hello, don't know if this project is still being maintained, however I noticed an issue in my own fork of the project, but when I tested it on the app which is available in Google Play it still appeared.

After you've selected the "Play" button in the first activity, and chosen a theme, whenever you lock your device manually, the app stops and closes, and when you unlock the device again, you have to restart the app. Should this be happening?

I am also interested in trying to fixing this myself, if one of the creators points me to the right direction.

Thank you.

@Dimitrees Dimitrees changed the title App Auto-Closing when phone is locked. App Auto-Closing when device is locked. Aug 31, 2017
@sromku
Copy link
Owner

sromku commented Aug 31, 2017

I am not sure I understood. What do you mean by "app stops and closes" - Is the app crashing? If yes, can you provide the stacktrace.

@Dimitrees
Copy link
Contributor Author

Exactly, it crashes.

09-01 17:14:33.625 25054-25054/com.snatik.matches E/AndroidRuntime: FATAL EXCEPTION: main Process: com.snatik.matches, PID: 25054 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.snatik.matches/com.snatik.matches.MainActivity}: java.lang.NullPointerException: Attempt to read from field 'int com.snatik.matches.themes.Theme.id' on a null object reference at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2305) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2365) at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3927) at android.app.ActivityThread.access$900(ActivityThread.java:148) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1289) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5272) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:909) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:704) Caused by: java.lang.NullPointerException: Attempt to read from field 'int com.snatik.matches.themes.Theme.id' on a null object reference at com.snatik.matches.fragments.DifficultySelectFragment.onCreateView(DifficultySelectFragment.java:28) at android.support.v4.app.Fragment.performCreateView(Fragment.java:2192) at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1299) at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1528) at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1595) at android.support.v4.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:2900) at android.support.v4.app.FragmentController.dispatchActivityCreated(FragmentController.java:201) at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:603) at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1220) at android.app.Activity.performStart(Activity.java:5993) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2268) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2365)  at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3927)  at android.app.ActivityThread.access$900(ActivityThread.java:148)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1289)  at android.os.Handler.dispatchMessage(Handler.java:102)  at android.os.Looper.loop(Looper.java:135)  at android.app.ActivityThread.main(ActivityThread.java:5272)  at java.lang.reflect.Method.invoke(Native Method)  at java.lang.reflect.Method.invoke(Method.java:372)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:909)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:704)  09-01 17:14:33.651 25054-25054/com.snatik.matches I/Process: Sending signal. PID: 25054 SIG: 9

@sromku
Copy link
Owner

sromku commented Sep 1, 2017

In fact I never experienced this with this app, but looks like a valid exception. It happens because of static fields in Engine class. What happens is that Android kills the game process due to low memory and once you open the app it tries restoring to previous state - to Fragment that was last in the foreground. The problem is that DifficultySelectFragment uses the static field mSelectedTheme from Engine which is initialized in fragment before - ThemeSelectFragment, but this fragment isn't created after restoring the app state.

So, in general:

  1. Using static members is a bad idea :) And if it's used the initialization must happen in Application class only.
  2. onSaveInstanceState and restore state should be used if we want to retain Activity and Fragments states.

In this case I would remove fragments at all and use only views. Each screen would be a View. Also, I would create some recycler bin for controlling the memory allocation and views for reusage.
This pretty a big change.
For a quick win, I would go back to the home screen if Engine isn't initialized correctly after being restored - this should resolve the crash.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants