使用参数启动 Activity [英] Start an Activity with a parameter

查看:39
本文介绍了使用参数启动 Activity的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 Android 开发非常陌生.

I'm very new on Android development.

我想创建并启动一个活动来显示有关游戏的信息.我显示了我需要游戏 ID 的信息.

I want to create and start an activity to show information about a game. I show that information I need a gameId.

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

How can I pass this game ID to the activity? The game ID is absolutely necessary so I don't want to create or start the activity if it doesn't have the ID.

这就像活动只有一个带有一个参数的构造函数.

It's like the activity has got only one constructor with one parameter.

我该怎么做?

谢谢.

推荐答案

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

Put an int which is your id into the new 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:

Then grab the id in your new Activity:

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

这篇关于使用参数启动 Activity的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆