2014-08-28 2 views
0

Android 용 게임을 프로그래밍하고 있습니다. 게임을 실행하는 데 문제가 없지만 일부 이상한 일이 발생하고 있습니다.앱을 시작할 때 Android onPause가 호출됩니다.

약간의 테스트를 통해 게임을 시작할 때 Activity.onPause() 메서드가 호출되는 것을 알았습니다!

까지 내가) 응용 프로그램을 시작하고 활동 라이프 사이클 다이어그램이

나는 Log.d를 (넣어

을 입증 할 때 onPause()가 호출 할 수 없습니다 알고는 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"); 
    } 
+0

[여기] (http://developer.android.com/reference/android/app/Activity.html)의 라이프 사이클 다이어그램과 같이 약간 이상하게 보입니다. 그래도 활동 코드를 게시 하시겠습니까? 여러 활동을하고 있습니까? – aProperFox

+2

활동의 어느 곳에서나 방향 변경을 요청 했습니까? – CChi

+0

사실을 게시했습니다. 코드가 방향을 바꾸고 있습니다. 이 문제입니까? 그렇다면 활동이 일시 중지되지 않도록 방향을 수정할 수 있습니까? –

답변

1

onPause가 처음으로이 활동을 시작하는 것이 두 번 호출입니다 이유에 단 하나 개의 활동이, 오리엔테이션 변경을 요청하면 이전 활동 인스턴스가 삭제되고 새 활동 인스턴스가 작성됩니다. 호출 된 onpause는 이전 인스턴스에서 온 것입니다. 가로 방향을 적용하려면이 활동에 대한 androidmanifest에서 할 수 있으므로 그렇게되지는 않습니다.

1

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> 

Screen orientation and values in manifest.xml도 참조하십시오.

관련 문제