My App needs to refresh the menu items (showing and hiding items) on Android when a certain action is performed. I have an implementation of a searchView on my ActionBar and it works when the App first launches. However, after my menu refreshes using invalidateOptionsMenu() when a user does something, my searchView stops working. The faded searchView icon changes to the menu item icon that was set, and the menu item no longer does anything.
Currently using: Titanium Studio/SDK 3.3.0 (Alloy)
The error I get as a result is:
[ERROR] : MenuProxy: (main) [5494,5494] View already has a parent. Can't add it as an action viewHow do I remove this view's parent?
I ended up going back to the Titanium Alloy example that was provided on the documentation to see if this happens in the example. By adding an invalidateOptionsMenu() elsewhere in the code, this was reproducible.
I have also tried to implement onPrepareOptionsMenu() but it seems like onCreateOptionsMenu and onPrepareOptionsMenu are both run when invalidateOptionsMenu is executed when printing out info on the console. And I read that onPrepareOptionsMenu isn't used when an ActionBar is used, yet it seems to be from the console..
I am still new at this. Am I using this correctly? Can someone who has used this be able to provide some insight as to getting around this error?
index.html
<Alloy> <Window id="win" backgroundColor="blue" fullscreen="false" layout="vertical"> <TableView id="tableview" top="10" searchAsChild="false"> <TableViewRow title="Apple" /> <TableViewRow title="Banana" /> <TableViewRow title="Orange" /> <TableViewRow title="Raspberry" /> </TableView> </Window> </Alloy>
index.js
// use action bar search view var search = Alloy.createController("searchview").getView(); $.tableview.search = search; $.win.addEventListener("open", function() { $.win.activity.onCreateOptionsMenu = function(e) { Ti.API.info('Create...'); // log e.menu.add({ title: "Table Search", icon: (Ti.Android.R.drawable.ic_menu_search ? Ti.Android.R.drawable.ic_menu_search : "my_search.png"), actionView: search, showAsAction : Ti.Android.SHOW_AS_ACTION_ALWAYS | Ti.Android.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW }); } $.win.activity.invalidateOptionsMenu(); }); // This was added in that broke the SearchView $.tableview.addEventListener("click", function() { $.win.activity.invalidateOptionsMenu(); // refresh menu }); $.win.open();
searchview.xml
<Alloy> <SearchView id="searchView" ns="Ti.UI.Android" platform="android" hintText="Table Search" /> </Alloy>