Handling ActionBar title with the fragment back stack?

前端 未结 11 1473
挽巷
挽巷 2020-12-07 13:10

I have an Activity where I load in a ListFragment and, upon clicking, it drills down a level and a new type of ListFragment is shown,

11条回答
  •  伪装坚强ぢ
    2020-12-07 14:01

    You can Solve with onKeyDown! I have a bool mainisopen=true <-- MainFragment is Visible other Fragment mainisopen=false

    and here is My Code:

    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && mainisopen == false) {
            mainisopen = true;
            HomeFrag fragment = new HomeFrag();
            FragmentTransaction fragmentTransaction =
                    getSupportFragmentManager().beginTransaction();
            fragmentTransaction.replace(R.id.fragmet_cont, fragment);
            fragmentTransaction.commit();
            navigationView = (NavigationView) findViewById(R.id.nav_view);
            navigationView.getMenu().findItem(R.id.nav_home).setChecked(true);
            navigationView.setNavigationItemSelectedListener(this);
            this.setTitle("Digi - Home"); //Here set the Title back
            return true;
        } else {
            if (keyCode == KeyEvent.KEYCODE_BACK && mainisopen == true) {
                AlertDialog.Builder builder = new AlertDialog.Builder(this);
                builder.setMessage("Wollen sie die App schliessen!");
                builder.setCancelable(true);
    
                builder.setPositiveButton("Ja!", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        System.exit(1);
                    }
                });
    
                builder.setNegativeButton("Nein!", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(getApplicationContext(), "Applikation wird fortgesetzt", Toast.LENGTH_SHORT).show();
                    }
                });
    
                AlertDialog dialog = builder.create();
                dialog.show();
    
                return true;
            }
            return super.onKeyDown(keyCode, event);
        }
    
    }
    

提交回复
热议问题