Press the Home key during the game and return to the game interface.

Source: Internet
Author: User

Symptom: during the game, press the Home Key to return to the main menu of the mobile phone, and then click the game icon to try to return a dark screen when the game is played!

I have never been concerned that only the end thread is running the game. Today I think I have to think about this problem carefully!

First, print the logo to see which methods will be called after pressing the Home key. The result is as follows:

After you press home during the game:

11-29 20:42:07. 090: I/system. Out (18835): Activity onpause...
11-29 20:42:14. 190: I/system. Out (18835): Activity onstop...
11-29 20:42:14. 260: I/system. Out (18835): surfaceview surfacedestroyed...


It can be seen that the surfaceview is destroyed when it returns to the main menu of the mobile phone, and the main thread of my surfaceview is created in the constructor.

So what methods will we call when we return to the game? Next, let's look at the log:

11-29 20:48:06. 940: I/system. Out (18835): Activity onrestart...
11-29 20:48:06. 950: I/system. Out (18835): Activity onresume...
11-29 20:48:07. 230: I/system. Out (18835): surfaceview surfacecreated...
11-29 20:48:07. 240: I/system. Out (18835): surfaceview surfacechanged...


When surfacedestroyed is running, the thread has exited, And the thread that returns back to the game screen is gone. No painting method is called, so you only see a black screen!
Now that we understand the operating principles, we know how to change it!


First, create a thread in surfacecreated and start the thread,

When surfacedestroyed is called, the thread is no longer valid. We set the thread object to null to release it,

Then, no matter how the thread is returned, your game will continue to run and your game will not be interrupted. The simple code is more expressive!

Package COM. game. view; import android. content. context; import android. graphics. canvas; import android. graphics. paint; import android. graphics. paint. style; import android. graphics. rect; import android. view. surfaceholder; import android. view. surfaceholder. callback; import android. view. surfaceview; public class testview extends surfaceview implements callback, runnable {public static final int game_heart = 1000/30; // refresh 30 times per second public static int screenw, screenh; private thread; private surfaceholder holder; private paint; Public testview (context) {super (context); // todo auto-generated constructor stubholder = getholder (); holder. addcallback (this); paint = new paint (paint. anti_alias_flag); // non-sawtooth paint. setstyle (style. fill); // fill style paint. settextsize (16); // font size}/*** Method for executing game logic */private void Update () {}/*** execute game rendering */private rect = new rect (); Private void draw () {canvas = holder. lockcanvas (); string text = "demo of angel wings"; // get the text width and height paint. gettextbounds (text, 0, text. length (), rect); // display the text paint in the center of the screen. setcolor (0xfff000f0); // note that the maximum two FF values indicate the transparency of the paint brush. a painting that is not set is completely transparent and does not have any effect on the canvas. drawtext (text, (screenw-rect. width ()/2, screenh/2 + rect. height ()/2, paint); holder. unlockcanvasandpost (canvas) ;}@ overridepublic void surfacecreated (surfaceholder holder) {// todo auto-generated method stubscreenw = getwidth (); screenh = getheight (); thread = new thread (this); isrun = true; thread. start () ;}@ overridepublic void surfacechanged (surfaceholder holder, int format, int width, int height) {// todo auto-generated method stub // obtain the screen width and height again when the screen is rotated. screenw = getwidth (); screenh = getheight ();} @ overridepublic void surfacedestroyed (surfaceholder holder) {// todo auto-generated method stubisrun = false; thread = NULL;} private Boolean isrun; // indicates private int usetime; // record the time used for each screen flushing @ overridepublic void run () {// todo auto-generated method stublong start, end; while (isrun) {start = system. currenttimemillis (); Update (); // refresh all elements on the Interface draw (); // draw the Interface Element end = system. currenttimemillis (); usetime = (INT) (end-Start); If (usetime <game_heart) {// ensure that the interval of each screen flushing is the same. Try {thread. sleep (game_heart-usetime);} catch (interruptedexception e) {// todo auto-generated catch blocke. printstacktrace ();}}}}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.