众所周知,Android中使用Intent将数据从一个活动传递到另一个活动。但是还有另一种方法,可以使用一种更好的方式和更少的代码空间将数据从一个活动传递到另一个活动,即通过在Android中使用Bundles。 Android捆绑包通常用于将数据从一个活动传递到另一个活动。基本上,这里使用键值对的概念,其中要传递的数据是映射的值,以后可以通过使用键来检索。包与意图一起使用,并且值以与意图相同的方式发送和检索。这取决于用户要传递的值的类型,但是包可以容纳所有类型的值(int,String,boolean,char)并将其传递给新活动。
以下是从捆绑包传递/检索到的主要类型:
putInt(String key, int value), getInt(String key, int value)
putString(String key, String value), getString(String key, String value)
putStringArray(String key, String[] value), getStringArray(String key, String[] value)
putChar(String key, char value), getChar(String key, char value)
putBoolean(String key, boolean value), getBoolean(String key, boolean value)
在Android应用中使用捆绑包
该捆绑包始终与Android中的Intent一起使用。现在使用Bundle在MainActivity中编写以下代码。
Java
// creating a intent
Intent intent = new Intent(this, SecondActivity.class);
// crating a bundle object
Bundle bundle = new Bundle();
// storing the string value in the bundle
// which is mapped to key
bundle.putString("key1", "GFG :- Main Activity");
// passing the bundle into the intent
intent.putExtras(bundle);
// starting the intent
startActivity(intent);
Kotlin
// creating the instance of the bundle
val bundle = Bundle()
// storing the string value in the bundle
// which is mapped to key
bundle.putString("key1", "Gfg :- Main Activity")
// creating a intent
intent = Intent(this@MainActivity, SecondActivity::class.java)
// passing a bundle to the intent
intent.putExtras(bundle)
// starting the activity by passing the intent to it.
startActivity(intent)
Java
// getting the bundle back from the android
Bundle bundle = getIntent().getExtras();
// getting the string back
String title = bundle.getString("key1", "Default");
Kotlin
// getting the bundle back from the android
val bundle = intent.extras
// performing the safety null check
var s:String? = null
// getting the string back
s = bundle!!.getString("key1", "Default"))
XML
XML
Java
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import androidx.appcompat.app.AppCompatActivity;
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);
// one button will pass the bundle and other button
// will not pass the bundle
btnPassBundles.setOnClickListener(this);
btnNoPassBundle.setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.btnPassBundles:
// creating a bundle instance
Bundle bundle = new Bundle();
// passing the data into the bundle
bundle.putString(
"key1",
"Passing Bundle From Main Activity to 2nd Activity");
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
// passing the bundle to the intent
intent.putExtras(bundle);
// starting the activity by passing the intent
// to it.
startActivity(intent);
break;
case R.id.btnNoPassBundle:
bundle = new Bundle();
bundle.putString(
"key1",
"Not passing Bundle From Main Activity");
// clearing the data stored into the bundle
bundle.clear();
// passing the intent to the second activity
intent = new Intent(MainActivity.this, SecondActivity.class);
intent.putExtras(bundle);
startActivity(intent);
break;
}
}
}
Kotlin
import android.content.Intent
import android.os.Bundle
import android.view.View
import android.widget.Button
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity(), View.OnClickListener {
var btnPassBundles: Button? = null
var btnNoPassBundle: Button? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
btnPassBundles = findViewById(R.id.btnPassBundles)
btnNoPassBundle = findViewById(R.id.btnNoPassBundle)
// one button will pass the bundle and other button
// will not pass the bundle
btnPassBundles?.setOnClickListener(this)
btnNoPassBundle?.setOnClickListener(this)
}
override fun onClick(view: View) {
when (view.id) {
R.id.btnPassBundles -> {
// creating the bundle instance
val bundle = Bundle()
// passing the data into the bundle
bundle.putString("key1", "Passing Bundle From Main Activity to 2nd Activity")
val intent = Intent(this@MainActivity, SecondActivity::class.java)
intent.putExtras(bundle)
startActivity(intent)
}
R.id.btnNoPassBundle -> {
val bundle = Bundle()
bundle.putString("key1", "Not passing Bundle From Main Activity")
// clearing the bundle
bundle.clear()
// passing the intent to the second activity
intent = Intent(this@MainActivity, SecondActivity::class.java)
// passing the bundle into the intent
intent.putExtras(bundle)
startActivity(intent)
}
}
}
}
Java
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
public class SecondActivity extends AppCompatActivity {
TextView txtString;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
txtString = findViewById(R.id.txtString);
// getting the bundle from the intent
Bundle bundle = getIntent().getExtras();
// setting the text in the textview
txtString.setText(bundle.getString("key1", "No value from the MainActivity"));
}
}
Kotlin
import android.os.Bundle
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
class SecondActivity : AppCompatActivity() {
var txtString: TextView? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_second)
txtString = findViewById(R.id.txtString)
txtBoolean = findViewById(R.id.txtBoolean)
// getting the bundle from the intent
val bundle = intent.extras
// setting the text in the textview
txtString?.setText(bundle!!.getString("key1", "No value from MainActivity :("))
}
}
现在创建另一个名为SecondActivity的空活动。现在,要检索存储在Bundle中的数据,请在SecondActivity中编写以下代码。
Java
// getting the bundle back from the android
Bundle bundle = getIntent().getExtras();
// getting the string back
String title = bundle.getString("key1", "Default");
科特林
// getting the bundle back from the android
val bundle = intent.extras
// performing the safety null check
var s:String? = null
// getting the string back
s = bundle!!.getString("key1", "Default"))
或者,如果您也不想使用默认值,则可以执行此操作,但请记住它会给出异常。
For eg: boolean b = bundle.getBoolean(“pass the key here”);
如果不存在与该键对应的映射,则可能导致NullPointerException。因此,建议为Bundle添加默认值。
例子
步骤1:创建一个新项目
- 单击文件,然后单击新建=>新建项目。
- 选择清空活动
- 选择语言作为Java/科特林
- 根据需要选择最小的SDK。
步骤2:使用activity_main.xml文件
现在,在应用程序中添加两个Button,一个按钮将传递存储在bundle中的数据,另一个按钮将传递空的bundle,即使用bundle.clear()清除捆绑,然后将Bundle传递给Intent。下面给出了activity_main.xml文件的完整代码。在这里,可以看到第一个按钮用于传递非空包,而第二个按钮用于传递空包。
XML格式
步骤3:创建另一个活动并将其命名为SecondActivity
现在创建另一个名为第二活动的空活动。请按照下图所示的过程创建另一个活动。
步骤4:使用activity_second.xml文件
在此文件中,添加一个TextView以在SecondActivity中显示文本。
XML格式
步骤5:使用MainActivity文件
MainActivity的完整代码如下。添加注释以轻松理解代码。
Java
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import androidx.appcompat.app.AppCompatActivity;
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);
// one button will pass the bundle and other button
// will not pass the bundle
btnPassBundles.setOnClickListener(this);
btnNoPassBundle.setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.btnPassBundles:
// creating a bundle instance
Bundle bundle = new Bundle();
// passing the data into the bundle
bundle.putString(
"key1",
"Passing Bundle From Main Activity to 2nd Activity");
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
// passing the bundle to the intent
intent.putExtras(bundle);
// starting the activity by passing the intent
// to it.
startActivity(intent);
break;
case R.id.btnNoPassBundle:
bundle = new Bundle();
bundle.putString(
"key1",
"Not passing Bundle From Main Activity");
// clearing the data stored into the bundle
bundle.clear();
// passing the intent to the second activity
intent = new Intent(MainActivity.this, SecondActivity.class);
intent.putExtras(bundle);
startActivity(intent);
break;
}
}
}
科特林
import android.content.Intent
import android.os.Bundle
import android.view.View
import android.widget.Button
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity(), View.OnClickListener {
var btnPassBundles: Button? = null
var btnNoPassBundle: Button? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
btnPassBundles = findViewById(R.id.btnPassBundles)
btnNoPassBundle = findViewById(R.id.btnNoPassBundle)
// one button will pass the bundle and other button
// will not pass the bundle
btnPassBundles?.setOnClickListener(this)
btnNoPassBundle?.setOnClickListener(this)
}
override fun onClick(view: View) {
when (view.id) {
R.id.btnPassBundles -> {
// creating the bundle instance
val bundle = Bundle()
// passing the data into the bundle
bundle.putString("key1", "Passing Bundle From Main Activity to 2nd Activity")
val intent = Intent(this@MainActivity, SecondActivity::class.java)
intent.putExtras(bundle)
startActivity(intent)
}
R.id.btnNoPassBundle -> {
val bundle = Bundle()
bundle.putString("key1", "Not passing Bundle From Main Activity")
// clearing the bundle
bundle.clear()
// passing the intent to the second activity
intent = Intent(this@MainActivity, SecondActivity::class.java)
// passing the bundle into the intent
intent.putExtras(bundle)
startActivity(intent)
}
}
}
}
步骤6:使用SecondActivity文件
SecondActivity的完整代码如下。添加注释以轻松理解代码。
Java
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
public class SecondActivity extends AppCompatActivity {
TextView txtString;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
txtString = findViewById(R.id.txtString);
// getting the bundle from the intent
Bundle bundle = getIntent().getExtras();
// setting the text in the textview
txtString.setText(bundle.getString("key1", "No value from the MainActivity"));
}
}
科特林
import android.os.Bundle
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
class SecondActivity : AppCompatActivity() {
var txtString: TextView? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_second)
txtString = findViewById(R.id.txtString)
txtBoolean = findViewById(R.id.txtBoolean)
// getting the bundle from the intent
val bundle = intent.extras
// setting the text in the textview
txtString?.setText(bundle!!.getString("key1", "No value from MainActivity :("))
}
}