Titanium Mobile
  1. Titanium Mobile
  2. TIMOB-11602

Android: Expose activity.invalidateOptionsMenu to allow changing action items in Android 3.0 and above

    Details

      Description

      As described in http://developer.android.com/guide/topics/ui/menus.html#ChangingTheMenu, to change menu items (action items) in 3.0 and above, we'd need to expose the invalidateOptionsMenu method.

        Activity

        Hide
        Allen Yeung added a comment -

        Test Steps:
        1. Add the following entry into your Tiapp.xml:

            <android xmlns:android="http://schemas.android.com/apk/res/android">
                <manifest>
                    <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="14"/>
                </manifest>
            </android>
        

        2. Run the code snippet:

        var win = Ti.UI.createWindow({
        	fullscreen : true
        });
        var LOGIN = 1, LOGOUT = 2;
        var loggedIn = false;
        
        var activity = win.activity;
        
        activity.onCreateOptionsMenu = function(e) {
        	var menu = e.menu;
        	var login = menu.add({
        		title : "Login",
        		itemId : LOGIN
        	});
        	login.setIcon("login.png");
        	login.addEventListener("click", function(e) {
        		loggedIn = true;
        		win.activity.invalidateOptionsMenu();
        	});
        	login.showAsAction = Ti.Android.SHOW_AS_ACTION_ALWAYS;
        	var logout = menu.add({
        		title : "Logout",
        		itemId : LOGOUT
        	});
        	logout.setIcon("logout.png");
        
        	logout.addEventListener("click", function(e) {
        		loggedIn = false;
        		win.activity.invalidateOptionsMenu();
        	}); 
        
        	logout.showAsAction = Ti.Android.SHOW_AS_ACTION_ALWAYS;
        };
        
        activity.onPrepareOptionsMenu = function(e) {
        	var menu = e.menu;
        	menu.findItem(LOGIN).setVisible(!loggedIn);
        	menu.findItem(LOGOUT).setVisible(loggedIn);
        };
        
        win.addEventListener('click', function(){
        	win.activity.openOptionsMenu();
        });
        
        win.open(); 
        

        3. Click the login button in the action bar and it should change to logout

        This should be run on a 2.3 and a 3.0+ devices. When testing on a 2.3 device also click on the window and make sure that the menu bar opens.

        Show
        Allen Yeung added a comment - Test Steps: 1. Add the following entry into your Tiapp.xml: <android xmlns:android="http://schemas.android.com/apk/res/android"> <manifest> <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="14"/> </manifest> </android> 2. Run the code snippet: var win = Ti.UI.createWindow({ fullscreen : true }); var LOGIN = 1, LOGOUT = 2; var loggedIn = false; var activity = win.activity; activity.onCreateOptionsMenu = function(e) { var menu = e.menu; var login = menu.add({ title : "Login", itemId : LOGIN }); login.setIcon("login.png"); login.addEventListener("click", function(e) { loggedIn = true; win.activity.invalidateOptionsMenu(); }); login.showAsAction = Ti.Android.SHOW_AS_ACTION_ALWAYS; var logout = menu.add({ title : "Logout", itemId : LOGOUT }); logout.setIcon("logout.png"); logout.addEventListener("click", function(e) { loggedIn = false; win.activity.invalidateOptionsMenu(); }); logout.showAsAction = Ti.Android.SHOW_AS_ACTION_ALWAYS; }; activity.onPrepareOptionsMenu = function(e) { var menu = e.menu; menu.findItem(LOGIN).setVisible(!loggedIn); menu.findItem(LOGOUT).setVisible(loggedIn); }; win.addEventListener('click', function(){ win.activity.openOptionsMenu(); }); win.open(); 3. Click the login button in the action bar and it should change to logout This should be run on a 2.3 and a 3.0+ devices. When testing on a 2.3 device also click on the window and make sure that the menu bar opens.
        Show
        Ping Wang added a comment - PR: https://github.com/appcelerator/titanium_mobile/pull/3369
        Show
        Allen Yeung added a comment - 3.X PR: https://github.com/appcelerator/titanium_mobile/pull/3414
        Hide
        Tamila Smolich (Inactive) added a comment -

        Closing as fixed. Expected behavior is shown.
        Tested and verified on:
        Titanium Studio, build: 3.0.0.201211121907
        Titanium SDK, builds: 3.0.0.v20121113111657; 3.1.0.v20121112192600
        Device: Samsung Galaxy SIII (4.0.4)

        Show
        Tamila Smolich (Inactive) added a comment - Closing as fixed. Expected behavior is shown. Tested and verified on: Titanium Studio, build: 3.0.0.201211121907 Titanium SDK, builds: 3.0.0.v20121113111657; 3.1.0.v20121112192600 Device: Samsung Galaxy SIII (4.0.4)
        Hide
        Arthur Evans added a comment -

        Reopening to add label.

        Show
        Arthur Evans added a comment - Reopening to add label.

          People

          • Assignee:
            Allen Yeung
            Reporter:
            Arthur Evans
          • Watchers:
            4 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved: