Here is a simple solution that may be acceptable in some cases, for example, a background animation screen and actions whose states do not need to be restored - surface activity should end on pause.
protected void onPause() { super.onPause(); finish(); }
The best solution is to transfer the creation of a stream from the constructor to onto the Creaed surface as follows:
@Override public void surfaceCreated(SurfaceHolder holder) { _thread = new TutorialThread(holder, this); _thread.setRunning(true); _thread.start(); }
Then create a pause flag in the stream loop:
if(!pause){ _panel.onDraw(c); }
Finally, in onPause and onRestore, a pause flag is set for activity:
protected void onResume() { super.onResume(); pause = false; } protected void onPause() { super.onPause(); pause = true; }
When the user clicks the "Main button" button, the "Finish" method is launched, which will disable the current "_thread" thread. When it returns to the application, surfaceCreated will assign the "_thread" link to the new thread, and the old thread object will be deleted by the garbage collector.
Lumis Mar 16 2018-11-11T00: 00Z
source share