带抽屉导航的Android主页按钮停止与旋转屏幕一起使用

时间:2016-10-25 14:13:33

标签: android android-fragments rotation navigation-drawer

我有一个带导航抽屉的主要活动和一个加载片段的按钮。

启动主要活动时,我加载MainFragment。 在这一刻,一切顺利。汉堡包图标正在显示,我的主片段已加载。

接下来,我单击一个按钮并加载secondFragment。加载第二个片段后,我可以使用backbutton和homebutton来回到主要活动而没有问题。

当我加载第二个片段并进行旋转屏幕时,后退按钮继续正常工作,但主页按钮消失,并出现汉堡包图标。

我做错了什么?

  public class MainActivity extends AppCompatActivity {
        private ActionBarDrawerToggle actionBarDrawerToggle;
        private DrawerLayout drawerLayout;
        private Toolbar toolbar;
        private CharSequence mDrawerTitle;
        private CharSequence mTitle;
        private String mSubTitle;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            toolbar = (Toolbar) findViewById(R.id.toolbar);
            setSupportActionBar(toolbar);
            initNavigationDrawer();


            if (savedInstanceState != null) {
                    return;
            }

            MainActivityFragment mainFragment = new MainActivityFragment();

            firstFragment.setArguments(getIntent().getExtras());
            // Add the fragment to the 'fragment_container' FrameLayout
            FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();

            //transaction.replace(R.id.fragment_container, firstFragment);
            transaction.replace(R.id.fragment_container, mainFragment, "princ");
            transaction.commit();

        }


        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            // Only handle with DrawerToggle if the drawer indicator is enabled.
            if (actionBarDrawerToggle.isDrawerIndicatorEnabled() &&
                    actionBarDrawerToggle.onOptionsItemSelected(item)) {
                return true;
            }
            // Handle your other action bar items...
            return super.onOptionsItemSelected(item);
        }
        }

        public Fragment getCurrentFragment(){
            FragmentManager frgmgr = getSupportFragmentManager();
            return frgmgr.findFragmentById(R.id.fragment_container);
        }

        @Override
        public void onBackPressed() {
            if (drawerLayout != null) {
                if (drawerLayout.isDrawerOpen(GravityCompat.START)) {
                    drawerLayout.closeDrawer(GravityCompat.START);
                    return;
                }
            }
            if ((getCurrentFragment() instanceof MainFragment)) {
                finish();
            } else if (getSupportFragmentManager().getBackStackEntryCount() > 0) {
                getSupportFragmentManager().popBackStack();
            } else   super.onBackPressed();
        }


        public void initNavigationDrawer() {
            NavigationView navigationView = (NavigationView) findViewById(R.id.navigation_view);
            navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
                @Override
                public boolean onNavigationItemSelected(MenuItem menuItem) {
                    FragmentManager frgmgr = getSupportFragmentManager();
                    FragmentTransaction transaction = frgmgr.beginTransaction();
                    Fragment cur_frag;
                    // Handle navigation view item clicks here.
                    int id = menuItem.getItemId();

                    switch (id) {
                        case R.id.home:
                            cur_frag = frgmgr.findFragmentById(R.id.fragment_container);
                            if (!(cur_frag instanceof MainActivityFragment)) {
                                MainActivityFragment frag = new MainActivityFragment();
                                transaction.setCustomAnimations(R.anim.enter_from_left, R.anim.exit_to_right, R.anim.enter_from_right, R.anim.exit_to_left);
                                transaction.replace(R.id.fragment_container, frag);
                                transaction.commit();
                                break;
                            }
                            drawerLayout.closeDrawers();
                            break;
                        case R.id.logout_drw:
                            finish();

                    }
                    drawerLayout.closeDrawer(GravityCompat.START);
                    return true;
                }
            });
            View header = navigationView.getHeaderView(0);
            TextView tv_email = (TextView) header.findViewById(R.id.tv_email);
            tv_email.setText("Agendamento");

            drawerLayout = (DrawerLayout) findViewById(R.id.drawer);
            mTitle = mDrawerTitle = getTitle();

            actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.drawer_open, R.string.drawer_close) {
                @Override
                public void onDrawerClosed(View v) {
                    super.onDrawerClosed(v);
                }

                @Override
                public void onDrawerOpened(View v) {
                    super.onDrawerOpened(v);
                }
            };
            drawerLayout.addDrawerListener(actionBarDrawerToggle);
            actionBarDrawerToggle.syncState();

            if (toolbar != null) {
                getSupportFragmentManager().addOnBackStackChangedListener(new FragmentManager.OnBackStackChangedListener() {
                    @Override
                    public void onBackStackChanged() {
                        int i=getSupportFragmentManager().getBackStackEntryCount();
                        if ((i > 0) || (!(getCurrentFragment() instanceof MainActivityFragment))) {
                            getSupportActionBar().setDisplayHomeAsUpEnabled(true); // show back button
                            toolbar.setNavigationOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    onBackPressed();
                                }
                            });
                        } else {
                            getSupportActionBar().setDisplayHomeAsUpEnabled(false);
                            actionBarDrawerToggle.syncState();
                            toolbar.setNavigationOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    drawerLayout.openDrawer(GravityCompat.START);
                                }
                            });
                        }
                    }
                });

            }
        }
    }

主要片段:

public class MainFragment extends Fragment {
    public MainFragment() {
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.fragment_main, container, false);
        Button butCons= (Button) view.findViewById(R.id.butcons);

        butCons.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SecundaryFrag frag = new SecundaryFragFrag();
                // Add the fragment to the 'fragment_container' FrameLayout
                FragmentTransaction transaction=getFragmentManager().beginTransaction();
                transaction.setCustomAnimations(R.anim.enter_from_left, R.anim.exit_to_right, R.anim.enter_from_right, R.anim.exit_to_left);

                transaction.replace(R.id.fragment_container, frag);
                transaction.addToBackStack(null);
                transaction.commit();

            }
        });
        return view;
    }
}

次要片段:

public class SecundaryFrag extends Fragment{
    public SecundaryFrag() {
    }
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.fragment_consulta_ag, container, false);
        return view;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                ((MainActivity)getActivity()).onBackPressed();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }
}

轮换后:

After Rotation Image

主要片段的主要活动: Main Activity

轮换之前:

Before Rotation

1 个答案:

答案 0 :(得分:1)

我认为这与在方向改变后重新创建视图的事实有关。我会仔细研究一下MainActivity中的onCreate(),尤其是

if (savedInstanceState != null) {
   return;
}

被触发。

一种选择是保留碎片并将其恢复到创建。你也可以通过捕捉方向并在应用程序中自己处理它来关闭娱乐,你可以在这里看到两个选项解释developer.android.com/guide/topics/resources /