方向更改后,活动不会重新启动

时间:2015-02-05 12:54:10

标签: android main-activity

我的Android项目出现问题。我为手机和平板电脑创建了不同的布局,但遇到了一些问题。这些布局使用不同的片段。 这是我的MainActivity onCreate方法:

    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_navigation);

    if (isTablet(this.getApplicationContext()) &&
            getResources().getConfiguration().orientation
            == Configuration.ORIENTATION_LANDSCAPE){
        mNavigationFragment = (NavigationFragment)
                getSupportFragmentManager().findFragmentById(R.id.navigation);

    } else {
        mNavigationDrawerFragment = (NavigationDrawerFragment)
                getSupportFragmentManager().findFragmentById(R.id.navigation_drawer);
        // Set up the drawer.
        mNavigationDrawerFragment.setUp(
                R.id.navigation_drawer,
                (DrawerLayout) findViewById(R.id.drawer_layout));

    }

    mTitle = getTitle();

    getSupportFragmentManager().addOnBackStackChangedListener(new FragmentManager.OnBackStackChangedListener() {

        @Override
        public void onBackStackChanged() {
            Fragment f = getSupportFragmentManager().findFragmentById(R.id.container);
            if (f != null){
                updateTitleAndDrawer (f);
            }

        }
    });
}

这是isTablet方法:

public static boolean isTablet(Context context) {
    return (context.getResources().getConfiguration().screenLayout
            & Configuration.SCREENLAYOUT_SIZE_MASK)
            >= Configuration.SCREENLAYOUT_SIZE_LARGE;
}

但重点是,当我改变方向时,布局不会改变。你能告诉一些意见吗?

忘记提及,我在AndroidManifest中有以下行

android:configChanges="keyboardHidden|orientation|screenSize">

1 个答案:

答案 0 :(得分:2)

您应该添加到mainActivity:

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

}

并添加到您的清单:

<activity
    android:configChanges="orientation"
</activity>