简体   繁体   English

启动应用程序时调用了Android onPause

[英]Android onPause called when starting the app

I'm programming a game for android. 我正在为Android编写游戏。 I have no problem running the game but some wierd stuff are happening 我运行游戏没有问题,但是正在发生一些奇怪的事情

with a little of testing I found out that the Activity.onPause() method is being called when the game starts! 经过一些测试,我发现在游戏开始时会调用Activity.onPause()方法!

as far as I know onPause() should not be called when the app starts and the activity life cycle diagram proves it 据我所知,当应用启动且活动生命周期图证明不应该调用onPause()时

I put a Log.d() , the onResume() method is being called , then onPause(), then onResume() again then the app starts running normally! 我放入Log.d(),先调用onResume()方法,然后再调用onPause(),然后再调用onResume(),然后应用程序将开始正常运行!

I didn't put any piece of my code because I have no Idea where the problem is and I'm not sure if this is a problem I caused in the first place, is this behavior normal? 我没有放入任何代码,因为我不知道问题出在哪里,而且我不确定这是否是我首先造成的问题,这是否正常? or something causing this 或造成这种情况的原因

Edit: as asked, the code for the activity, there is only one activity in the app 编辑:按照要求输入活动代码,应用中只有一个活动

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");
    }

The reason onPause is called twice is that the first time you start an activity, it request an orientation change, the old instance of the activity is destroyed and a new instance of the activity is created. 两次调用onPause的原因是,第一次启动活动时,它会请求方向更改,活动的旧实例被破坏,活动的新实例被创建。 the onpause that gets called is from the old instance. 被调用的onpause来自旧实例。 If you want to enforce landscape orientation, you can do it in the androidmanifest for this activity so that this won't happen. 如果要强制横向,则可以在androidmanifest中为此活动进行操作,以免发生这种情况。

If you call setRequestedOrientation , your activity is possibly restarted. 如果调用setRequestedOrientation ,则您的活动可能会重新启动。 So, the onPause is called. 因此,onPause被调用。 If you are making a game and only want your app in landscape mode, you can set that in the manifest. 如果您要制作游戏,并且只希望您的应用程序处于横向模式,则可以在清单中进行设置。

android:screenOrientation="landscape"

eg. 例如。

<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>

Also see Screen orientation and values in manifest.xml . 另请参见manifest.xml中的屏幕方向和值

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android 活动生命周期 - 何时调用“onPause”? - Android Activity LifeCycle - when 'onPause' is called? 使用导航图时未调用android onpause - android onpause not called when using navgraph Android:调用onPause()时隐藏或销毁视图 - Android: Hiding or destroy view when onPause() is called 在我的Android应用程序中调用onPause()时如何保持计时器运行? - How can I keep a timer running when onPause( ) is called in my android app? Android onPause()没有被调用 - Android onPause() not geting called Android - 如果应用程序暂停(到后台),是否会调用应用程序的LeScanCallback? - Android - Would LeScanCallback of an App get called if the App goes onPause (to the background)? 调用onPause时Android应用崩溃 - Android app crash when call onPause 当我的应用程序被多次最小化时,不会调用 Fragment onPause() - Fragment onPause() is not called when my app is minimized multiple times 即使强制关闭应用程序进程,也保证可以调用onPause()吗? - Is onPause() guaranteed to be called, even when force closing the app process? 当用户从最近关闭应用程序时调用 onPause() - Is onPause() called when user closes the app from recents
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM