方向重新启动我的应用

时间:2016-04-27 19:19:58

标签: android android-fragments orientation screen-rotation savestate

我是编程的新手,我有一个带有碎片的导航抽屉。当我旋转我的设备时,它“重新启动应用程序”或调用Home片段。我不知道如何解决这个问题,它与super.onCreate(savedInstance)有关,但还没有找到如何实现它。我也尝试使用我的清单上的onConfigurationChanged方法,但没有运气。我一直在复制不同教程的代码,以使我的应用程序工作,所以我不确定代码即时发布应该是我应该发布的。感谢。

public class MainActivity extends FragmentActivity {


    private DrawerLayout mDrawerLayout;
    ImageView home;
    Fragment fragment = null;
    TextView appname;
    ExpandableListView expListView;
    HashMap<String, List<String>> listDataChild;
    ExpandableListAdapter listAdapter;
    List<String> listDataHeader;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        String fontPath = "neue.ttf";
        setContentView(R.layout.activity_main);
        home = (ImageView)findViewById(R.id.home);
        home.setOnClickListener(homeOnclickListener);
        appname = (TextView)findViewById(R.id.appname);
        Typeface tf = Typeface.createFromAsset(this.getAssets(), fontPath);
        appname.setTypeface(tf);

        setUpDrawer();
    }

    private void setUpDrawer() {
        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        mDrawerLayout.setScrimColor(getResources().getColor(android.R.color.transparent));
        mDrawerLayout.setDrawerListener(mDrawerListener);
        expListView = (ExpandableListView) findViewById(R.id.lvExp);
        prepareListData();
        listAdapter = new ExpandableListAdapter(this, listDataHeader, listDataChild);
        expListView.setAdapter(listAdapter);
        fragment = new Home();
        getSupportFragmentManager().beginTransaction().replace(R.id.content_frame, fragment).commit();
        mDrawerLayout.closeDrawer(expListView);

        expListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
                                @Override
                                public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
                                    switch (groupPosition) {
                                        case 0:
                                            switch (childPosition) {
                                                case 0:
                                                    fragment = new 1();
                                                    break;
                                                case 1:
                                                    fragment = new 2();
                                                    break;
                                                case 2:
                                                    fragment = new 3();
                                                    break;
                            ...
                        }
                        break;

                    case 1:
                        switch (childPosition) {
                            case 0:
                                fragment = new 4();
                                break;
                           ...
                            default:
                                break;
                        }
                        break;

                    ...
                }
                getSupportFragmentManager().beginTransaction().replace(R.id.content_frame, fragment).commit();
                mDrawerLayout.closeDrawer(expListView);
                return false;
            }
        });

    }...

1 个答案:

答案 0 :(得分:1)

在设备轮换时,重新启动活动 - 这是明显的行为。如果您不希望重新创建活动,则应在android:configChanges="orientation"文件中为该活动添加Manifest

否则,您可以通过检查setUpDrawer()这样重新启动活动来阻止调用savedInstanceState -

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        String fontPath = "neue.ttf";
        setContentView(R.layout.activity_main);
        home = (ImageView)findViewById(R.id.home);
        home.setOnClickListener(homeOnclickListener);
        appname = (TextView)findViewById(R.id.appname);
        Typeface tf = Typeface.createFromAsset(this.getAssets(), fontPath);
        appname.setTypeface(tf);

       if(savedInstanceState == null){
           setUpDrawer();
      }
    }