android - 带参数启动 Activity

标签 android android-activity oncreate

我是 Android 开发的新手。

我想创建并启动一项 Activity 以显示有关游戏的信息。我表明我需要一个 gameId 的信息。

如何将此游戏 ID 传递给 Activity ?游戏 ID 是绝对必要的,所以如果没有 ID,我不想创建或启动 Activity 。

就像 Activity 只有一个参数的一个构造函数。

我该怎么做?

谢谢。

最佳答案

将一个 int 作为你的 id 放入新的 Intent

Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
Bundle b = new Bundle();
b.putInt("key", 1); //Your id
intent.putExtras(b); //Put your id to your next Intent
startActivity(intent);
finish();

然后在你的新 Activity 中获取 id:

Bundle b = getIntent().getExtras();
int value = -1; // or other values
if(b != null)
    value = b.getInt("key");

关于android - 带参数启动 Activity,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3913592/

相关文章:

android - 如何在主线程上显示 ANR 错误对话框

android - Activity 与 fragment 实现

Android 和 bundle

android - 在应用程序启动时启动相机

android - 如何将 Vitamio 库导入 Android 应用程序

android - ImageView 作为网络图像的掩码或参数 - 如何?

android - 谷歌浏览器在 Android 模拟器上以白屏打开

Android - 在 SurfaceView 上绘图未出现

java - 打开抽屉导航时如何使背景 Activity 变小?

Android服务onCreate被多次调用,没有调用onDestroy