1. Download our Official Android App: Forums for Android!

onCreateOptionsMenu is never called

Discussion in 'Android Development' started by umtblbl, Sep 9, 2019.

  1. umtblbl

    umtblbl Newbie
    Thread Starter
    Rank:
     #590
    Points:
    16
    Posts:
    49
    Joined:
    Mar 6, 2019

    Mar 6, 2019
    49
    5
    16
    Hi my friends, I want to make a search bar and want to capture each letter entry. However, onCreateOptionsMenu is not called at all, no debug falls on that line.

    Code (Text):
    1.  
    2. class PhotoFragment : Fragment() {
    3.  
    4.     companion object {
    5.         fun newInstance() = PhotoFragment()
    6.     }
    7.  
    8.     @Inject
    9.     lateinit var dataManager: DataManager
    10.  
    11.     private lateinit var viewModel: PhotoViewModel
    12.  
    13.     var searchList: MutableList<String> = ArrayList()
    14.  
    15.     override fun onCreateView(
    16.         inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?
    17.     ): View? {
    18.         val view = inflater.inflate(R.layout.photo_fragment, container, false)
    19.  
    20.         (activity as AppCompatActivity).supportActionBar
    21.         view.toolbarProfileSearch.setNavigationOnClickListener { context }
    22.         setHasOptionsMenu(true)
    23.  
    24.         return view
    25.  
    26.     }
    27.  
    28.     override fun onActivityCreated(savedInstanceState: Bundle?) {
    29.         super.onActivityCreated(savedInstanceState)
    30.         componentFragment.inject(this)
    31.         viewModel = ViewModelProviders.of(this, PhotoFragmentFactory(dataManager)).get(PhotoViewModel::class.java)
    32.         setHasOptionsMenu(true)
    33.  
    34.  
    35.         viewModel.profileResult.observe(this, Observer { profileResult ->
    36.             loadPicture(profileResult)
    37.         })
    38.  
    39.  
    40.         btnGetProfilePicture.setOnClickListener {
    41.             viewModel.callProfileResult(editUserName.text.toString())
    42.         }
    43.  
    44.  
    45.     }
    46.  
    47.     override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
    48.  
    49.         inflater.inflate(R.menu.toolbar_menu_searchprofile, menu)
    50.         val searchItem = menu.findItem(R.id.search)
    51.         val searchView = searchItem.actionView as SearchView
    52.  
    53.         searchView.setOnQueryTextListener(object : SearchView.OnQueryTextListener {
    54.             override fun onQueryTextSubmit(query: String?): Boolean {
    55.                 searchView.clearFocus()
    56.                 searchView.setQuery("", false)
    57.                 searchItem.collapseActionView()
    58.                 Log.d("SEARCH1", "$query")
    59.  
    60.                 return true
    61.  
    62.             }
    63.  
    64.             override fun onQueryTextChange(newText: String?): Boolean {
    65.                 Log.d("SEARCH2", "$newText")
    66.                 return false
    67.  
    68.             }
    69.         })
    70.  
    71.  
    72.         super.onCreateOptionsMenu(menu, inflater)
    73.     }
    74. }
    75.  
     

    Advertisement

  2. wseng92

    wseng92 Android Enthusiast
    Rank:
     #163
    Points:
    73
    Posts:
    250
    Joined:
    Oct 26, 2015

    Oct 26, 2015
    250
    47
    73
    Male
    Did it solve?
     
  3. umtblbl

    umtblbl Newbie
    Thread Starter
    Rank:
     #590
    Points:
    16
    Posts:
    49
    Joined:
    Mar 6, 2019

    Mar 6, 2019
    49
    5
    16
    I used
    in onCreateView -> setHasOptionsMenu(true)
    onActivityCreated -> (activity as AppCompatActivity).setSupportActionBar(toolbarProfileSearch)
    solved.
     
    wseng92 likes this.

Share This Page

Loading...