Unable to finish activity from onOptionsItemSelected in Android

后端 未结 1 1535
予麋鹿
予麋鹿 2021-01-22 12:47

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 r

相关标签:
1条回答
  • 2021-01-22 13:20

    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.

    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.
        }
    }
    
    0 讨论(0)
提交回复
热议问题