android startActivityForResult不包括Bundle

时间:2016-02-24 07:02:42

标签: android fragment android-support-library

我正在开发一款应用。而min SDK是14.现在我将从片段中打开新的Activity,当用户点击片段上的按钮时。

//打开新活动的代码

newFavoriteFAB.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            Intent mIntent = new Intent(getActivity(), CategoriesActivity.class);
            Bundle mBundle = new Bundle();
            mBundle.putInt(BundleConstants.OPERATION_TYPE, OperationTypes.ADD_FAVORITE);
            mIntent.putExtras(mBundle);
            startActivityForResult(mIntent, ActivityStartRequestCode.ADD_FAVORITE);
        }
    });

//活动onCreate方法,试图打开

public void onCreate(Bundle bundle) {
        super.onCreate(bundle);

        Log.d(TAG, "onCreate");
//here, when i am going to get it, i see that bundle argument is null

我试图完成活动,但它告诉我:调用需要API级别16.因为我使用API​​ 14作为最小值。

 getActivity().startActivityForResult(mIntent, ActivityStartRequestCode.ADD_FAVORITE, mBundle);

请提供有效的解决方案。 感谢

1 个答案:

答案 0 :(得分:1)

要从已启动的活动中接收数据,请使用getIntent()方法中的onCreate

即,

 Intent mIntent = new Intent(getActivity(), CategoriesActivity.class);
            mIntent.putExtra(BundleConstants.OPERATION_TYPE, OperationTypes.ADD_FAVORITE);
            startActivityForResult(mIntent, ActivityStartRequestCode.ADD_FAVORITE);

接收,使用onCreate()

Intent received = getIntent();
int var = received.getInt(....);

无论如何,如果您想要捆绑包,请使用以下内容: https://stackoverflow.com/a/16073980/1979882或者: https://stackoverflow.com/a/31021165/1979882