无法完成Android中onOptionsItemSelected的活动 [英] Unable to finish activity from onOptionsItemSelected in Android

查看:69
本文介绍了无法完成Android中onOptionsItemSelected的活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从菜单选项中关闭活动".当选择菜单项,(和在调试时)我注意到,调试器总是从返回真步骤到默认跳跃. 我尝试使用ActivityClassName.this.finish(),但仍然得到相同的结果

I am trying to close the Activity from menu option. When menuItem menu_close_activity is selected, (and while debugging) I noticed that debugger always jumps from return true step to default. I tried to use ActivityClassName.this.finish(), but I am still getting the same results

@Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.menu_xxxx:
                break;
            case R.id.menu_yyyy:
                break;
            case R.id.close_activiy:
                // doing some stuff here;
                setResult(0001);
                finish();    // Debugger jumps from here
                return true;
            default:
                return super.onOptionsItemSelected(item); // Debugger jumps to here.
        }
    }

为什么我要跳到默认值,而不要返回true?

Why I am jumping to default, and not going to return true?

除此方法外,我public boolean onCreateOptionsMenu(Menu menu)除了增大选项菜单外什么都不做,而protected void onCreate(Bundle savedInstanceState)

Beside this method I have public boolean onCreateOptionsMenu(Menu menu) doing nothing but inflating the options menu, and protected void onCreate(Bundle savedInstanceState)

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_name_list);
        Bundle b = getIntent().getExtras();
        name = b.getString("name");
        setTitle("Students of " + name);
    }

推荐答案

我看不到您的活动未完成的原因.它应该.您可以在onDestroy()方法中添加一些日志,看看它正在被调用吗.

I don't see the reason why you activity is not finishing. It should. Could you add some logs to onDestroy() method and see it's getting called.

关于Debugger为何跳的原因,在这种情况下,请不要相信调试器.我认为它之所以会跳跃是因为在不同的线程中发生了不同的事情.而是添加一些日志以查看实际发生的情况.用以下代码替换您的代码,并检查日志.

As to why the Debugger is Jumping, well, don't trust the debugger in this case. I think it jumps because of different stuff happening in different threads. Instead add some logs to see what actually happens. Replace your code with the following and check the logs.

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.menu_xxxx:
            break;
        case R.id.menu_yyyy:
            break;
        case R.id.close_activiy:
            // doing some stuff here;
            Log.d("DEBUG","BEFORE FINISH = "+item.toString());
            setResult(1);
            finish();    // Compiler jumps from here
            Log.d("DEBUG","AFTER FINISH = "+item.toString());
            return true;
        default:
            Log.d("DEBUG","DEFAULT STATEMENT = "+item.toString());
            return super.onOptionsItemSelected(item); // Compiler jumps to here.
    }
}

这篇关于无法完成Android中onOptionsItemSelected的活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆