【问题标题】:Android onPause called when starting the app启动应用程序时调用 Android onPause
【发布时间】:2014-08-29 06:22:37
【问题描述】:

我正在为 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");
    }

【问题讨论】:

  • 根据生命周期图here 看起来有点奇怪。你介意发布活动代码吗?您是否正在开展多项活动?
  • 您是否要求在活动中的任何地方更改方向?
  • 我发布了一个编辑实际上是的,代码正在改变方向是这个问题吗?如果是这样,我可以修复方向以使活动不会暂停吗?

标签: android


【解决方案1】:

onPause 被调用两次的原因是当你第一次启动一个 Activity 时,它请求改变方向,该 Activity 的旧实例被销毁并创建一个新的 Activity 实例。被调用的 onpause 来自旧实例。如果您想强制横向,您可以在 androidmanifest 中为此活动执行此操作,这样就不会发生这种情况。

【讨论】:

    【解决方案2】:

    如果您拨打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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-12-17
      • 1970-01-01
      • 1970-01-01
      • 2012-05-09
      • 2011-01-05
      • 2012-05-12
      • 2013-03-10
      相关资源
      最近更新 更多