• Skip to main content
  • Skip to secondary menu
  • Skip to primary sidebar
  • Skip to footer
  • Home
  • About
  • Projects
    • GStaticMap WP Plugin
  • Contact
  • Privacy Policy

Lorenz Blog

All About Web & Mobile Application Development

  • Featured Articles
  • Gadgets
    • Android
    • Blackberry
  • Programming
    • Android
    • PHP
    • Java Script
    • MySQL
    • Postgresql
    • Flex
    • Web
  • Software
    • Mac OS
    • Windows
    • Linux
  • Web
You are Here » Home >> Information Technology >> Programming >> Android >> Android Coding Tips: How to Create Options Menu on Child Activity inside an ActivityGroup

Android Coding Tips: How to Create Options Menu on Child Activity inside an ActivityGroup

March 11, 2011 by Lorensius Londa 3 Comments

Android ActivityGroup is a screen that contains and run multiple embedded child activities. It works just like a normal Activity but on ActivityGroup, we can create multiple child activities and  the ActivityGroup will be the parent activity. Creating options menu on a normal activity and a child activity inside an ActivityGroup is different.  To create options menu, first override the onCreateOptionsMenu and onOptionsItemSelected method on parent activity, then override the two methods on child activity as usual.

On parent activity

public class TestGroup extends ActivityGroup {
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		//start child activity
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
	    return getLocalActivityManager().getCurrentActivity().onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onMenuItemSelected(int featureId, MenuItem item) {
	    return getLocalActivityManager().getCurrentActivity().onMenuItemSelected(featureId, item);
	}
}

On child activity

public class ChildActivity extends Activity {
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		//your code goes here
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		//add menu here
		//do not call super.onCreateOptionsMenu(menu) here

	    return true;
	}

	@Override
	public boolean onMenuItemSelected(int featureId, MenuItem item) {
		//handle on menu item selected here
		//do not call super.onMenuItemSelected(featureId, item) here

	    return true;
	}
}

As we can see from the two code snippets above, onCreateOptionsMenu and onMenuItemSelected are also called in the parent activity. This has to be done because menu can be handled in the parent activity but created in child activity.

Facebooktwitterredditpinterestlinkedinmailby feather

Related posts:

  1. How to Create Custom Window Title in Android
  2. How to Make Android Map Scrollable Inside a ScrollView Layout
  3. Coding Tips: How to Use Custom Font in Android
  4. Coding Tips: How to Use Custom Transition Animation in Android

Filed Under: Android, Information Technology, Programming Tagged With: activity group, Android, context menu, option menu, tab activity

About Lorensius Londa

Passionate web and mobile application developer. Co-founder of TRUSTUDIO, loves programming, Android, aviation, travelling, photography, coffee and gym mania.

Reader Interactions

Comments

  1. Sam says

    February 15, 2012 at 1:24 am

    I’ve tried above code,but i’m getting the same menu for both activity, I have different menu.xml for each activity, please let me know how to fix this…

    Reply
  2. Tamika says

    November 23, 2012 at 11:50 pm

    It’s an amazing paragraph in support of all the web people; they will obtain advantage from it I am sure.

    Reply
  3. ranjith says

    December 3, 2012 at 2:28 pm

    hai…. very helpful one.. i use the option menus to navigate from one activity to another activity…

    where the codes to be placed??? can you hel me…

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About Me

A husband, father of two, passionate software developer, diy lover and home baker who loves to learn new things. Read More…

  • Facebook
  • GitHub
  • Google+
  • Instagram
  • Twitter
  • YouTube

Featured Articles

How to Setup MQTT Server Using Mosquitto and Libwebsocket on Freebsd

Blue Bamboo P25 Printer Android Demo Application With Source Code

Simple JSON RPC Client for Android

How to Send Message to Google Cloud Messaging (GCM) Server Using JSON and PHP

Footer

Recent Comments

  • Aditya Dabas on About
  • Ayten Göksenin Barutçu on How to Make Android Map Scrollable Inside a ScrollView Layout
  • mang jojot on About
  • Hussain on How to Programmatically Scan or Discover Android Bluetooth Devices

Recent Posts

  • How to Fix Blank Screen on WordPress Add/Edit Post Page
  • How to Programmatically Restart the ESP32 Board
  • How to Get Hardware Info of ESP32
  • How to Setup MQTT Server Using Mosquitto and Libwebsocket on Freebsd

Latest Tweets

To protect our users from spam and other malicious activity, this account is temporarily locked. Please log in to https://twitter.com to unlock your account.

Copyright © 2023 · Magazine Pro on Genesis Framework · WordPress · Log in