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
Related
I have been working with ActionBarSherlock recently, and follwing various tutorials, I wrote this code to add items to Action bar
#Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add("Refresh")
.setIcon(R.drawable.ic_action_refresh)
.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
menu.add("Search")// Search
.setIcon(R.drawable.ic_action_search)
.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
return true;
}
However, I dont know how to distinguish the two clicks.
Although I did find out that you have to Override onOptionsItemSelected to handle the clicks and also that a switch statement can be used to distinguish between clicks, but most tutorials use item ids from thier xml menus. Since I am not creating menus in xml how can i distinguish the clicks without ids.
private static final int REFRESH = 1;
private static final int SEARCH = 2;
#Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(0, REFRESH, 0, "Refresh")
.setIcon(R.drawable.ic_action_refresh)
.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
menu.add(0, SEARCH, 0, "Search")
.setIcon(R.drawable.ic_action_search)
.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case REFRESH:
// Do refresh
return true;
case SEARCH:
// Do search
return true;
default:
return super.onOptionsItemSelected(item);
}
}
Just check following
http://developer.android.com/guide/topics/ui/actionbar.html
which contains
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) { <--- here you can get it
case android.R.id.home:
// app icon in action bar clicked; go home
Intent intent = new Intent(this, HomeActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
you can do ti by there Id in onOptionsItemSelected ................which can be set here also
http://thedevelopersinfo.wordpress.com/2009/10/29/handling-options-menu-item-selections-in-android/
http://developer.android.com/reference/android/view/Menu.html#add(int, int, int, java.lang.CharSequence)
use
public abstract MenuItem add (int groupId, int itemId, int order, CharSequence title)
Since: API Level 1
Add a new item to the menu. This item displays the given title for its label.
Parameters
groupId The group identifier that this item should be part of. This can be used to define groups of items for batch state changes. Normally use NONE if an item should not be in a group.
itemId Unique item ID. Use NONE if you do not need a unique ID.
order The order for the item. Use NONE if you do not care about the order. See getOrder().
title The text to display for the item.
Returns
The newly added menu item.
I am trying to use the onPrepareOptionsMenu and when I press the menu, it does nothing!! Where am I going wrong?
Also, am I using the finish (); command right? When the user presses exit(menuX) I want them to exit the application completely, not the activity.
Thanks! :)
public boolean onCreateOptionsMenu(Menu menu){
super.onCreateOptionsMenu(menu);
MenuInflater getItnow = getMenuInflater();
getItnow.inflate(R.menu.main_menu, menu);
return true;
}
public boolean onPrepareOptionsMenu(MenuItem item){
switch (item.getItemId()){
case R.id.menuA:
startActivity(new Intent("com.abc.example.A"));
return true;
case R.id.menuB:
startActivity(new Intent("com.abc.example.B"));
return true;
case R.id.menuC:
startActivity(new Intent("com.abc.example.C"));
return true;
case R.id.menuD:
startActivity(new Intent("com.abc.example.D"));
case R.id.menuX:
finish();
}
return false;
}
}
First off all I would rather use onOptionsItemSelected then onPrepareOptionsMenu. Because as I understand you just want to press it. It is quite the same only first line is changed.
The second thing - app exit. Well, if this menu is in your main and first activity, then when you call finish() then this activity is destroyed (not exactly but the thing is that you don't see it anymore), so if app has no more activity everything is destroyed.
More advanced method for that is using instruction android.os.Process.killProcess(android.os.Process.myPid()); and what it is doing is simply sending kill signal for whole process. As far as your app won`t get bigger, I mean you will have only your activities in this process, than it is fine. Hope it will help.
When I create an options menu, I usually use these two methods:
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
menu.add(0, 1, 0, "option text");
.....
}
and
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
if(item.getItemId() == 1)
...
}
Also, with your switch statement, you are forgetting to put in break; for each option (which I notice mostly because I am notorious for doing the same).
I have a Gallery view, In this view I set up an options menu
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
menu.add(0, EMAIL_MENU_ID, 0, "Email");
menu.add(0, SHARE_MENU_ID, 0, "Share");
menu.add(0, RATE_MENU_ID, 0, "Rate");
menu.add(0, BUY_MENU_ID, 0, "Buy");
return true;
}
I have the following method to get the option item selected.
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case EMAIL_MENU_ID:
sendMail(index);
case SHARE_MENU_ID:
postToWeb(index);
case RATE_MENU_ID:
postRating(index);
case BUY_MENU_ID:
buy(index);
}
return super.onOptionsItemSelected(item);
}
How could i find the item of the Gallery view that's currently in focus?
Very good tutorial. In my scenario the user has pressed the Menu button and I would like to present the user with some options to take on the image that is currently in focus. At this point the user has not clicked on the image so I don't have the position that you would normally get when a user selects an item. I've thought about using a context menu instead but was hoping to use an options menu.
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()
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.