cmd/ebitenmobile: Do not call addView in onLayout

In onLayout, addViewInLayout is safer.

This change fixes the potential issue by moving addView to
constructors.
This commit is contained in:
Hajime Hoshi 2019-10-19 02:10:32 +09:00
parent 00ae15082d
commit 6f6cceb42c
2 changed files with 10 additions and 14 deletions

View File

@ -362,21 +362,22 @@ public class EbitenView extends ViewGroup {
public EbitenView(Context context) { public EbitenView(Context context) {
super(context); super(context);
ebitenSurfaceView_ = new EbitenSurfaceView(context); ebitenSurfaceView_ = new EbitenSurfaceView(context);
initialize();
} }
public EbitenView(Context context, AttributeSet attrs) { public EbitenView(Context context, AttributeSet attrs) {
super(context, attrs); super(context, attrs);
ebitenSurfaceView_ = new EbitenSurfaceView(context, attrs); ebitenSurfaceView_ = new EbitenSurfaceView(context, attrs);
initialize();
}
private void initialize() {
LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
addView(ebitenSurfaceView_, params);
} }
@Override @Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) { protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
if (!initialized_) {
LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
addView(ebitenSurfaceView_, params);
initialized_ = true;
}
int widthInDp = (int)Math.ceil(pxToDp(right - left)); int widthInDp = (int)Math.ceil(pxToDp(right - left));
int heightInDp = (int)Math.ceil(pxToDp(bottom - top)); int heightInDp = (int)Math.ceil(pxToDp(bottom - top));
Ebitenmobileview.layout(widthInDp, heightInDp, new ViewRectSetter() { Ebitenmobileview.layout(widthInDp, heightInDp, new ViewRectSetter() {
@ -401,18 +402,14 @@ public class EbitenView extends ViewGroup {
// It is recommended to call this when the application is being suspended e.g., // It is recommended to call this when the application is being suspended e.g.,
// Activity's onPause is called. // Activity's onPause is called.
public void suspendGame() { public void suspendGame() {
if (initialized_) { ebitenSurfaceView_.onPause();
ebitenSurfaceView_.onPause();
}
} }
// resumeGame resumes the game. // resumeGame resumes the game.
// It is recommended to call this when the application is being resumed e.g., // It is recommended to call this when the application is being resumed e.g.,
// Activity's onResume is called. // Activity's onResume is called.
public void resumeGame() { public void resumeGame() {
if (initialized_) { ebitenSurfaceView_.onResume();
ebitenSurfaceView_.onResume();
}
} }
// onErrorOnGameUpdate is called on the main thread when an error happens when updating a game. // onErrorOnGameUpdate is called on the main thread when an error happens when updating a game.
@ -422,7 +419,6 @@ public class EbitenView extends ViewGroup {
} }
private EbitenSurfaceView ebitenSurfaceView_; private EbitenSurfaceView ebitenSurfaceView_;
private boolean initialized_ = false;
} }
` `

File diff suppressed because one or more lines are too long