Handling ActionBar title with the fragment back stack?

前端 未结 11 1461
挽巷
挽巷 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 13:50

    As the original answer is quite old, this might come of help as well. As the documentation states, one might want to register a listener to listen on the back stack changes in the hosting Activity:

    getSupportFragmentManager().addOnBackStackChangedListener(
            new FragmentManager.OnBackStackChangedListener() {
                public void onBackStackChanged() {
                    // Update your UI here.
                }
            });
    

    Then, identify the situation in the callback method and set a proper title, without accessing the ActionBar from the Fragment.

    This is a more elegant solution as the Fragment doesn't have to know about the ActionBar existence and Activity is usually the place that is managing the backstack so having it handled over there seems to be more appropriate. Fragment should at all time be considered only by its own content, not the surroundings.

    More on the topic in the documentation.

    0 讨论(0)
  • 2020-12-07 13:50

    To update the actionbar title on back press. Just simply put

    getActivity.setTitle("title")

    inside onCreateView method.

    0 讨论(0)
  • 2020-12-07 13:55

    Let the controlling activity do all the work as follows:

    Listen for backstack events (in onCreate() of activity):

    // Change the title back when the fragment is changed
        getSupportFragmentManager().addOnBackStackChangedListener(new FragmentManager.OnBackStackChangedListener() {
            @Override
            public void onBackStackChanged() {
                Fragment fragment = getFragment();
                setTitleFromFragment(fragment);
            }
        });
    

    Get the current fragment from the container:

    /**
     * Returns the currently displayed fragment.
     * @return
     *      Fragment or null.
     */
    private Fragment getFragment() {
        Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.container);
        return fragment;
    }
    

    Set the fragment inside the content view:

    private void setFragment(Fragment fragment, boolean addToBackStack) {
        // Set the activity title
        setTitleFromFragment(fragment);
        .
        .
        .
    }
    
    0 讨论(0)
  • 2020-12-07 13:58

    I use a similar solution to Lee approach, but replacing onBackStackChanged() method instead.

    First I set the fragment name when adding the transaction to the back stack.

    getSupportFragmentManager().beginTransaction()
                    .replace(R.id.frame_content, fragment)
                    .addToBackStack(fragmentTitle)
                    .commit();
    

    Then I override the onBackStackChanged() method and I call setTitle() with the last backstack entry name.

    @Override
    public void onBackStackChanged() {
        int lastBackStackEntryCount = getSupportFragmentManager().getBackStackEntryCount() - 1;
        FragmentManager.BackStackEntry lastBackStackEntry =
                getSupportFragmentManager().getBackStackEntryAt(lastBackStackEntryCount);
    
        setTitle(lastBackStackEntry.getName());
    }
    
    0 讨论(0)
  • 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);
        }
    
    }
    
    0 讨论(0)
提交回复
热议问题