【问题标题】:Getting intent extras from just one class从一门课中获得额外的意图
【发布时间】:2016-02-24 14:20:14
【问题描述】:

有没有一种方法可以让我只接收一个班级的意向附加值?例如,我有两个活动使用意图打开第三个活动。现在我只希望前两个中的一个传递额外内容,以便我可以在第三个活动中收到它。

我已经实现了一切,但它崩溃了,因为当我从第二个没有添加额外内容的活动中打开第三个活动时,它会使我的应用程序崩溃。

为了更清楚地描绘它:

  • 活动 1:传递 Intent Extras
  • 活动 2:不传递任何额外内容
  • 活动 3:接收 Intent Extras

Activity 1 和 2 都使用 Intent 来启动 Activity 3,但我只需要 Activity 1 的附加功能。

【问题讨论】:

    标签: java android object android-intent


    【解决方案1】:

    只做一个空检查

    Intent intent = getIntent();
    Bundle data = intent.getExtras();
    if(data != null)
    {
        String one = intent.getString("data_one", null); // use your data type
    }
    else
    {
        // No extra received
    }
    

    【讨论】:

    • 工作。谢谢 :) 对于阅读本文的人,要使 Bundle 工作,您必须为 API12 构建或放置 Annotation @TargetApi
    【解决方案2】:

    也许就这么简单?

    if(getIntent() != null && getIntent().getExtras() != null)
    {
      //presume this is Activity1, get extras that we need
      ...
    } else {
      //any non-Activity1 activity launched it
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-09-30
      • 1970-01-01
      • 2017-11-27
      • 2023-03-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多