android 开发套件
In this tutorial, we’ll be discuss about android bundle to pass data between activities.
在本教程中,我们将讨论有关Android bundle以便在活动之间传递数据的问题。
Android套件 (Android Bundle)
Android Bundle is used to pass data between activities. The values that are to be passed are mapped to String keys which are later used in the next activity to retrieve the values.
Android Bundle用于在活动之间传递数据。 将要传递的值映射到String键,以后在下一个活动中将其用于检索值。
Following are the major types that are passed/retrieved to/from a Bundle.
以下是传入/传出捆绑包的主要类型。
putInt(String key, int value)
,getInt(String key, int value)
putInt(String key, int value)
,getInt(String key, int value)
putIntArray(String key, int[] value)
,getStringArray(String key, int[] value)
putIntArray(String key, int[] value)
,getStringArray(String key, int[] value)
putIntegerArrayList(String key, ArrayList value)
,getIntegerArrayList(String key, ArrayList value value)
putIntegerArrayList(String key, ArrayList value)
,getIntegerArrayList(String key, ArrayList value value)
putString(String key, String value)
,getString(String key, String value)
putString(String key, String value)
,getString(String key, String value)
putStringArray(String key, String[] value)
,getStringArray(String key, String[] value)
putStringArray(String key, String[] value)
,getStringArray(String key, String[] value)
putStringArrayList(String key, ArrayList value)
,getStringArrayList(String key, ArrayList value value)
putStringArrayList(String key, ArrayList value)
,getStringArrayList(String key, ArrayList value value)
putLong(String key, long value)
,getLong(String key, long value)
putLong(String key, long value)
,getLong(String key, long value)
putLongArray(String key, long[] value)
,getLongArray(String key, long[] value)
putLongArray(String key, long[] value)
,getLongArray(String key, long[] value)
putBoolean(String key, boolean value)
,getBoolean(String key, boolean value)
putBoolean(String key, boolean value)
,getBoolean(String key, boolean value)
putBooleanArray(String key, boolean[] value)
,getBooleanArray(String key, boolean[] value)
putBooleanArray(String key, boolean[] value)
,getBooleanArray(String key, boolean[] value)
putChar(String key, char value)
,getChar(String key, char value)
putChar(String key, char value)
,getChar(String key, char value)
putCharArray(String key, char[] value)
,getBooleanArray(String key, char[] value)
putCharArray(String key, char[] value)
,getBooleanArray(String key, char[] value)
putCharSequence(String key, CharSequence value)
,getCharSequence(String key, CharSequence value)
putCharSequence(String key, CharSequence value)
,getCharSequence(String key, CharSequence value)
putCharSequenceArray(String key, CharSequence[] value)
,getCharSequenceArray(String key, CharSequence[] value)
putCharSequenceArray(String key, CharSequence[] value)
,getCharSequenceArray(String key, CharSequence[] value)
putCharSequenceArrayList(String key, ArrayList value)
,getCharSequenceArrayList(String key, ArrayList value value)
putCharSequenceArrayList(String key, ArrayList value)
,getCharSequenceArrayList(String key, ArrayList value value)
使用Android套件 (Using Android Bundle)
A Bundle is passed in the following way.
捆绑包通过以下方式传递。
Intent intent = new Intent(this,SecondActivity.class);
Bundle bundle = new Bundle();
bundle.putString("key_1", "MainActivity greeted you with a HI");
bundle.putBoolean("key_2", true);
intent.putExtras(bundle);
startActivity(intent);
Data from a Bundle is retrieved in the SecondActivity.java
in the following manner.
通过以下方式在SecondActivity.java
中检索来自Bundle的数据。
Bundle bundle = getIntent().getExtras();
String title = bundle.getString("key_1");
boolean b = bundle.getBoolean("key_2");
If the key doesn’t map to any value, it may lead to NullPointerException. Hence it’s recommended to add null checks for the Bundle as well as the retrieved values.
如果键没有映射到任何值,则可能导致NullPointerException 。 因此,建议对Bundle以及检索到的值添加空检查。
Alternatively, we can set a default value too in case the mapped key doesn’t have any value.
另外,如果映射键没有任何值,我们也可以设置默认值。
Bundle bundle = getIntent().getExtras();
String title = bundle.getString("key_1", "Default");
boolean b = bundle.getBoolean("key_2", false);
To remove a value from the bundle the remove()
method is passed with the relevant key as shown below.
要从包中remove()
值,请使用相关键传递remove()
方法,如下所示。
bundle.remove("key_2");
To remove all data from the Bundle, the method clear()
is called on the Bundle instance.
要从Bundle中删除所有数据,请在Bundle实例上调用clear()
方法。
Android Bundle示例项目结构 (Android Bundle Example Project Structure)
Android捆绑示例代码 (Android Bundle Example Code)
The code for the activity_main.xml
layout is given below.
下面给出了activity_main.xml
布局的代码。
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:app="https://schemas.android.com/apk/res-auto"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.journaldev.bundles.MainActivity">
<Button
android:id="@+id/btnPassBundles"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="PASS BUNDLES"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/btnNoPassBundle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="PASS NO BUNDLE"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginTop="8dp"
app:layout_constraintTop_toBottomOf="@+id/btnPassBundles" />
</android.support.constraint.ConstraintLayout>
The first button would pass a bundle with data into the SecondActivity.java
while the second button would pass an empty bundle
第一个按钮将带有数据的捆绑包传递到SecondActivity.java
而第二个按钮将传递一个空捆绑包
The code for the activity_second.xml
layout is given below.
下面给出了activity_second.xml
布局的代码。
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:app="https://schemas.android.com/apk/res-auto"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.journaldev.bundles.MainActivity">
<TextView
android:id="@+id/txtString"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="String from MainActivity"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/txtBoolean"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Boolean value from MainActivity"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginTop="8dp"
app:layout_constraintTop_toBottomOf="@+id/txtString" />
</android.support.constraint.ConstraintLayout>
The code for the MainActivity.java
is given below.
MainActivity.java
的代码如下。
package com.journaldev.bundles;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
Button btnPassBundles, btnNoPassBundle;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnPassBundles = findViewById(R.id.btnPassBundles);
btnNoPassBundle = findViewById(R.id.btnNoPassBundle);
btnPassBundles.setOnClickListener(this);
btnNoPassBundle.setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.btnPassBundles:
Bundle bundle = new Bundle();
bundle.putString("key_1", "MainActivity greeted you with a HI");
bundle.putBoolean("key_2", true);
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
intent.putExtras(bundle);
startActivity(intent);
break;
case R.id.btnNoPassBundle:
bundle = new Bundle();
bundle.putString("key_1", "This string shall be displayed in the SecondActivity");
bundle.putBoolean("key_2", true);
bundle.clear();
intent = new Intent(MainActivity.this, SecondActivity.class);
intent.putExtras(bundle);
startActivity(intent);
break;
}
}
}
The code for the SecondActivity.java
is given below.
SecondActivity.java
的代码如下。
package com.journaldev.bundles;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;
public class SecondActivity extends AppCompatActivity {
TextView txtString, txtBoolean;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
txtString = findViewById(R.id.txtString);
txtBoolean = findViewById(R.id.txtBoolean);
Bundle bundle = getIntent().getExtras();
txtString.setText(bundle.getString("key_1", "No value from the MainActivity"));
txtBoolean.setText("Does bundle contain data? " + bundle.getBoolean("key_2", false));
}
}
The output of the above application in action is given below.
下面给出了上面应用程序的输出。
Try passing in an ArrayList of any data type in the bundle and display them in a ListView in the next Activity!
尝试传入捆绑中任何数据类型的ArrayList ,并在下一个Activity中将它们显示在ListView中!
This brings an end to android bundle tutorial. You can download the final Android Bundle Project from the link below.
这结束了android bundle教程。 您可以从下面的链接下载最终的Android Bundle Project 。
Reference: Official Doc
参考: 官方文件
android 开发套件