This is my first app, and a basic question. I want to know how to deal with interruptions such as locking the screen, or minimising the app. Both of these things currently cause the app to go black, and stop responding.
The app is structured with just one activity: Main Activity. Here,
GameSurface extends SurfaceView implements SurfaceHolder.Callback { ... }
is a class that has a related "GameThread.java" Thread , and together they run the simple app and draw to a Canvas.
Here's MainActivity.java:
public class MainActivity extends AppCompatActivity {
final int flags = View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_FULLSCREEN
| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
getWindow().getDecorView().setSystemUiVisibility(flags);
this.setContentView(new GameSurface(this));
}
@Override
protected void onPause() {
//finish();
super.onPause();
}
}
Without finish() in onPause(), the app just can't cope with interruptions. Presumably this is to do with the savedInstanceState Bundle not saving the "GameSurface", or not being able to recover in the onResume() function, but I don't understand the problem.
Adding finish() to onPause() causes the app to close, but the image of the app lingers behind on my home screen background, behind my app icons. I don't want to kill the app when I interrupt it, but I thought it should be easy, so I'm curious as to why this doesn't cleanly close the app. I can't seem to find another way to end the main activity, is there a different function for terminating the app cleanly?
My immediate aim is to have the app resume when the screen is unlocked, after having been locked, and I thought this would be a simple procedure, but I can't find out how to do it. Perhaps my app is poorly structured. If you have a better way that I can structure the app, please tell me. The whole UI is a single canvas that I draw cartoon trees etc. onto.
EDIT: I have read in another question: Android Activity lifecycle and locking/unlocking device
That since I have specified the screen orientation as landscape in Main Activity, the Main activity has to be destroyed in order to display the portrait lock screen. Then, I guess, onCreate() doesn't work because the ,savedInstanceState is bad, but I don't see why it's bad. I also don't know what happens when I just minimise the app, without screen orientation changing.
EDIT: Here is the log of me opening the app, locking the screen, unlocking, then closing:
(I have added Log instructions to some methods on Main Activity)
[The app starts, lots of normal logging]
06-09 08:06:47.384 29697-29697/com.example.christian.androidgame2d W/System: ClassLoader referenced unknown path: /data/app/com.example.christian.androidgame2d-2/lib/arm
06-09 08:06:47.388 29697-29697/com.example.christian.androidgame2d I/InstantRun: starting instant run server: is main process
06-09 08:06:47.533 29697-29697/com.example.christian.androidgame2d D/log: onCreate
06-09 08:06:47.587 29697-29697/com.example.christian.androidgame2d W/art:
Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
06-09 08:06:47.837 29697-29697/com.example.christian.androidgame2d D/log: onstart
06-09 08:06:47.837 29697-29697/com.example.christian.androidgame2d D/log: onresume
06-09 08:06:47.859 29697-29735/com.example.christian.androidgame2d D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
06-09 08:06:47.957 29697-29735/com.example.christian.androidgame2d I/Adreno-EGL: <qeglDrvAPI_eglInitialize:379>: QUALCOMM Build: 10/09/15, 6cbbf7d, I3193f6e94a
06-09 08:06:47.968 29697-29735/com.example.christian.androidgame2d I/OpenGLRenderer: Initialized EGL, version 1.4
06-09 08:06:48.206 29697-29697/com.example.christian.androidgame2d W/art: Before Android 4.1, method int android.support.v7.widget.ListViewCompat.lookForSelectablePosition(int, boolean) would have incorrectly overridden the package-private method in android.widget.ListView
The app is now running ok. [I lock the screen (power button)]
06-09 08:07:22.852 29697-29697/com.example.christian.androidgame2d D/log: onPause
[ 06-09 08:07:22.868 1735: 1835 D/ ]
activate, handle: 1598182242, enabled: 0, index 2
[ 06-09 08:07:22.868 1735: 1835 D/ ]
BstSensorExt: id=1598182242, en=0
[ 06-09 08:07:22.868 1735: 1835 D/ ]
enable ID_SORI, path /sys/class/srot_sensor/g_sensor/en_disp_rotation, fd 248
06-09 08:07:23.344 29697-29697/com.example.christian.androidgame2d D/log: ondestroy
[I unlock the screen]
No more logging. But the app displays as a frozen image. I minimise the app and it glitches as follows: Buggy minimisation
So the app hasn't closed, but main activity has called onDestroy(). I don't know what stage the app is in.