0

Meine Symbolleistenschaltfläche wird nicht in Fragmenten angezeigt, die ich mit versucht habe setHasOptionsMenu (true);. Wenn ich auf das Element der Navigationsleiste klicke, wird ein neues Fragment für jedes Fragment geladen, das eine andere Aktionsleiste anzeigen soll (Benachrichtigung oder Speichern). Folgendes ist mein Code, danke im Voraus.Aktionsleistenschaltfläche wird nicht im Fragment angezeigt

Es folgt mein Fragment Code: (Es ist für alle Fragmente in Navigationsleiste)

public class Company1 extends Fragment { 

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

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 

    View view = inflater.inflate(R.layout.fragment_company_profile,container, false); 

    init(view); 

    btnAdd.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      showAddDialog(); 
     } 
    }); 


    return view; 
} 

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

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    switch (item.getItemId()) { 
     case R.id.notification: 
      break; 

    } 
    return super.onOptionsItemSelected(item); 
} 

} 

Dies ist company_profile.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" > 

    <item 
     android:id="@+id/notification" 
     android:icon="@drawable/ic_action_notification" 
     android:title="@string/mnuNotificationText" 
     app:showAsAction="always"/> 
    </menu> 
+0

I s es erscheint überhaupt nicht in irgendeinem Fragment oder nur in einigen Fällen (nicht aktualisiert usw.)? – Shaishav

+0

nicht in allen Fragmenten mit Navigationsschublade. – Harshad07

Antwort

1

Bitte überprüfen Sie diese Code-Schnipsel

public class Company1 extends Fragment { 

@Override 
public void onCreate(@Nullable Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    } 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 

    View view = inflater.inflate(R.layout.fragment_company_profile,container, false); 
    setHasOptionsMenu(true); 
    init(view); 

    btnAdd.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      showAddDialog(); 
     } 
    }); 


    return view; 
} 

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

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    switch (item.getItemId()) { 
     case R.id.notification: 
      break; 

    } 
    return super.onOptionsItemSelected(item); 
} 
@Override 
    public void onPrepareOptionsMenu(Menu menu) { 
     //You can change menuitem property 
     //menu.findItem(R.id.notification).setVisible(true); 
     super.onPrepareOptionsMenu(menu); 

    } 
}