I have a TabActivity which contains an Activity. When the tab for the activity is selected, if I press the Menu button, onPrepareOptionsMenu is called in the parent TabActivity, but not on the activity for the tab which was selected.
The options menu for the activity in the tab isn't shown unless I click inside the tab, then I get calls to both (which is what I want). Is there any way to 'focus' the activity in the tab when the tab is selected?
Just use this code for all tab activity without giving option menu for TabHost (Main Activity)
/**...........Context Menu........cg.*/
public boolean onCreateOptionsMenu (Menu menu) {
menu.add(0, 0, Menu.NONE, "Refresh");
menu.add(0, 1, Menu.NONE, "Exit");
return true;
}
/**---------- Handles item selections ------------cg */
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case 0:
System.out.println("Refresh");
return true;
case 1:
System.out.println("Exit");
return true;
}
return false;
}
I hope it will help you.
Thank you,
Ganapathy.
I am not sure I understand the question.
IF you want to refresh the current TAB, the one that is in focus, and the menu is from the parent (Tabs), try this:
Inside the menu you use:
Activity MyActivity = this.getCurrentActivity();
if ( MyClassActivity.class.isInstance(MyActivity) == true )
{
((MyClassActivity)MyActivity).Refresh();
}
You should have a refresh function implemented in your activity.
Hope this helps you.
Adrian.
Related
My problem is as follows:
I have one activity with many fragments. Main activity has "three dots" menu.
When I call a fragment, it has it's own menu (which is showing).
This fragment is a form with EditText fields, and checkboxes.
When a user changes something in those fields, and then press the "save" icon in the toolbar, the values get collected, and sent to the web service, and updated. BUT only the first time.
After going back (back arrow in toolbar), and returning to a fragment (new fragment gets created), after editing the fields, and pressing the "save" button, the form doesn't get saved/updated.
I debugged it, and found out that the "save" button which gets called for the second time, is actually the "save" button of the previous fragment.
The values in the editText fields gets collected inside a HashMap. And this HashMap gets updated and filled with new data inside new fragment. BUT when it gets to calling onOptionsItemSelected(), OLD DATA from the previous (or THE FIRST fragment) gets in the focus, and those data gets saved/updated.
So, my question is this: Is there a way to detach the menu items from the fragment, so when another (new) fragment is called, and gets created it attaches its own menu option to itself (not from the first fragment ever created)?
I tested it with System.identityHashCode(fragment), and when a user edit fields in "second" fragment, the hash code is OK, BUT when the "save" gets called hash code gets changed to the hash code of the first fragment.
This is the part of code from FormFragment:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
menu.clear();
inflater.inflate(R.menu.menu_main, menu);
int itemId = 0;
for (ActionList menuItem : allData.getActionList()) {
menu.add(1, itemId, itemId, menuItem.getPrompt());
itemId++;
}
inflater.inflate(R.menu.menu_activity, menu);
super.onCreateOptionsMenu(menu, inflater);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.action_favourite) {
if (valuesArray != null) {
((CommInterface) getActivity()).getUpdateList(valuesArray, taskUrl, dataItemId);
return true;
} else {
Toast.makeText(getActivity(), "Not updated!", Toast.LENGTH_SHORT).show();
return true;
}
}
for (ActionList menuName : allData.getActionList()) {
if (item.getTitle().equals(menuName.getPrompt())) {
((CommInterface) getActivity()).formMenuItemSelected(menuName.getActionName(), position);
return true;
}
}
return super.onOptionsItemSelected(item);
}
And the part of code from MainActivity:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_choose_instance:
....
case android.R.id.home:
if (height > width) {
getSupportFragmentManager().popBackStack();
toolbarTitles.remove(toolbarTitlesCurrentNumber);
toolbarTitlesCurrentNumber--;
toolbar.setTitle(toolbarTitles.get(toolbarTitlesCurrentNumber));
if (toolbarTitlesCurrentNumber == 0) {
getSupportActionBar().setDisplayHomeAsUpEnabled(false);
getSupportActionBar().setHomeButtonEnabled(false);
}
} else if (...) {
....
}
default:
return super.onOptionsItemSelected(item);
}
}
Create the menu items under fragments not under activity,
see the previous discussion
Android Options Menu in Fragment
The answer to this problem is: use the getChildFragmentManager() in TabbedPagerAdapter.
I was using the Activity's getSupportFragmentManager() which is wrong when you are using NESTED fragments like in TabbedFragment which has a ViewPager, and a ViewPager has (child) Fragments inside.
I have 2 xml one main xml and one menu xml. I want to get the click event from menu xml to main xml activity. is there is any way to solve this problem.
Main Home Page
Menu Page
Now click on the menu page button event i want in my main home activity page.
I done Like this
View otherLayout = LayoutInflater.from(this).inflate(R.layout.menu_layout,null);
Button tstclick = (Button) otherLayout.findViewById(R.id.textclick);
tstclick.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//Your thing
System.exit(0);
}
});
The Activity class provides us two methods to inflate/show menu items and work with them or assign some tasks to them. They are:
onCreateOptionsMenu(Menu menu)
onOptionsItemSelected(MenuItem item)
The onCreateOptionsMenu() method is responsible for creating and inflating the menu by help of MenuInflater class.
The onOptionsItemSelected() method is responsible for assigning tasks to each menu item. Each menu item is identified by help of its unique ID.
In order to show and work with menu's in any Activity, both the methods needs to be overridden as shown below:
public class MainActivity extends AppCompatActivity {
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem menuItem) {
switch (menuItem.getItemId()) {
// All your menu items will come here. Each menu item ID will become a separate case in the Switch case
default:
return super.onOptionsItemSelected(menuItem);
}
}
}
As soon as the user clicks or taps on any menu item, the onOptionsItemSelected() method is called by the Android OS and then the ID for that particular menu item will be matched in the method. The group of statements specified in the respective case will then be executed.
For more info visit the following links:
http://developer.android.com/guide/topics/ui/menus.html
http://developer.android.com/reference/android/view/MenuInflater.html
I am developing an app using actionbar sherlock. The problem I have is that I have the menus setup and all my xml files are correct since I get no compiler and logcat error.
THe problem I am having is that when I load a fragment A with its own specific menu everything is good. But when I then move to another fragment B, fragment A menu appears on fragment B. Any ideas as to whats causing this.
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
menu.clear();
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.product_allergy, menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
super.onOptionsItemSelected(item);
switch(item.getItemId()){
case R.id.productClear:
addtoList();
break;
}
return true;
}
The menu is belonged to Activty rather than fragments. Fragment A and B are in same activity, so when A adds some items into activity's menu and then it is replaced by Fragment B, the activity does not change, i.e., the menu is still there.
If you want your Fragment B to have different menu items, override the onCreateOptionsMenu() method, remember to setHasOptionsMenu(true) in Fragment's onCreate() method.
FYI, if you simply want to clear prior fragment's menu item, call menu.clear() in onCreateOptionsMenu().
EDIT:
When you are trying to handle the action bar callback in fragments, you should do something like below:
#override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.fragmentActionBarButton:
//your code
return true;
default:
return super.onOptionsItemSelected(item);
}
}
I have an activity that refuses to close the first time finish() is called. I had originally put a Cancel button in the activity, but then I noticed that it exhibits the same behavior when I use the Back button too. It can be described like this:
1. The activity opens, and the appropriate data is filled in (which came from a List View from the previous activity).
2. Press the Back button, and all the fields are cleared of data, but it doesn't return the user to the original activity.
3. Press the Back button again, and the user is returned to the original activity.
I can post code, but I'm not sure if the issue is in the current activity or the main one. I can mention that switched to using a context menu to open the Activity.
UPDATE:
Ok, looks like both of these suggestions were correct, the main activity is indeed launching 2 versions of the Edit activity. Now I'm just trying to figure out why. I noticed this happening when I switched over to a context menu for the list item options. The Add button is a Menu Item, however. Here is the relevant code:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
// add(int groupId, int itemId, int order, int titleRes)
menu.add(0, INSERT_ID, 0, R.string.menu_insert);
menu.add(0, SETTINGS_ID, 1, R.string.sharedPrefButton);
menu.add(0, COMMON_DESC_ID, 2, R.string.commonDescButton);
menu.add(0, RESET_ID, 3, R.string.menu_reset);
return true;
}
/*
* This is going to handle the "Add Expenditure" menu item. When this is selected, the
* onOptionsItemSelected() method will be called with the item.getId() set to INSERT_ID
* (the constant we used to identify the menu item).
*/
#Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
switch(item.getItemId()) {
case INSERT_ID:
createExpenditure();
break;
case SETTINGS_ID:
manageSharedPrefs();
break;
case COMMON_DESC_ID:
manageCommonDesc();
break;
}
return super.onMenuItemSelected(featureId, item);
}
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
menu.add(0, DELETE_ID, 0, R.string.menu_delete);
menu.add(0, EDIT_ID, 0, R.string.menu_edit);
}
public boolean onContextItemSelected(MenuItem item) {
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
int itemId = item.getItemId();
switch(itemId) {
case DELETE_ID:
mDbHelper.deleteExpenditure(info.id);
fillData();
break;
case EDIT_ID:
Intent i = new Intent(this, ExpenditureEdit.class);
i.putExtra(ExpendituresDbAdapter.KEY_ROWID, info.id);
startActivityForResult(i, ACTIVITY_EDIT);
break;
}
return super.onContextItemSelected(item);
}
It appears that you starting the same Activity twice. This is your Activity stack:
Original Activity, starts ListActivity
ListActivity (no data in fields), starts another instance of itself
ListActivity (fill in appropriate data)
Now when you push the back button:
ListActivity, close this second instance but...
ListActivity (still no data), the first instance resumes, so push back again
Original Activity
(Without code I cannot help you move than to describe what I think is happening...)
This is happening because apparently onMenuItemSelected is called whenever onContextItemSelected is executed. Although I'm still unsure as to why, if I use a conditional inside my onMenuItemSelected method, everything seems to work fine:
#Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
if (featureId == Window.FEATURE_CONTEXT_MENU)
{
Log.i("EZBudget","Context Menu");
}
else if (featureId == Window.FEATURE_OPTIONS_PANEL)
{
switch(item.getItemId()) {
case INSERT_ID:
createExpenditure();
break;
case SETTINGS_ID:
manageSharedPrefs();
break;
case COMMON_DESC_ID:
manageCommonDesc();
break;
}
}
return super.onMenuItemSelected(featureId, item);
}
I found this answer in another post: onContextItemSelected doesn't get called
My problem is, that I have got a TabActivity, which has 4 Tabs right now. The first Tab is a special Details-Tab, where the user could modify some data.
The problem is, that if I add a OptionsMenu for the Activity, that the OptionsMenu is appearing on every Tab.
I tried to check the current mTabHost.getCurrentTabTag() in the onCreateOptionsMenu but that changed nothing.
So, how to do that?
(The following code, which still shows the OptionsMenu on every Tab)
public boolean onCreateOptionsMenu(Menu menu) {
if(mTabHost.getCurrentTabTag()==getString(R.string.tab_details)) {
boolean result = super.onCreateOptionsMenu(menu);
menu.add(0, EDIT_ID, 0, R.string.menu_edit).setIcon(R.drawable.edit);
return result;
}
return true;
}
public boolean onMenuItemSelected(int featureId, MenuItem item) {
switch (item.getItemId()) {
case EDIT_ID: {
Toast.makeText(this, "o.O", Toast.LENGTH_LONG).show();
}
}
return super.onMenuItemSelected(featureId, item);
}
I see that you are comparing strings (tags in fact) with ==, usually, this is nicer to do that with .equals() method. Maybe it will solve your issue.
To dynamically update an option menu (from the documentation) :
If you want to change the Options
Menu each time it opens, you must
override the onPrepareOptionsMenu()