(Deprecated) Fragment Onoptionsitemselected Not Being Called

(Deprecated) Fragment onOptionsItemSelected not being called

Edit for actionbar sherlock use

I had to use

public boolean onMenuItemSelected(int featureId, MenuItem item) {

in the main activity to capture the menu item

onOptionsItemSelected not being called from within fragment

You are using an "action layout" for this item. This is just going to act like a normal view and clicking on it won't trigger the onOptionsItemSelected callback. You have to get a reference to the layout itself and add a click listener:

@Override
public void onCreateOptionsMenu(final Menu menu, final MenuInflater inflater) {
inflater.inflate(R.menu.activity_recent_destinations, menu);
MenuItem item = menu.findItem(R.id.menu_action_item_clear);
View actionView = item.getActionView();
// Set the listener on the root view or any children if necessary
actionView.setOnClickListener(...);
}

Android onOptionsItemSelected is not functioning

As I found that the stack actually has nothing to pop(I wanna know why...), I choose an alternative to do so.

I don't know whether it is the best solution but it seems work.

replace

getFragmentManager().popBackStack();

to

FragmentManager fm = getFragmentManager();
if (fm.getBackStackEntryCount()>0){
//Log.d("Bottom Tab", "popping backstack");
fm.popBackStack();
} else {
//Log.d("Bottom Tab", "nothing can pop");
super.onBackPressed();
}

Even the stack has nothing, it will override the onBackPressed() to go back.

onCreateOptionsMenu deprecated

I found the answer.

In your fragment you add a MenuHost from which you call oncreateMenu and onMenuItemSelected.

To get the whole answer follow: 'setHasOptionsMenu(Boolean): Unit' is deprecated. Deprecated in Java

It's worth mentioning that Android Studio's docs still shows the full documentation for onCreateOptionsMenu even tough it seems to be deprecated

setHasOptionsMenu(Boolean): Unit' is deprecated. Deprecated in Java

From the Developer documentation, this can be achieved by the following:

/**
* Using the addMenuProvider() API directly in your Activity
**/
class ExampleActivity : ComponentActivity(R.layout.activity_example) {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

// Add menu items without overriding methods in the Activity
addMenuProvider(object : MenuProvider {
override fun onCreateMenu(menu: Menu, menuInflater: MenuInflater) {
// Add menu items here
menuInflater.inflate(R.menu.example_menu, menu)
}

override fun onMenuItemSelected(menuItem: MenuItem): Boolean {
// Handle the menu selection
return true
}
})
}
}

/**
* Using the addMenuProvider() API in a Fragment
**/
class ExampleFragment : Fragment(R.layout.fragment_example) {

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
// The usage of an interface lets you inject your own implementation
val menuHost: MenuHost = requireActivity()

// Add menu items without using the Fragment Menu APIs
// Note how we can tie the MenuProvider to the viewLifecycleOwner
// and an optional Lifecycle.State (here, RESUMED) to indicate when
// the menu should be visible
menuHost.addMenuProvider(object : MenuProvider {
override fun onCreateMenu(menu: Menu, menuInflater: MenuInflater) {
// Add menu items here
menuInflater.inflate(R.menu.example_menu, menu)
}

override fun onMenuItemSelected(menuItem: MenuItem): Boolean {
// Handle the menu selection
return when (menuItem.itemId) {
R.id.menu_clear -> {
// clearCompletedTasks()
true
}
R.id.menu_refresh -> {
// loadTasks(true)
true
}
else -> false
}
}
}, viewLifecycleOwner, Lifecycle.State.RESUMED)
}

Fragments setHasOptionsMenu deprecated, use setHasOptionsMenu

onCreateOptionsMenu not being called on FragmentActivity when run on phone version

You should consider from your Fragment code:

1) Implementing onCreateOptionsMenu(Menu menu, MenuInflater inflater)

2) Calling setHasOptionsMenu

3) And also implementing onOptionsItemSelected(MenuItem item)

Then you will be ok on both the phone and tablet.



Related Topics



Leave a reply



Submit