[英]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.