从权限对话框返回后,onActivityResult返回null getExtra值

时间:2016-11-08 20:47:29

标签: android onactivityresult startactivityforresult

这是我的代码: final Intent intent = new Intent(this, SelectWidgetActivity.class); startActivityForResult(intent, REQUEST_CREATE_APPWIDGET);

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    if (resultCode == RESULT_OK ) {
        String selectedName = data.getStringExtra(SelectWidgetActivity.SELECTED_WIDGET);
        if(!mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId, info.provider)) {
                    Intent bindIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
                    bindIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
                    bindIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.provider);
                    bindIntent.putExtra(SelectWidgetActivity.SELECTED_WIDGET, selectedName);
                    startActivityForResult(bindIntent, REQUEST_BIND_APPWIDGET);
        }
    }
}

我在第一次调用startActivityForResult时放置了String,在第二次调用时我试图获取相同的字符串,但是得到null。例如,在第一次调用selectedName =“Calendar”时,第二次调用onActivityResult()selectedName = null

有什么想法吗?

1 个答案:

答案 0 :(得分:3)

你必须检查你在拨打电话时发送的onActivityResult中的requestCode

startActivityForResult(intent, REQUEST_CREATE_APPWIDGET);

试试这个

  if (requestCode == REQUEST_CREATE_APPWIDGET && resultCode == Activity.RESULT_OK) {
        //do your work here;
    }