Intent 和 Bundle 的简单示例 [英] Simple example for Intent and Bundle

查看:19
本文介绍了Intent 和 Bundle 的简单示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 android 新手,对 Java 和 XML 几乎一无所知.我正在通过我在网上获得的 pdf 来学习它.我了解了 Toast,了解了一些 Intent,但我对 Bundle 一无所知.我知道它们用于将数据从一个活动传递到另一个活动,但我无法实现这一点.

I'm new to android with almost no knowledge about Java and XML. I'm learning it through pdfs that i'm getting on net. I have learnt about Toast, a bit about Intents but me not able to understand anything about Bundles. I have understood that they are used to pass data from one activity to another but I'm not able to implement this.

请举一个简单的例子来实现.

please give a simple example to implement the same.

例如我刚刚创建了两个活动,即 Main_Activity 和 Other_Activity,我还没有对它们做任何事情.

as for example I have just created two activities namely , Main_Activity and Other_Activity, and i haven't done anything to them yet.

请举一个最简单的例子,让我学习实现.

Please give the simplest example so that i can learn to implement.

提前致谢!!

推荐答案

例如:

在主活动中:

Intent intent = new Intent(this, OtherActivity.class);
intent.putExtra(OtherActivity.KEY_EXTRA, yourDataObject);
startActivity(intent);

在其他活动中:

public static final String KEY_EXTRA = "com.example.yourapp.KEY_BOOK";

@Override
protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);

  String yourDataObject = null;

  if (getIntent().hasExtra(KEY_EXTRA)) {
      yourDataObject = getIntent().getStringExtra(KEY_EXTRA);
  } else {
      throw new IllegalArgumentException("Activity cannot find  extras " + KEY_EXTRA);
  }
  // do stuff
}

更多信息在这里:http://developer.android.com/reference/android/content/Intent.html

这篇关于Intent 和 Bundle 的简单示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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