Skip to content

Commit 4abc7f4

Browse files
authored
Only pause GLSurfaceView if activity is moved to the background, and not if it just loses focus (#1942)
* Only pause GLSurfaceView if activity is being stopped, and not if it just loses focus * Add copyright notice * Ensure foreground and background events are not sent out on initial app start Remove work-around in nativeOnResume to avoid calling applicationWillEnterForeground twice since it is no longer required Remove redundant setRenderMode calls in AxmolGLSurfaceView * Add copyright notices
1 parent aacfa57 commit 4abc7f4

File tree

4 files changed

+90
-79
lines changed

4 files changed

+90
-79
lines changed

core/platform/android/java/src/org/axmol/lib/AxmolActivity.java

Lines changed: 36 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,27 @@
11
/****************************************************************************
2-
Copyright (c) 2010-2013 cocos2d-x.org
3-
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
4-
5-
https://axmolengine.github.io/
6-
7-
Permission is hereby granted, free of charge, to any person obtaining a copy
8-
of this software and associated documentation files (the "Software"), to deal
9-
in the Software without restriction, including without limitation the rights
10-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11-
copies of the Software, and to permit persons to whom the Software is
12-
furnished to do so, subject to the following conditions:
13-
14-
The above copyright notice and this permission notice shall be included in
15-
all copies or substantial portions of the Software.
16-
17-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23-
THE SOFTWARE.
2+
Copyright (c) 2010-2013 cocos2d-x.org
3+
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
4+
Copyright (c) 2019-present Axmol Engine contributors (see AUTHORS.md).
5+
6+
https://axmolengine.github.io/
7+
8+
Permission is hereby granted, free of charge, to any person obtaining a copy
9+
of this software and associated documentation files (the "Software"), to deal
10+
in the Software without restriction, including without limitation the rights
11+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
copies of the Software, and to permit persons to whom the Software is
13+
furnished to do so, subject to the following conditions:
14+
15+
The above copyright notice and this permission notice shall be included in
16+
all copies or substantial portions of the Software.
17+
18+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
THE SOFTWARE.
2425
****************************************************************************/
2526
package org.axmol.lib;
2627

@@ -72,6 +73,7 @@ public abstract class AxmolActivity extends Activity implements AxmolEngineListe
7273
private boolean hasFocus = false;
7374
private boolean showVirtualButton = false;
7475
private boolean paused = true;
76+
private boolean rendererPaused = true;
7577

7678
public AxmolGLSurfaceView getGLSurfaceView(){
7779
return mGLSurfaceView;
@@ -223,7 +225,11 @@ public void onWindowFocusChanged(boolean hasFocus) {
223225
private void resume() {
224226
this.hideVirtualButton();
225227
AxmolEngine.onResume();
226-
mGLSurfaceView.onResume();
228+
if (rendererPaused) {
229+
mGLSurfaceView.onResume();
230+
rendererPaused = false;
231+
}
232+
mGLSurfaceView.setRenderMode(GLSurfaceView.RENDERMODE_CONTINUOUSLY);
227233
}
228234

229235
private void resumeIfHasFocus() {
@@ -242,6 +248,13 @@ protected void onPause() {
242248
paused = true;
243249
super.onPause();
244250
AxmolEngine.onPause();
251+
mGLSurfaceView.setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);
252+
}
253+
254+
@Override
255+
protected void onStop() {
256+
super.onStop();
257+
rendererPaused = true;
245258
mGLSurfaceView.onPause();
246259
}
247260

core/platform/android/java/src/org/axmol/lib/AxmolGLSurfaceView.java

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,27 @@
11
/****************************************************************************
2-
Copyright (c) 2010-2011 cocos2d-x.org
3-
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
4-
5-
https://axmolengine.github.io/
6-
7-
Permission is hereby granted, free of charge, to any person obtaining a copy
8-
of this software and associated documentation files (the "Software"), to deal
9-
in the Software without restriction, including without limitation the rights
10-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11-
copies of the Software, and to permit persons to whom the Software is
12-
furnished to do so, subject to the following conditions:
13-
14-
The above copyright notice and this permission notice shall be included in
15-
all copies or substantial portions of the Software.
16-
17-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23-
THE SOFTWARE.
2+
Copyright (c) 2010-2011 cocos2d-x.org
3+
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
4+
Copyright (c) 2019-present Axmol Engine contributors (see AUTHORS.md).
5+
6+
https://axmolengine.github.io/
7+
8+
Permission is hereby granted, free of charge, to any person obtaining a copy
9+
of this software and associated documentation files (the "Software"), to deal
10+
in the Software without restriction, including without limitation the rights
11+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
copies of the Software, and to permit persons to whom the Software is
13+
furnished to do so, subject to the following conditions:
14+
15+
The above copyright notice and this permission notice shall be included in
16+
all copies or substantial portions of the Software.
17+
18+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
THE SOFTWARE.
2425
****************************************************************************/
2526
package org.axmol.lib;
2627

@@ -186,7 +187,6 @@ public void setEditText(final AxmolEditBox pEditText) {
186187
@Override
187188
public void onResume() {
188189
super.onResume();
189-
this.setRenderMode(RENDERMODE_CONTINUOUSLY);
190190
this.queueEvent(new Runnable() {
191191
@Override
192192
public void run() {
@@ -203,7 +203,6 @@ public void run() {
203203
AxmolGLSurfaceView.this.mRenderer.handleOnPause();
204204
}
205205
});
206-
this.setRenderMode(RENDERMODE_WHEN_DIRTY);
207206
super.onPause();
208207
}
209208

core/platform/android/java/src/org/axmol/lib/AxmolRenderer.java

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
11
/****************************************************************************
2-
Copyright (c) 2010-2011 cocos2d-x.org
3-
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
4-
5-
https://axmolengine.github.io/
6-
7-
Permission is hereby granted, free of charge, to any person obtaining a copy
8-
of this software and associated documentation files (the "Software"), to deal
9-
in the Software without restriction, including without limitation the rights
10-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11-
copies of the Software, and to permit persons to whom the Software is
12-
furnished to do so, subject to the following conditions:
13-
14-
The above copyright notice and this permission notice shall be included in
15-
all copies or substantial portions of the Software.
16-
17-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23-
THE SOFTWARE.
2+
Copyright (c) 2010-2011 cocos2d-x.org
3+
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
4+
Copyright (c) 2019-present Axmol Engine contributors (see AUTHORS.md).
5+
https://axmolengine.github.io/
6+
7+
Permission is hereby granted, free of charge, to any person obtaining a copy
8+
of this software and associated documentation files (the "Software"), to deal
9+
in the Software without restriction, including without limitation the rights
10+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
copies of the Software, and to permit persons to whom the Software is
12+
furnished to do so, subject to the following conditions:
13+
14+
The above copyright notice and this permission notice shall be included in
15+
all copies or substantial portions of the Software.
16+
17+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23+
THE SOFTWARE.
2424
****************************************************************************/
2525
package org.axmol.lib;
2626

@@ -47,6 +47,7 @@ public class AxmolRenderer implements GLSurfaceView.Renderer {
4747
private int mScreenWidth;
4848
private int mScreenHeight;
4949
private boolean mNativeInitCompleted = false;
50+
private boolean mIsPaused = false;
5051

5152
// ===========================================================
5253
// Constructors
@@ -97,7 +98,7 @@ public void onDrawFrame(final GL10 gl) {
9798
} else {
9899
final long now = System.nanoTime();
99100
final long interval = now - this.mLastTickInNanoSeconds;
100-
101+
101102
/*
102103
* Render time MUST be counted in, or the FPS will slower than appointed.
103104
*/
@@ -164,10 +165,14 @@ public void handleOnPause() {
164165
return;
165166

166167
AxmolRenderer.nativeOnPause();
168+
mIsPaused = true;
167169
}
168170

169171
public void handleOnResume() {
170-
AxmolRenderer.nativeOnResume();
172+
if (mIsPaused) {
173+
AxmolRenderer.nativeOnResume();
174+
mIsPaused = false;
175+
}
171176
}
172177

173178
private static native void nativeInsertText(final String text);

core/platform/android/jni/Java_org_axmol_lib_AxmolRenderer.cpp

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/****************************************************************************
22
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
3+
Copyright (c) 2019-present Axmol Engine contributors (see AUTHORS.md).
34
45
https://axmolengine.github.io/
56
@@ -55,18 +56,11 @@ JNIEXPORT void JNICALL Java_org_axmol_lib_AxmolRenderer_nativeOnPause(JNIEnv*, j
5556

5657
JNIEXPORT void JNICALL Java_org_axmol_lib_AxmolRenderer_nativeOnResume(JNIEnv*, jclass)
5758
{
58-
static bool firstTime = true;
5959
if (Director::getInstance()->getGLView())
6060
{
61-
// don't invoke at first to keep the same logic as iOS
62-
// can refer to https://github.com/cocos2d/cocos2d-x/issues/14206
63-
if (!firstTime)
64-
Application::getInstance()->applicationWillEnterForeground();
65-
61+
Application::getInstance()->applicationWillEnterForeground();
6662
ax::EventCustom foregroundEvent(EVENT_COME_TO_FOREGROUND);
6763
ax::Director::getInstance()->getEventDispatcher()->dispatchEvent(&foregroundEvent, true);
68-
69-
firstTime = false;
7064
}
7165
}
7266

0 commit comments

Comments
 (0)