2016-09-04 12 views
10
@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    // Handle action bar item clicks here. The action bar will 
    // automatically handle clicks on the Home/Up button, so long 
    // as you specify a parent activity in AndroidManifest.xml. 
    int id = item.getItemId(); 

    //noinspection SimplifiableIfStatement 
    if (id == R.id.action_filter) { 
     FragmentManager fm = getSupportFragmentManager(); 
     if (userType.equals("İş Arayan")) 
      filterDialogTitle = "İş İlanları Filtre"; 
     else if (userType.equals("Hizmet Arayan")) 
      filterDialogTitle = "Hizmet İlanları Filtre"; 
     FilterDialogFragment editNameDialogFragment = FilterDialogFragment.newInstance(filterDialogTitle); 
     editNameDialogFragment.show(fm, "fragment_edit_name"); 
     return true; 
    } 

    return super.onOptionsItemSelected(item); 
} 

मैं टुकड़ा में जोड़ा में बुला onOptionsItemSelected, लेकिन मैं टी कहा जाता है, अगर मैं MainActivity में जोड़ने के लिए, यह काम करता है, लेकिन मैं टुकड़ा में फोन चाहते हैं 'नहीं था। मैं यह कैसे कर सकता हूँ ?जोड़ें टुकड़ा

उत्तर

19

टुकड़ा में आप setHasOptionsMenu(true)

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

तो फोन लगता है आप को संभाल करने के लिए है menu_item_to_handle_in_fragment आइटम पर क्लिक करें

टुकड़ा वर्ग के लिए

@Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     switch (item.getItemId()) { 

     case R.id.menu_item_to_handle_in_fragment: 
      // Do onlick on menu action here 
      return true; 
     } 
    return false; 
} 

गतिविधि वर्ग

@Override 
     public boolean onOptionsItemSelected(MenuItem item) { 
      switch (item.getItemId()) { 

      case R.id.menu_item_to_handle_in_fragment: 
       return false; 
      } 
     return false; 
    } 
के लिए
2

आपको अपने टुकड़े के टुकड़े में setHasOptionMenu(true) जोड़ने की आवश्यकता है।
जब आप इस विकल्प को जोड़ते हैं तो खंड जीवनशैली ऑनक्रेटऑप्शनमेनू() और ऑनऑप्टीइटेम चयन() पर कॉल करेगी।

इस चरणों का पालन करें:

  • setHasOptionsMenu (सही) अपने टुकड़ा की onCreate में विधि() जोड़ें।

  • क्रिएटऑप्शनमेनू (मेनू मेनू, मेनू इन्फ्लूटर inflater) और पर ओवरराइड ऑनऑप्शन इटैम चयन (मेनूइटेम आइटम) विधियों में आपके टुकड़े में।

  • अपने onOptionsItemSelected (MenuItem आइटम) के अंदर गतिविधि की विधि, सुनिश्चित करें कि आप रिटर्न फाल्स जब मेनू आइटम कार्रवाई onOptionsItemSelected (MenuItem आइटम) में लागू टुकड़ा के तरीका होगा बनाते हैं।

संबंधित मुद्दे