按下主页按钮后应用程序进入黑色窗口并重新进入

时间:2016-10-02 03:36:44

标签: android android-activity android-lifecycle

我正在Android工作室开发游戏。

在正常模式下,大部分时间,当我按下主页按钮时,应用程序调用OnDestroy()方法,当我再次重新进入它时(通过单击Overview按钮并打开最近的一个),OnCreat()方法调用试。

好的,这很正常。

但有时,当我按下主页按钮时,应用程序调用OnDestroy()方法,当我再次重新进入它时,任何方法都不会再次调用!!我只看到一个黑色的窗户。

任何人都知道问题是什么,我该如何解决?

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

}

0 个答案:

没有答案