简体   繁体   English

Android - Finish()重启当前活动

[英]Android - Finish() restarts current activity

I have an activity that refuses to close the first time finish() is called. 我有一个活动拒绝在第一次调用finish()时关闭。 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. 我最初在活动中放了一个取消按钮,但后来我注意到它在使用Back按钮时表现出相同的行为。 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). 它可以这样描述:1。活动打开,填写适当的数据(来自上一个活动的列表视图)。 2. Press the Back button, and all the fields are cleared of data, but it doesn't return the user to the original activity. 2.按“返回”按钮,清除所有字段的数据,但不会将用户返回到原始活动。 3. Press the Back button again, and the user is returned to the original activity. 3.再次按“返回”按钮,用户将返回原始活动。

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. 我可以提一下,切换到使用上下文菜单打开Activity。

UPDATE: Ok, looks like both of these suggestions were correct, the main activity is indeed launching 2 versions of the Edit activity. 更新:好的,看起来这两个建议都是正确的,主要活动确实启动了2个版本的Edit活动。 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. 您似乎两次启动相同的Activity。 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. 发生这种情况是因为每当执行onContextItemSelected时都会调用onMenuItemSelected。 Although I'm still unsure as to why, if I use a conditional inside my onMenuItemSelected method, everything seems to work fine: 虽然我仍然不确定为什么,如果我在onMenuItemSelected方法中使用条件,一切似乎都工作正常:

 @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 我在另一篇文章中找到了这个答案: onContextItemSelected不会被调用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM