我是 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/