Control the back “button” in android

前端 未结 2 659
没有蜡笔的小新
没有蜡笔的小新 2021-01-14 09:20

I want to get the Text data value from Sub-Activity back. And everything is ok. But when the Sub-activity was opened, then I just click back \"button\" on the phone, it thro

相关标签:
2条回答
  • 2021-01-14 10:14

    I think Nate is correct about the NPE at intent.getExtras().

    You should be checking the value of your result code before you do anything. You also might want to check to make sure intent.hasExtra(SBooksDbAdapter.KEY_TITLE_RAW) before you get that value. So, something like this:

    switch(requestCode){
    case ACTIVITY_SEARCH:
        if (resultCode == RESULT_OK && intent.hasExtra(SBooksDbAdapter.KEY_TITLE_RAW)) {
            title_raw = intent.getStringExtra(SBooksDbAdapter.KEY_TITLE_RAW);
            ... do all that other stuff ...
        }
    }
    
    0 讨论(0)
  • 2021-01-14 10:21

    Try putting in some logging into the onActivityResult(...) method of your main activity. The "back" button from the subactivity may be giving you a result code of RESULT_CANCELED, and the intent can be null in that case. So you'll get an NPE at

    Bundle bundle = intent.getExtras();

    0 讨论(0)
提交回复
热议问题