【问题标题】:android startActivityForResult not including Bundleandroid startActivityForResult 不包括 Bundle
【发布时间】:2016-02-24 07:02:41
【问题描述】:

我正在开发一个应用程序。最小 SDK 为 14。现在我要从 Fragment 打开新的 Activity,当用户单击 Fragment 上的按钮时。

//打开新活动的代码

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);
        }
    });

//activity 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);

请提供可行的解决方案。 谢谢

【问题讨论】:

    标签: android fragment android-support-library


    【解决方案1】:

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

    也就是说,

     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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-28
      • 2018-12-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多