在OnCreate之后调用Android onStop onDestroy

时间:2014-05-07 14:30:21

标签: android ondestroy

我希望你能帮助我。我不明白为什么会发生这种情况。

我的应用在调用onCreate()后立即调用onPause(),onStop(),onDestroy()。屏幕是黑色的。当我按下主页按钮时,onPause(),onStop(),onRestart(),onStart()和onResume()被调用,一切都很好。 这只发生在我的真实设备上(xperia z2,kitkat 4.4.2)。 这绝不会发生在我的平板电脑(galaxy tab3,4.x)和许多其他模拟器上。

你们有解决这个问题的方法吗?非常感谢你。

这是Logcat:

05-07 16:16:32.882: I/System.out(5149): onstart() called
05-07 16:16:32.882: I/System.out(5149): onresume() called
05-07 16:16:32.902: I/System.out(5149): onpause() called
05-07 16:16:32.902: I/System.out(5149): onstop() called
05-07 16:16:32.902: I/System.out(5149): ondestroy() called
05-07 16:16:33.002: D/dalvikvm(5149): GC_FOR_ALLOC freed 14423K, 28% free 59061K/80924K, paused 13ms, total 13ms
05-07 16:16:33.012: I/dalvikvm-heap(5149): Grow heap (frag case) to 72.570MB for 14745616-byte allocation
05-07 16:16:33.032: D/dalvikvm(5149): GC_FOR_ALLOC freed 9K, 10% free 73452K/80924K, paused 13ms, total 13ms
05-07 16:16:33.052: I/dalvikvm-heap(5149): Grow heap (frag case) to 79.953MB for 7750096-byte allocation
05-07 16:16:34.012: I/System.out(5149): onstart() called
05-07 16:16:34.012: I/System.out(5149): onresume() called
05-07 16:16:34.022: I/Choreographer(5149): Skipped 67 frames!  The application may be doing too much work on its main thread.
05-07 16:16:34.072: I/Adreno-EGL(5149): <qeglDrvAPI_eglInitialize:410>: EGL 1.4 QUALCOMM build:  ()
05-07 16:16:34.072: I/Adreno-EGL(5149): OpenGL ES Shader Compiler Version: E031.24.00.06
05-07 16:16:34.072: I/Adreno-EGL(5149): Build Date: 02/12/14 Wed
05-07 16:16:34.072: I/Adreno-EGL(5149): Local Branch: au090
05-07 16:16:34.072: I/Adreno-EGL(5149): Remote Branch: 
05-07 16:16:34.072: I/Adreno-EGL(5149): Local Patches: 
05-07 16:16:34.072: I/Adreno-EGL(5149): Reconstruct Branch: 
05-07 16:16:34.162: D/OpenGLRenderer(5149): Enabling debug mode 0
05-07 16:16:34.242: I/ActivityManager(5149): Timeline: Activity_idle id: android.os.BinderProxy@447d2510 time:107086183
05-07 16:16:34.242: I/ActivityManager(5149): Timeline: Activity_idle id: android.os.BinderProxy@447d2510 time:107086183
05-07 16:17:02.702: I/System.out(5149): onpause() called
05-07 16:17:03.232: W/IInputConnectionWrapper(5149): showStatusIcon on inactive InputConnection
05-07 16:17:03.292: I/System.out(5149): onstop() called
05-07 16:17:04.452: I/System.out(5149): onrestart() called
05-07 16:17:04.452: I/System.out(5149): onstart() called
05-07 16:17:04.452: I/System.out(5149): onresume() called

编辑: 这就是所有logcat抛出的。 我刚刚发现,当我将设备置于横向时,它不会调用onDestroy()。

我实施了:

@Override
public void onConfigurationChanged(Configuration newConfig) 
{
    super.onConfigurationChanged(newConfig);
}

和android:configChanges =“orientation | keyboardHidden”

......但仍然遇到同样的问题。

代码:

package com.example.x;

import java.util.HashMap;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.media.AudioManager;
import android.media.SoundPool;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.google.android.gms.ads.*;

public class GameActivity extends Activity {

    private static InterstitialAd interstitial;
    private AdView adView;
    private GameView theGameView;
    private static SoundPool soundPool;
    private static HashMap<Integer, Integer> soundsMap;
    static int SOUND1=1;
    static int SOUND2=2; 
    static int SOUND3=3; 
    static int SOUND4=4; 
    static int SOUND5=5; 
    static int SOUND6=6; 
    static int SOUND7=7; 
    static int SOUND8=8; 
    static int SOUND9=9; 
    static int SOUND10=10; 
    static int SOUND11=11; 
    static int SOUND12=12; 
    static int SOUND13=13; 
    static int SOUND14=14; 
    static int SOUND15=15; 
    static int SOUND16=16; 
    static int SOUND17=17; 
    static int SOUND18=18; 
    static int SOUND19=19; 
    static int SOUND20=20; 
    static int SOUND21=21; 
    static int SOUND22=22; 
    static int SOUND23=23; 
    static int SOUND24=24; 
    static int SOUND25=25; 
    static int SOUND26=26; 
    static int SOUND27=27; 
    static int SOUND28=28; 
    static float soundVol;


    @SuppressLint("UseSparseArrays")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
                WindowManager.LayoutParams.FLAG_FULLSCREEN
                );

        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); 


        theGameView = new GameView(this);

        RelativeLayout layout = new RelativeLayout(this);
        adView = new AdView(this);
         adView.setAdUnitId("x");
         adView.setAdSize(AdSize.SMART_BANNER);
         adSetXY(0, GameView.getHeight-GameView.FieldHeight-GameView.FieldHeight/3);
         layout.addView(theGameView);
         layout.addView(adView);
        setContentView(layout);





         soundPool = new SoundPool(15, AudioManager.STREAM_MUSIC, 100);
            soundsMap = new HashMap<Integer, Integer>();

            soundsMap.put(SOUND1, soundPool.load(this, R.raw.click, 1));
            soundsMap.put(SOUND2, soundPool.load(this, R.raw.mpupgradetower, 1));
            soundsMap.put(SOUND3, soundPool.load(this, R.raw.stower1, 1));
            soundsMap.put(SOUND4, soundPool.load(this, R.raw.stower2, 1));
            soundsMap.put(SOUND5, soundPool.load(this, R.raw.stower3, 1));
            soundsMap.put(SOUND6, soundPool.load(this, R.raw.stower4, 1));
            soundsMap.put(SOUND7, soundPool.load(this, R.raw.stower5, 1));
            soundsMap.put(SOUND8, soundPool.load(this, R.raw.stower6, 1));
            soundsMap.put(SOUND9, soundPool.load(this, R.raw.stower7, 1));
            soundsMap.put(SOUND10, soundPool.load(this, R.raw.stower8, 1));
            soundsMap.put(SOUND11, soundPool.load(this, R.raw.stower9, 1));
            soundsMap.put(SOUND12, soundPool.load(this, R.raw.sspecial1, 1));
            soundsMap.put(SOUND13, soundPool.load(this, R.raw.sspecial2, 1));
            soundsMap.put(SOUND14, soundPool.load(this, R.raw.senemy1, 1));
            soundsMap.put(SOUND15, soundPool.load(this, R.raw.senemy2, 1));
            soundsMap.put(SOUND16, soundPool.load(this, R.raw.senemy3, 1));
            soundsMap.put(SOUND17, soundPool.load(this, R.raw.senemy4, 1));
            soundsMap.put(SOUND18, soundPool.load(this, R.raw.senemy5, 1));
            soundsMap.put(SOUND19, soundPool.load(this, R.raw.senemy6, 1));
            soundsMap.put(SOUND20, soundPool.load(this, R.raw.senemy7, 1));
            soundsMap.put(SOUND21, soundPool.load(this, R.raw.senemy8, 1));
            soundsMap.put(SOUND22, soundPool.load(this, R.raw.senemy9, 1));
            soundsMap.put(SOUND23, soundPool.load(this, R.raw.sspecial, 1));
            soundsMap.put(SOUND24, soundPool.load(this, R.raw.gameover, 1));
            soundsMap.put(SOUND25, soundPool.load(this, R.raw.swater, 1));
            soundsMap.put(SOUND26, soundPool.load(this, R.raw.stree, 1));
            soundsMap.put(SOUND27, soundPool.load(this, R.raw.sfastattack, 1));
            soundsMap.put(SOUND28, soundPool.load(this, R.raw.sleben, 1));



         // Create the interstitial.
            interstitial = new InterstitialAd(this);
            interstitial.setAdUnitId("x");

            // Create ad request.
            AdRequest adRequest = new AdRequest.Builder()
                    .addTestDevice("B3EEABB8EE11C2BE770B684D95219ECB")
                    .addTestDevice("24610EF96304719510A4C412F8731837")
                    .addTestDevice("CAF0B70544C63E4A52F697BBF35A5BCA")
                    .build();

            // Begin loading your interstitial.
            interstitial.loadAd(adRequest);


             // Initiate a generic request.
             AdRequest adRequest2 = new AdRequest.Builder()
            .addTestDevice("B3EEABB8EE11C2BE770B684D95219ECB")
            .addTestDevice("24610EF96304719510A4C412F8731837")
            .addTestDevice("CAF0B70544C63E4A52F697BBF35A5BCA")      
            .build();

             // Load the adView with the ad request.
             adView.loadAd(adRequest2);


    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) 
    {
        super.onConfigurationChanged(newConfig);
    }

    public void adLoad()
    {
        runOnUiThread(new Runnable() {
             @Override
             public void run() {

                 if(adView != null) {
                    adView.setEnabled(true);
                    adView.setVisibility(View.VISIBLE);
                    adView.resume();
                 }
            }
        });

    }

    public void adDestroy()
    {
        runOnUiThread(new Runnable() {
             @Override
             public void run() {

                 if(adView != null) {
                    adView.setEnabled(false);
                    adView.setVisibility(View.GONE);
                    adView.pause();
                 }
            }
        });

    }

    public void adSetXY(float x, float y)
    {
         adView.setX(x);
         adView.setY(y);
    }


    @Override
    protected void onStart() {
        super.onStart();
        System.out.println("onstart() called");
    //  GameView.pause=true;
    }

    @Override
    protected void onPause() {
        super.onPause();
        System.out.println("onpause() called");
        GameView.pause=false;
        GameLoopThread.paused=true;
    }

    @Override
    protected void onStop() {
        super.onStop();
        System.out.println("onstop() called");
    }

    @Override
    protected void onDestroy() {
        adView.destroy();
        System.out.println("ondestroy() called");
        super.onDestroy();
    //  GameView.pause=true;
    }
    @Override
    protected void onResume() {
        super.onResume();
        System.out.println("onresume() called");
        GameLoopThread.unpaused=true;
    }
    @Override
    protected void onRestart() {
        System.out.println("onrestart() called");
        super.onRestart();
    //  GameView.pause=true;
    }


    public static void displayInterstitial() {
        if (interstitial.isLoaded()) {
          interstitial.show();
        }
      }

}

1 个答案:

答案 0 :(得分:5)

您在启动活动(setRequestedOrientation)后立即致电onCreate。文档说明

  

更改此活动的所需方向。如果活动当前处于前台或以其他方式影响屏幕方向,则将立即更改屏幕(可能导致重新启动活动)。否则,这将在下次活动可见时使用。

所以......你们看到了Activity的预期行为。

如果你想要做的是将屏幕锁定为横向模式,你可以像这样处理它:

<activity android:name="GameActivity "
android:screenOrientation="landscape"
android:configChanges="keyboardHidden|orientation|screenSize">

</activity>

会阻止屏幕配置更改触发您提到的事件。

http://developer.android.com/reference/android/app/Activity.html#setRequestedOrientation(int)