misrak
misrak

Reputation: 60

Android refresh actionbar and title on fragment change

in my app i am developing an activity using the actionbar in NAVIGATION_MODE_TABS-mode. Each tab is showing a fragment (list, detail). Initially the list-tab is visibile. The list is implementing setMultiChoicheModeListener() and modifies the ActionBar and the title of the activity if one or more items are selected. How can i reset the title and the ActionBar to the inital value (title and actions) when the user clicks on the detail-tab without deselecting the items?

BTW Target-Platform is > 4.1 and i am not using the support library.

Thanks.

public class MyActivity extends Activity {

    @Override
    protected void onCreate (Bundle savedInstanceState) {
        this.actionBar = getActionBar();
        this.actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
        ....
        for (Tab tab : getTabs())
        {
           //here are two tabs added (List and Detail)
           this.actionBar.addTab(tab);
        }
        ....
    }

    protected class NavigationTabListener implements ActionBar.TabListener {

        private Fragment fragment;
        ....
        public void onTabSelected(Tab tab, FragmentTransaction ft) {
           ft.replace(newFragmentResourceId, this.fragment);
        }
    }
}

public class MyListViewFragment extends LinearLayout implements IListViewFragment {

     ....

     @Override
     public void initialize() {
         inflate(getContext(), listLayoutResourceId, this); 
         this.myList.setChoiceMode(AbsListView.CHOICE_MODE_MULTIPLE_MODAL);
         this.myList.setMultiChoiceModeListener(new MultiChoiceModeListener() {

        ....

        @Override
        public boolean onCreateActionMode(ActionMode mode, Menu menu)
        {
            MenuInflater inflater = mode.getMenuInflater();
            inflater.inflate(selectedItemsMenuResourceId, menu);

            return true;
        }

        public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked)
        {
            if (checked)
            {
                this.numberItemsSelected++;
                adapter.setNewSelection(position);
            }
            else
            {
                this.numberItemsSelected--;
                adapter.removeSelection(position);
            }

            mode.setTitle(getContext().getResources().getQuantityString(
                R.plurals.items_selected, this.numberItemsSelected,
                Integer.valueOf(this.numberItemsSelected)));

         }

         ....

     }
}

I am trying to implement the MVP pattern, but it's still in evaluation phase. The Activity acts as the presenter, the views are in separate classes.

For each Fragment i am also implementing the MVP apttern, but i think this is not interesting to solve the problem.

Some notes to the classes:

MyActivity creates two fragments (one for List, one for Detail view, the detail view has nothing to do with the selected items). The initial view of the activity is the fragment with the list. If the user selects some entries I am updating the action bar and the title through the callback of MultiChoiceModeListener. But the user can now change the fragment by clicking on the "Detail" tab without deselecting the items or clicking to the new elements in the action bar, the result is that the detail fragment is shown, but the title of the activity is still the one I modified in the MultiChoiceModeListener, and there is also the check mark of the action bar visible (auto created by the system). So the best way is I think to get somehow the current ActionMode, so I can invoke finish() to "reset" the ActionBar and the title.

Upvotes: 2

Views: 3626

Answers (1)

Nikola Despotoski
Nikola Despotoski

Reputation: 50578

Make sure you keep reference of the ActionMode in the ActionMode.Callback methods inside your activity which has the ActionBar.TabListener. When a new tab is selected just finish the action mode, like:

public void onTabSelected(Tab tab, FragmentTransaction ft) {
           if(mActionMode != null){
              mActionMode.finish();
           }
           ft.replace(newFragmentResourceId, this.fragment);

 }

Make the ActionMode reference back to null when onDestroyActionMode(ActionMode) is called.

Upvotes: 1

Related Questions