简体   繁体   中英

Android onPause called when starting the app

I'm programming a game for 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!

as far as I know onPause() should not be called when the app starts and the activity life cycle diagram proves it

I put a Log.d() , the onResume() method is being called , then onPause(), then onResume() again then the app starts running normally!

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. the onpause that gets called is from the old instance. If you want to enforce landscape orientation, you can do it in the androidmanifest for this activity so that this won't happen.

If you call setRequestedOrientation , your activity is possibly restarted. So, the onPause is called. 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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM