E
E
Eserchik2014-11-13 12:16:27
Android
Eserchik, 2014-11-13 12:16:27

Correct work of nested fragments with ActionBar menu items?

Hello, can you help me find a solution?
What is:
Android 4.2.2 (Both the device and the emulator)
Task:
Fragment (With a list in the ActionBar)-> Click on the list item -> Launches a nested fragment that should add its icons to the ActionBar menu, while the list that the parent created fragment should remain.
Problem:
The parent fragment contains a container for nested fragments, as well as the parent fragment recreates the Action Bar and adds a list (navigation menu) to it, when you click on the element of which, the desired nested fragment is displayed, which in turn should add its menu elements to the ActionBar ). Howeverwhen delegating to a nested fragment, the ability to manage menu items (setHasOptionsMenu(true)) , the application starts to endlessly recreate this very nested fragment and freezes. If delegation is disabled, then nested fragments work fine, but do not create their own menu items
Here is what LogCat outputs : Choreographer: Skipped 89 frames! The application may be doing too much work on its main thread.
My Code:
Parent Fragment

public class FragmentBugs extends Fragment  implements ActionBar.OnNavigationListener{

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

  public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    
  }
  
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_bugs_empty, container, false);
   


        return rootView;
    }


    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    	
    	 restoreActionBar();
    	 inflater.inflate(R.menu.menu_bugs, menu);
         super.onCreateOptionsMenu(menu, inflater);
         
         
    	
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        return super.onOptionsItemSelected(item);
    }
    
    private void restoreActionBar(){
        ActionBar actionBar = getActivity().getActionBar();
        actionBar.removeAllTabs();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
        actionBar.setDisplayShowTitleEnabled(true);
        actionBar.setIcon(R.drawable.ic_bug);
        actionBar.setTitle(R.string.title_section5);


   
        SpinnerAdapter mSpinnerAdapter = ArrayAdapter.createFromResource(getActivity(),
                R.array.action_list_bugs, R.layout.simple_spinner_dropdown_item);
        
        actionBar.setListNavigationCallbacks(mSpinnerAdapter, this);
 
    }


  @Override
  public boolean onNavigationItemSelected(int itemPosition, long itemId) {
    // TODO Auto-generated method stub
        FragmentManager fragmentManager = getChildFragmentManager();
    switch(itemPosition){
    case 0:
                //Если  вложенному фрагменту, я разрешу управление меню,
               // то этот пункт будет выполняться бесконечно,  пока приложение
              // не зависнет
    Log.d("MyLog", "Fr1");
          fragmentManager.beginTransaction()
          .replace(R.id.containerBugs, new BugsNewEventFragment(),"Fr1")
            .addToBackStack(null)
          .commit();
        
      break;
      
    case 1:
      Log.d("MyLog", "Fr2");
            fragmentManager.beginTransaction()
            .replace(R.id.containerBugs, new BugsEventHistoryFragment(),"Fr2")
            .addToBackStack(null)
            .commit();
      break;
    }
    return false;
  }



}

Nested snippet:
public class BugsNewEventFragment  extends Fragment {

  @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setHasOptionsMenu(true);//Если  поставить   false,  то этот вложенный фрагмент не добавит свои элементы меню,  но будет нормально работать
     
  }
  public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
  
  }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_bugs_new_event, container, false);


        
        return rootView;
    }
    
    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
     
   	 
   	    inflater.inflate(R.menu.menu_bugs_new, menu);
        super.onCreateOptionsMenu(menu, inflater);
       
    }
    


@Override
public boolean onOptionsItemSelected(MenuItem item) {
    return super.onOptionsItemSelected(item);
}


}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
AndroidDev2015, 2016-01-15
@AndroidDev2015

I didn’t try it with the parent fragment, but I did it with an activity with nested fragments, I brought the drawing and processing of the menu to the activity through an interface that listened to the opening and closing of each fragment and changed the menu accordingly. There are many ways to manage a fragment from an activity.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question