1、Android using Bundle for sharing variables

注:android中使用Bundle来共享变量,下例中Activity1和Activity2通过bundle共享一个变量myValue  

Sharing variables between Activities is quite important point during development time of your Application. This  Example suppose Activity1 from where you run up other Activity2 based on your selection from submenu.   You gonna share variable myValue

From Activity1

Intent intent = new Intent(this,myActivity2.class);
Bundle bundle = new Bundle();
bundle.putString(“myValue“, myValue);
intent.putExtras(bundle);
navigation.this.startActivity(intent);

In Activity2

Bundle bundle = getIntent().getExtras();
act2MyValue= bundle.getString(“myValue“);

Now is your application powered to share variables between two different activities.

2、

Bundle is generally used for passing data between various Activities of android. It depends on you what type of values you want to pass but bundle can hold all types of values and pass to the new activity.

You can use it like .....

Intent intent = new
Intent(getApplicationContext(),SecondActivity.class);
intent.putExtra("myKey",AnyValue);
startActivity(intent);

Now you can get the passed values by...

Bundle extras = intent.getExtras();
String tmp = extras.getString("myKey");

3、

In this example, the main Activity (appropriately named 'MainActvity') will pass information to a sub-activity (called SecondaryActivity).  SecondaryActivity will allow you to alter that information and then return it to MainActivity. The bundle of information will be sent back and forth between the Activity objects via an Intent object.

In order to pass the information we'll create our Intent object and do two things with it before sending it off.  First, we'll specify SecondaryActivity as the target by passing it into the Intent's constructor.  Then we'll stuff the information into it (the information is stored in a 'Bundle' object that lives inside the Intent - the Bundle is created when you call the putExtras() method of the Intent object).  Once our Intent is ready to go, our MainActivity will simply launch it by passing it as a parameter into startActivityForResult().

When SecondaryActivity is created, it will check to see if an Intent is available. If so, it wil extract the data from the Bundle and put it into an EditText so that you can alter it.  Then you can click a button to send it back to MainActivity.  When the button is clicked, a new Intent is created with the updated information stuffed into a Bundle.  SecondaryActivity then calls setResult() which will return the information back to MainActivity.

It's important to note that when you pass information around like this, you'll have to add an entry into the AndroidManifest.xml file.  I always seem to forget this step.  You'll have to add the following line of code into the <application> section:

<activity android:name=".SecondaryActivity"/>

Here's the code...

This is the layout file for MainActivity (main.xml)...

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="MAIN ACTIVITY"/>
<EditText android:id="@+id/editText1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<Button android:id="@+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Click To Launch Secondary Activity"/>
</LinearLayout>

Here's the code for the MainActivity class...

package com.remwebdevelopment;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Button; public class MainActivity extends Activity implements View.OnClickListener {
private final int SECONDARY_ACTIVITY_REQUEST_CODE=0;
private EditText mEditText1;
private Button mButton1;
//DONT FORGET: to add SecondaryActivity to the manifest file!!!!!
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mEditText1 = (EditText)findViewById(R.id.editText1);
mButton1 = (Button)findViewById(R.id.button1);
mButton1.setOnClickListener(this);
}
public void onClick(View view){
if(view == mButton1){
//create a new intent and specify that it's target is SecondaryActivity...
Intent intent = new Intent(getApplicationContext(),SecondaryActivity.class);
//load the intent with a key "myKey" and assign it's value
//to be whatever has been entered into the text field...
intent.putExtra("myKey",mEditText1.getText().toString());
//launch the secondary activity and send the intent along with it
//note that a request code is passed in as well so that when the
//secondary activity returns control to this activity,
//we can identify the source of the request...
startActivityForResult(intent, SECONDARY_ACTIVITY_REQUEST_CODE);
}
}
//we need a handler for when the secondary activity finishes it's work
//and returns control to this activity...
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent){
super.onActivityResult(requestCode, resultCode, intent);
Bundle extras = intent.getExtras();
mEditText1.setText(extras != null ? extras.getString("returnKey"):"nothing returned");
}
}

Here's the layout file for SecondaryActivity (secondary_activity.xml)...

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="SECONDARY ACTIVITY"/>
<EditText android:id="@+id/txtSecondary"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<Button android:id="@+id/btnSecondary"
android:text="Click To Return to Main Activity"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>

Finally, here's the code for the SecondaryActivity class...

package com.remwebdevelopment;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText; public class SecondaryActivity extends Activity{
private EditText mEditText2;
private Button mButton2;
private String mIntentString; @Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState); setContentView(R.layout.secondary_activity); mEditText2 = (EditText)findViewById(R.id.txtSecondary);
mButton2 = (Button)findViewById(R.id.btnSecondary);
//add the event handler for the button...
mButton2.setOnClickListener(new View.OnClickListener(){
public void onClick(View view){
mIntentString = mEditText2.getText().toString();
//create a new intent...
Intent intent = new Intent();
//add "returnKey" as a key and assign it the value
//in the textbox...
intent.putExtra("returnKey",mEditText2.getText().toString());
//get ready to send the result back to the caller (MainActivity)
//and put our intent into it (RESULT_OK will tell the caller that
//we have successfully accomplished our task..
setResult(RESULT_OK,intent);
//close this Activity...
finish();
}
}); //if the activity is being resumed...
mIntentString = savedInstanceState != null ? savedInstanceState.getString("myKey"):null; //check to see if a Bundle is .
if(mIntentString == null){
//get the Bundle out of the Intent...
Bundle extras = getIntent().getExtras();
//check to see if "myKey" is in the bundle, if so then assign it's value
// to mIntentString if not, assign "nothing passed in" to mIntentString...
mIntentString = extras != null ? extras.getString("myKey") : "nothing passed in";
}
//set the textbox to display mIntentString...
mEditText2.setText(mIntentString);
} }

关于android中Bundle的使用的更多相关文章

  1. Android中Bundle和Intent的区别

    Bundle的作用,以及和Intent的区别: 一.Bundle: A mapping from String values to various Parcelable types 键值对的集合 类继 ...

  2. 【转】Android中如何使用Bundle传递对象[使用Serializable或者Parcelable] -- 不错

    原文网址:http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2012/1211/694.html Android中Bundle类的作用 Bun ...

  3. Android中Intent传值与Bundle传值的区别详解

    Android中Intent传值与Bundle传值的区别详解 举个例子我现在要从A界面跳转到B界面或者C界面   这样的话 我就需要写2个Intent如果你还要涉及的传值的话 你的Intent就要写两 ...

  4. Android开发中Bundle用法包裹数据(转)

    Android开发中Bundle用法包裹数据 Bundle的经典用法,包裹数据放入Intent中,目的在于传输数据. SDK 里是这样描述: A mapping from String values ...

  5. Android中的Parcel机制 实现Bundle传递对象

    Android中的Parcel机制    实现了Bundle传递对象    使用Bundle传递对象,首先要将其序列化,但是,在Android中要使用这种传递对象的方式需要用到Android Parc ...

  6. 【转】Android中intent传递对象和Bundle的用法

    原文网址:http://blog.csdn.net/lixiang0522/article/details/8642202 android中的组件间传递的对象一般实现Parcelable接口,当然也可 ...

  7. Android中BroadcastReceiver的两种注册方式(静态和动态)详解

    今天我们一起来探讨下安卓中BroadcastReceiver组件以及详细分析下它的两种注册方式. BroadcastReceiver也就是"广播接收者"的意思,顾名思义,它就是用来 ...

  8. Android中使用ViewFlipper实现屏幕页面切换(关于坐标轴的问题已补充更改)

    屏幕切换指的是在同一个Activity内屏幕间的切换,ViewFlipper继承了Framelayout类,ViewAnimator类的作用是为FrameLayout里面的View切换提供动画效果.如 ...

  9. Android中使用ExpandableListView实现微信通讯录界面(完善仿微信APP)

    之前的博文<Android中使用ExpandableListView实现好友分组>我简单介绍了使用ExpandableListView实现简单的好友分组功能,今天我们针对之前的所做的仿微信 ...

随机推荐

  1. Java基础之线程——管理线程同步代码块(BankOperation4)

    控制台程序. 除了同步类对象的方法之外,还可以把程序中的语句或代码块制定为synchronized,这种方式更强大,因为可以指定哪个对象从语句或代码块的同步中获益,而不像同步方法那样仅仅是包含代码的对 ...

  2. 一款不错的多选下拉列表利器—— Ext.ux.form.SuperBoxSelect

    在B/S系统中,下拉列表(select/dropdownlist/combobox)的应用随处可见,为了增强用户体验,开发人员也常常会做一些带联想功能的下拉列表, 特别是数据项比较多的时候,用户筛选起 ...

  3. Java 集合类型

  4. 最长上升子序列的变形(N*log(N))hdu5256

    序列变换 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

  5. fences(桌面整理软件)与eDiary3.3.3下载链接

    fences:  http://www.jb51.net/softs/309746.html http://jingyan.baidu.com/article/e8cdb32b6e958337042b ...

  6. jsp页面中的java代码

    jsp页面中的java代码 1.jsp表达式  <%= ....%>  只能放置一个变量常量 2. jsp小脚本 <% .... %>  java语句,可以插入一些语句 3. ...

  7. po line received is canceled(恢复PO被取消的余量)

    1張PO已部分收貨,後來由于某種原因,將部分收貨的PO明行取消,現在要對已收料的這一部分進行退貨處理,要怎麼做才好呢? [@more@]DATA COLLECTED===============COL ...

  8. java 网络编程(二)----UDP基础级的示例

    下面介绍UDP基础级的代码示例: 首先了解创建UDP传输的发送端的思路: 1.创建UDP的Socket服务.2.将要发送的数据封装到数据包中.3.通过UDP的socket服务将数据包发送出去.4.关闭 ...

  9. DB2 表空间和缓冲池

    简介 对于刚涉足 DB2 领域的 DBA 或未来的 DBA 而言,新数据库的设计和性能选择可能会很令人困惑.在本文中,我们将讨论 DBA 要做出重要选择的两个方面:表空间和缓冲池.表空间和缓冲池的设计 ...

  10. innodb内部的并发线程

    1. innodb_thread_concurrency innodb有一系列的计数器来统计和控制内部的工作线程.其中最重要的一个是innodb_thread_concurrency,和它相关的inn ...