[英]Android onPause called when starting the app
我正在为Android编写游戏。 我运行游戏没有问题,但是正在发生一些奇怪的事情
经过一些测试,我发现在游戏开始时会调用Activity.onPause()方法!
据我所知,当应用启动且活动生命周期图证明不应该调用onPause()时
我放入Log.d(),先调用onResume()方法,然后再调用onPause(),然后再调用onResume(),然后应用程序将开始正常运行!
我没有放入任何代码,因为我不知道问题出在哪里,而且我不确定这是否是我首先造成的问题,这是否正常? 或造成这种情况的原因
编辑:按照要求输入活动代码,应用中只有一个活动
package com.khallouf.agf;
import com.khallouf.agf.graphics.Graphics;
import com.khallouf.agf.graphics.Renderer;
import com.khallouf.agf.graphics.Screen;
import android.app.Activity;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.PowerManager;
import android.os.PowerManager.WakeLock;
import android.util.Log;
import android.view.Display;
import android.view.Window;
import android.view.WindowManager;
public abstract class Game extends Activity implements Runnable{
public Renderer renderer;
private Thread renderingThread ;
public Screen currentScreen;
public Graphics graphics;
public Bitmap extraSpaces;
public WakeLock wakeLock;
public int physicalScreenWidth;
public int physicalScreenLength;
enum Orientation {Landscape , Portrait}
public Orientation orientation = Orientation.Landscape;
public Game(Orientation o)
{
orientation = o;
}
@SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle savedInstanceState)
{
//Setting the screen power and to full screen mode
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
PowerManager powerManager = (PowerManager)getSystemService(Context.POWER_SERVICE);
wakeLock = powerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK, "MyGame");
// reading the physical screen size
Display display = getWindowManager().getDefaultDisplay();
physicalScreenLength = display.getHeight();
physicalScreenWidth = display.getWidth();
setOrientation(orientation);
Bitmap frameBuffer = createFrameBuffer();
graphics = new Graphics(getAssets(), getResources() , frameBuffer);
currentScreen = getStartingScreen();
renderer = new Renderer(this,frameBuffer);
setContentView(renderer);
renderingThread = new Thread(this);
renderingThread.start();
}
@Override
protected void onResume() {
super.onResume();
wakeLock.acquire();
currentScreen.resume();
//Starting the Thread
Log.d("TAG", "onResume entered");
}
@Override
protected void onPause() {
super.onPause();
wakeLock.release();
currentScreen.pause();
Log.d("TAG", "OnPause entered");
}
两次调用onPause的原因是,第一次启动活动时,它会请求方向更改,活动的旧实例被破坏,活动的新实例被创建。 被调用的onpause来自旧实例。 如果要强制横向,则可以在androidmanifest中为此活动进行操作,以免发生这种情况。
如果调用setRequestedOrientation ,则您的活动可能会重新启动。 因此,onPause被调用。 如果您要制作游戏,并且只希望您的应用程序处于横向模式,则可以在清单中进行设置。
android:screenOrientation="landscape"
例如。
<activity
android:name=".YourActivity"
android:label="@string/app_name"
android:screenOrientation="landscape" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
另请参见manifest.xml中的屏幕方向和值 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.