Getting out of the application or moving to a start-up fragment with a "go" button.



  • There's a diver with fragments, when the application starts, the first fragment is launched, and through the driveway, the airways can move to different fragments. The question is, how do you do if we press the button on the first start-up, the annex closes, and other fragments when we press the bottom button, did the initial fragment open?



  • I found the answer. http://ru.androids.help/q5651 I've been doing this.

    Initial fragment:

     @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        getView().setFocusableInTouchMode(true);
        getView().requestFocus();
        getView().setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if (event.getAction() == KeyEvent.ACTION_DOWN) {
                    if (keyCode == KeyEvent.KEYCODE_BACK) {
                        getActivity().finish();
                        return true;
                    }
                }
                return false;
            }
        });
    }
    

    any other fragment:

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        getView().setFocusableInTouchMode(true);
        getView().requestFocus();
        getView().setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if (event.getAction() == KeyEvent.ACTION_DOWN) {
                    if (keyCode == KeyEvent.KEYCODE_BACK) {
                        FragmentFirst fragmentFirst = new FragmentFirst();
                        FragmentManager fragmentManager = getFragmentManager();
                        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                        fragmentTransaction.replace(R.id.mainFrame, fragmentFirst);
                        fragmentTransaction.commit();
                        // помечаем в драйвере первый фрагмент
                        MainActivity.navigationView.getMenu().getItem(0).setChecked(true);
                        return true;
                    }
                }
                return false;
            }
        });
    }
    



Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2