应用程序在重新打开后变为黑屏

时间:2016-10-19 23:27:36

标签: android android-activity android-lifecycle

当我通过点击主页按钮关闭我的应用程序时,我看到onDestroy()方法被调用,在我重新打开它之后,它从头开始,这没关系。

但有时候,在我重新开启之后,我会在没有任何反应的情况下获得黑屏,并且没有任何方法被调用 就像这里的第二张图片一样您可以看到第一张图片,“MyGame”是我重新打开的应用程序。

enter image description here enter image description here

我很困惑。任何的想法?谢谢你的关注

MainActivity.java

public class MainActivity extends AppCompatActivity implements IOnRemoveViewListener, IStartGameViewFragment {

    public static int statusBarHeight = 0;
    public static int navigationBarHeight = 0;
    public static volatile PointF screenSize;
    private View decorView;
    private gameViewFragment gameViewFragment = new gameViewFragment();

    private FragmentManager manager;
    private FragmentTransaction transaction;

    private Handler handler = new Handler();
    private Runnable runnable = new Runnable() {
        @Override
        public void run() {
            addFragment(gameViewFragment, gameViewFragment.TAG);
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setTheme(R.style.AppTheme);
        super.onCreate(savedInstanceState);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        decorView = getWindow().getDecorView();
        setOnSystemUiVisibilityChangeListener();
        // status bar height
        int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            statusBarHeight = getResources().getDimensionPixelSize(resourceId);
        }

        // navigation bar height
        resourceId = getResources().getIdentifier("navigation_bar_height", "dimen", "android");
        if (resourceId > 0) {
            navigationBarHeight = getResources().getDimensionPixelSize(resourceId);
        }
        manager = getSupportFragmentManager();
        setUiFlag();
        setContentView(R.layout.main);
    }

    @Override
    protected void onStart() {
        super.onStart();
        setUiFlag();
        if (manager.findFragmentByTag(splashFragment.TAG) == null)
            initFragments();
    }

    public void initFragments() {
        addFragment(new splashFragment(), splashFragment.TAG);
    }

    public void setUiFlag() {
        decorView.setSystemUiVisibility(
            View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                    | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);

        boolean hasBackKey = KeyCharacterMap.deviceHasKey(KeyEvent.KEYCODE_BACK);

        if (!hasBackKey) {
            decorView.setSystemUiVisibility(
                    View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                            | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                            | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                            | View.SYSTEM_UI_FLAG_FULLSCREEN
                            | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                            | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
            // Do whatever you need to do, this device has a navigation bar
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        setUiFlag();
    }

    public void setOnSystemUiVisibilityChangeListener() {
        decorView.setOnSystemUiVisibilityChangeListener
            (new View.OnSystemUiVisibilityChangeListener() {
                @Override
                public void onSystemUiVisibilityChange(int visibility) {
                    // Note that system bars will only be "visible" if none of the
                    // LOW_PROFILE, HIDE_NAVIGATION, or FULLSCREEN flags are set.
                    if ((visibility & View.SYSTEM_UI_FLAG_FULLSCREEN) == 0) {
                        // TODO: The system bars are visible. Make any desired
                        // adjustments to your UI, such as showing the action bar or
                        // other navigational controls.
                        setUiFlag();
                    } else {
                        // TODO: The system bars are NOT visible. Make any desired
                        // adjustments to your UI, such as hiding the action bar or
                        // other navigational controls.
                    }
                }
            });
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        handler.removeCallbacks(runnable);
        popBackStack();
    }

    public void popBackStack() {
        manager.popBackStack();
    }

    @Override
    protected void onStop() {
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        setUiFlag();
    }

    private void addFragment(Fragment fragment, String TAG) {
        transaction = manager.beginTransaction();
        transaction.setCustomAnimations(R.anim.enter_from_left, R.anim.exit_to_right, R.anim.enter_from_left, R.anim.exit_to_right);
        transaction.add(R.id.Container_frame_ly, fragment, TAG);
        transaction.addToBackStack(TAG);
        transaction.commit();
    }

    private void replaceFragment(Fragment fragment, String TAG) {
        transaction = manager.beginTransaction();
        transaction.setCustomAnimations(R.anim.enter_from_left, R.anim.exit_to_right, R.anim.enter_from_left, R.anim.exit_to_right);
        transaction.replace(R.id.Container_frame_ly, fragment, TAG);
        transaction.commit();
    }

    public void removeFragment(Fragment fragment) {
        transaction = manager.beginTransaction();
        transaction.setCustomAnimations(R.anim.enter_from_left, R.anim.exit_to_right, R.anim.enter_from_left, R.anim.exit_to_right);
        transaction.remove(fragment);
        transaction.commit();
    }

    @Override
    public void startGameViewFragment() {
        handler.postDelayed(runnable, 2000);
    }

}

style.xml

<resources>

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowFullscreen">true</item>
    <item name="android:windowContentOverlay">@null</item>
    <item name="android:windowBackground">@drawable/splash_drawable</item>
</style>

<style name="SplashTheme" parent="Theme.AppCompat.NoActionBar">
    <item name="android:windowBackground">@drawable/splash_drawable</item>
    <item name="android:windowContentOverlay">@null</item>
</style>

的AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="game.development.mahdi.nazari.gameblak">

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="MyGame"
    android:supportsRtl="true"
    android:theme="@style/SplashTheme">
    <activity
        android:name=".MVC.Activity.MainActivity"
        android:configChanges="orientation|keyboardHidden|screenSize"
        android:screenOrientation="sensorLandscape"
        android:clearTaskOnLaunch="true"
        android:noHistory="true"
        android:launchMode="singleTask">

        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>

    </activity>

</application>

0 个答案:

没有答案