Android学习之路(10) Bundle

news2024/9/24 21:26:04

Bundle的概念理解

Bundle经常出现在以下场合:

  1. Activity状态数据的保存与恢复涉及到的两个回调:void onSaveInstanceState (Bundle outState)void onCreate (Bundle savedInstanceState)
  2. Fragment的setArguments方法:void setArguments (Bundle args)
  3. 消息机制中的Message的setData方法:void setData (Bundle data)
  4. 其他场景不再列举

Bundle从字面上解释为“一捆、一批、一包”,结合上述几个应用场合,可以知道Bundle是用来传递数据的,我们暂将Bundle理解为Android中用来传递数据的一个容器。官方文档对Bundle的说明如下:

Bundle实现了Parcelable接口,所以他可以方便的在不同进程间传输,这里要注意我们传输的数据必须能够被序列化。

bundle的作用主要时用于传递数据;它所保存的数据是以key-value(键值对)的形式存在的,也就是说bundle是保存数据的容器,内部使用了ArrayMap去存储数据,也提供了很多get,put方法。

bundle传递的数据包括:string、int、boolean、byte、float、long、double等基本类型或它们对应的数组,也可以是对象或对象数组。当bundle传递的是对象或对象数组时,必须实现Serialiable或Parcelable接口。

Bundle源码分析

Bundle操作的基本数据类型如下表所示,它们都继承自BaseBundle (From class android.os.BaseBundle )

返回类型函数函数说明
voidclear()Removes all elements from the mapping of this Bundle.
booleancontainsKey(String key)Returns true if the given key is contained in the mapping of this Bundle.
objectget(String key)Returns the entry with the given key as an object.
booleangetBoolean(String key, boolean defaultValue)Returns the value associated with the given key, or defaultValue if no mapping of the desired type exists for the given key.
booleangetBoolean(String key)Returns the value associated with the given key, or false if no mapping of the desired type exists for the given key.
boolean[]getBooleanArray(String key)Returns the value associated with the given key, or null if no mapping of the desired type exists for the given key or a null value is explicitly associated with the key.
doublegetDouble(String key, double defaultValue)Returns the value associated with the given key, or defaultValue if no mapping of the desired type exists for the given key.
doublegetDouble(String key)Returns the value associated with the given key, or 0.0 if no mapping of the desired type exists for the given key.
double[]getDoubleArray(String key)Returns the value associated with the given key, or null if no mapping of the desired type exists for the given key or a null value is explicitly associated with the key.
intgetInt(String key)Returns the value associated with the given key, or 0 if no mapping of the desired type exists for the given key.
intgetInt(String key, int defaultValue)Returns the value associated with the given key, or defaultValue if no mapping of the desired type exists for the given key.
int[]getIntArray(String key)Returns the value associated with the given key, or null if no mapping of the desired type exists for the given key or a null value is explicitly associated with the key.
longgetLong(String key)Returns the value associated with the given key, or 0L if no mapping of the desired type exists for the given key.
longgetLong(String key, long defaultValue)Returns the value associated with the given key, or defaultValue if no mapping of the desired type exists for the given key.
long[]getLongArray(String key)Returns the value associated with the given key, or null if no mapping of the desired type exists for the given key or a null value is explicitly associated with the key.
StringgetString(String key)Returns the value associated with the given key, or null if no mapping of the desired type exists for the given key or a null value is explicitly associated with the key.
StringgetString(String key, String defaultValue)Returns the value associated with the given key, or defaultValue if no mapping of the desired type exists for the given key or if a null value is explicitly associated with the given key.
String[]getStringArray(String key)Returns the value associated with the given key, or null if no mapping of the desired type exists for the given key or a null value is explicitly associated with the key.
booleanisEmpty()Returns true if the mapping of this Bundle is empty, false otherwise.
SetkeySet()Returns a Set containing the Strings used as keys in this Bundle.
voidputAll(PersistableBundle bundle)Inserts all mappings from the given PersistableBundle into this BaseBundle.
voidputBoolean(String key, boolean value)Inserts a Boolean value into the mapping of this Bundle, replacing any existing value for the given key.
voidputBooleanArray(String key, boolean[] value)Inserts a boolean array value into the mapping of this Bundle, replacing any existing value for the given key.
voidputDouble(String key, double value)Inserts a double value into the mapping of this Bundle, replacing any existing value for the given key.
voidputDoubleArray(String key, double[] value)Inserts a double array value into the mapping of this Bundle, replacing any existing value for the given key.
voidputInt(String key, int value)Inserts an int value into the mapping of this Bundle, replacing any existing value for the given key.
voidputIntArray(String key, int[] value)Inserts an int array value into the mapping of this Bundle, replacing any existing value for the given key.
voidputLong(String key, long value)Inserts a long value into the mapping of this Bundle, replacing any existing value for the given key.
voidputLongArray(String key, long[] value)Inserts a long array value into the mapping of this Bundle, replacing any existing value for the given key.
voidputString(String key, String value)Inserts a String value into the mapping of this Bundle, replacing any existing value for the given key.
voidputStringArray(String key, String[] value)Inserts a String array value into the mapping of this Bundle, replacing any existing value for the given key.
voidremove(String key)Removes any entry with the given key from the mapping of this Bundle.
intsize()Returns the number of mappings contained in this Bundle.

首先看它的声明

public final class Bundle extends BaseBundle implements Cloneable, Parcelable

第一,它使用final修饰,所以不可以被继承

第二,它实现了两个接口,cloneable和Parcelable,这就意味着他必须实现以下方法:

public Object clone()
public int describeContents()
public void writeToParcel(Parcel parcel, int flags)
public void readFromParcel(Parcel parcel)
public static final Parcelable.Creator<Bundle> CREATOR = new Parcelable.Creator<Bundle>()

再看他的内存结构:

ArrayMap<String, Object> mMap = null;

使用的是ArrayMap,这个集合类存储的也是键值对,但是与Hashmap不同的是,hashmap采用的是“数组+链表”的方式存储,而ArrayMap中使用的是两个数组进行存储,一个数组存储key,一个数组存储value,内部的增删改查都将会使用二分查找来进行,这个和SparseArray差不多,只不过SparseArray的key值只能是int型的,而Arraymap可以是map型,所以在数据量不大的情况下可以使用这两个集合代替hashmap去优化性能。

我们知道Bundle其实就是一个容器,内部使用了ArrayMap去存储数据,那么就必然会提供get,put方法,由于Bundle支持的数据类型太多,这里我们就看一个布尔类型的,其他类型的方式都差不多。

public boolean getBoolean(String key, boolean defaultValue) {
    unparcel();
    Object o = mMap.get(key);
    if (o == null) {
        return defaultValue;
    }
    try {
        return (Boolean) o;
    } catch (ClassCastException e) {
        typeWarning(key, o, "Boolean", defaultValue, e);
        return defaultValue;
    }
}

数据读取的逻辑很简单,就是通过key从ArrayMap里读出保存的数据,并转换成对应的类型返回,当没找到数据或发生类型转换异常时返回缺省值。

public void putBoolean(@Nullable String key, boolean value) {
    unparcel();
    mMap.put(key, value);
}

这里出现了一个unparcel()方法

/* package */ 
void unparcel() {
    synchronized (this) {
        final Parcel parcelledData = mParcelledData;
        if (parcelledData == null) {
            if (DEBUG) Log.d(TAG, "unparcel "
                    + Integer.toHexString(System.identityHashCode(this))
                    + ": no parcelled data");
            return;
        }

        if (LOG_DEFUSABLE && sShouldDefuse && (mFlags & FLAG_DEFUSABLE) == 0) {
            Slog.wtf(TAG, "Attempting to unparcel a Bundle while in transit; this may "
                    + "clobber all data inside!", new Throwable());
        }

        if (isEmptyParcel()) {
            if (DEBUG) Log.d(TAG, "unparcel "
                    + Integer.toHexString(System.identityHashCode(this)) + ": empty");
            if (mMap == null) {
                mMap = new ArrayMap<>(1);
            } else {
                mMap.erase();
            }
            mParcelledData = null;
            return;
        }

        int N = parcelledData.readInt();
        if (DEBUG) Log.d(TAG, "unparcel " + Integer.toHexString(System.identityHashCode(this))
                + ": reading " + N + " maps");
        if (N < 0) {
            return;
        }
        ArrayMap<String, Object> map = mMap;
        if (map == null) {
            map = new ArrayMap<>(N);
        } else {
            map.erase();
            map.ensureCapacity(N);
        }
        try {
            parcelledData.readArrayMapInternal(map, N, mClassLoader);
        } catch (BadParcelableException e) {
            if (sShouldDefuse) {
                Log.w(TAG, "Failed to parse Bundle, but defusing quietly", e);
                map.erase();
            } else {
                throw e;
            }
        } finally {
            mMap = map;
            parcelledData.recycle();
            mParcelledData = null;
        }
        if (DEBUG) Log.d(TAG, "unparcel " + Integer.toHexString(System.identityHashCode(this))
                + " final map: " + mMap);
    }
}

先来看下BaseBundle中mParcelledData的定义:

Parcel mParcelledData = null;

在大部分情况下mParcelledData都是null,因此unparcel()直接返回。当使用构造函数public Bundle(Bundle b)创建Bundle时,会给mParcelledData赋值;

void copyInternal(BaseBundle from, boolean deep) {
    synchronized (from) {
        if (from.mParcelledData != null) {
            if (from.isEmptyParcel()) {
                mParcelledData = NoImagePreloadHolder.EMPTY_PARCEL;
            } else {
                mParcelledData = Parcel.obtain();
                mParcelledData.appendFrom(from.mParcelledData, 0,
                        from.mParcelledData.dataSize());
                mParcelledData.setDataPosition(0);
            }
        } else {
            mParcelledData = null;
        }

        if (from.mMap != null) {
            if (!deep) {
                mMap = new ArrayMap<>(from.mMap);
            } else {
                final ArrayMap<String, Object> fromMap = from.mMap;
                final int N = fromMap.size();
                mMap = new ArrayMap<>(N);
                for (int i = 0; i < N; i++) {
                    mMap.append(fromMap.keyAt(i), deepCopyValue(fromMap.valueAt(i)));
                }
            }
        } else {
            mMap = null;
        }

        mClassLoader = from.mClassLoader;
    }
}

从上述代码片段可以知道mParcelledData的取值有3种情况:

  • mParcelledData = EMPTY_PARCEL
  • mParcelledData = Parcel.obtain()
  • mParcelledData = null

在unparcel()方法中就对上述几种情况做了不同的处理,当mParcelledData为null时,直接返回;当mParcelledData为EMPTY_PARCEL时,会创建一个容量为1的ArrayMap对象;当mParcelledData为Parcel.obtain()时,则会将里面的数据读出,并创建一个ArrayMap,并将数据存储到ArrayMap对象里面,同时将mParcelledData回收并置为null;

Parcelable接口分析

在Android中,Parcelable接口是用于实现对象序列化和反序列化的一种机制。它允许我们将自定义的Java对象转换成一个可传输的二进制数据流,以便在不同组件之间传递数据。通常在Activity之间传递复杂的自定义对象时,使用Parcelable接口比使用Java的Serializable接口更高效。

Parcelable接口的工作原理是通过将对象的数据拆分成原始数据类型,并在写入和读取时进行序列化和反序列化。这样可以避免使用Java的反射机制,提高了性能。

要实现Parcelable接口,首先需要让自定义的Java类实现Parcelable接口,并实现以下几个方法:

  1. writeToParcel(Parcel parcel, int flags): 将对象的数据写入Parcel对象,以进行序列化。在这个方法中,需要将对象的各个字段写入Parcel对象。
  2. createFromParcel(Parcel parcel): 从Parcel对象中读取数据,以进行反序列化。在这个方法中,需要读取Parcel中的数据,并将其设置为对象的各个字段。
  3. newArray(int size): 创建一个指定大小的对象数组,通常用于反序列化的过程。

接着,需要在类中添加一个静态的Parcelable.Creator对象,用于创建和反序列化对象。这个对象需要实现Parcelable.Creator接口,并实现以下方法:

  1. createFromParcel(Parcel parcel): 根据Parcel对象创建并返回对象实例。
  2. newArray(int size): 创建一个指定大小的对象数组。

最后,在类中添加一个public static final的Parcelable.Creator对象,以供系统使用。

下面是一个简单的示例,演示了如何在Android中实现Parcelable接口:

import android.os.Parcel;
import android.os.Parcelable;

public class Student implements Parcelable {
    private String name;
    private int age;

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    protected Student(Parcel in) {
        name = in.readString();
        age = in.readInt();
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(name);
        dest.writeInt(age);
    }

    @Override
    public int describeContents() {
        return 0;
    }

    public static final Creator<Student> CREATOR = new Creator<Student>() {
        @Override
        public Student createFromParcel(Parcel in) {
            return new Student(in);
        }

        @Override
        public Student[] newArray(int size) {
            return new Student[size];
        }
    };
}

在这个例子中,我们创建了一个名为Student的类,实现了Parcelable接口。在writeToParcel方法中,我们将Student对象的name和age字段写入Parcel对象。在createFromParcel方法中,我们从Parcel对象中读取name和age字段,并创建一个新的Student对象。

通过实现Parcelable接口,我们可以在不同的Android组件之间传递Student对象,而不需要进行繁琐的序列化和反序列化操作。同时,Parcelable接口也比Serializable接口更高效,适用于在性能要求较高的场景下使用。

代码举例说明

当使用Parcelable接口时,我们可以将自定义的Java类对象传递给Android组件,例如Activity之间的传递。下面是一个简单的示例,展示如何实现Parcelable接口和在Activity之间传递自定义对象:

首先,创建一个名为Student的Java类,该类包含一些基本的字段和方法:

import android.os.Parcel;
import android.os.Parcelable;

public class Student implements Parcelable {
    private String name;
    private int age;

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    protected Student(Parcel in) {
        name = in.readString();
        age = in.readInt();
    }

    public String getName() {
        return name;
    }

    public int getAge() {
        return age;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(name);
        dest.writeInt(age);
    }

    @Override
    public int describeContents() {
        return 0;
    }

    public static final Creator<Student> CREATOR = new Creator<Student>() {
        @Override
        public Student createFromParcel(Parcel in) {
            return new Student(in);
        }

        @Override
        public Student[] newArray(int size) {
            return new Student[size];
        }
    };
}

接下来,在发送方的Activity中,我们创建一个Student对象并使用Intent将其传递给接收方的Activity:

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

public class SenderActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sender);

        // 创建一个Student对象
        Student student = new Student("Alice", 20);

        // 使用Intent传递Student对象给ReceiverActivity
        Intent intent = new Intent(this, ReceiverActivity.class);
        intent.putExtra("student", student);
        startActivity(intent);
    }
}

最后,在接收方的Activity中,我们从Intent中获取传递过来的Student对象:

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

public class ReceiverActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_receiver);

        // 从Intent中获取传递过来的Student对象
        Student student = getIntent().getParcelableExtra("student");

        if (student != null) {
            // 使用Student对象的数据
            String name = student.getName();
            int age = student.getAge();

            // 在这里进行相关操作,例如显示学生信息
        }
    }
}

通过实现Parcelable接口,我们可以轻松地在Activity之间传递自定义的Student对象,而不需要进行额外的序列化和反序列化操作。在接收方的Activity中,我们可以获取传递过来的Student对象,并使用其中的数据进行相应的处理。这样,我们就实现了自定义对象的传递和使用。

Bundle实战

在Activity to Activity传递数据时使用Bundle

① 当传递简单数据时

新建一个MainActivity,对应的布局文件比较简单,就是一个Button,点击这个按钮后,程序跳转到SecondActivity,并将传递的数据在SecondActivity的TextView中显示出来。这样,就使用Bundle实现了数据在Activity之间的传递。

package com.example.bundletest;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {
    //声明控件对象
    private Button mButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //获取控件的对象
        mButton = findViewById(R.id.button);

        //为Button绑定监听器
        mButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                /**
                 * 存入数据
                 */
                //实例化一个Bundle
                Bundle bundle = new Bundle();
                Intent intent = new Intent(MainActivity.this, SecondActivity.class);

                //设置数据
                String name = "Trump";
                int num = 123;

                //把数据放入到Bundle容器中
                bundle.putString("Name", name);
                bundle.putInt("Num", num);

                //把Bundle容器中的数据放到Intent中
                intent.putExtra("Message", bundle);

                //启动该Intent,实现Activity的跳转
                startActivity(intent);
            }
        });
    }
}

新建一个SecondActivity,用于显示传递的数据。对应的布局文件也很简单,就是一个TextView。

package com.example.bundletest;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;

public class SecondActivity extends AppCompatActivity {
    //声明控件对象
    private TextView textView;

    @SuppressLint("SetTextI18n")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);

        //获取控件的对象
        textView = findViewById(R.id.text_view);

        /**
         *读取数据
         */
        Intent intent = getIntent();
        //从Intent中取出Bundle
        Bundle bundle = intent.getBundleExtra("Message");

        //获取数据
        assert bundle != null;
        String name = bundle.getString("Name");
        int num = bundle.getInt("Num");

        //显示数据
        textView.setText(name + "\n" + num);
    }
}

运行程序后,结果如下图所示:

点击Button,结果如下图所示:

② 当传递的参数很多,或者传递一个类的对象时

新建一个JavaBean,将这个类命名为FunPerson,并实现Serializable接口。

package com.example.bundletest;

import java.io.Serializable;

public class FunPerson implements Serializable {
    //创建实例变量
    private String name;
    private int num;

    public void setName(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setNum(int num) {
        this.num = num;
    }

    public int getNum() {
        return num;
    }
}

修改MainActivity中的代码:

public class MainActivity extends AppCompatActivity {
    //声明控件对象
    private Button mButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //获取控件的对象
        mButton = findViewById(R.id.button);

        //为Button绑定监听器
        mButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                /**
                 * 存入数据
                 */
                FunPerson person = new FunPerson();
                //设置数据
                String name = "Trump is fun";
                int num = 12345;
                person.setName("name");
                person.setNum(num);

                //实例化一个Bundle
                Bundle bundle = new Bundle();
                //把FunPerson数据放入到Bundle容器中
                bundle.putSerializable("Person", person);

                Intent intent = new Intent(MainActivity.this, SecondActivity.class);
                //把Bundle容器中的数据放到Intent中
                intent.putExtras(bundle);

                //启动该Intent,实现Activity的跳转
                startActivity(intent);
            }
        });
    }
}

修改SecondActivity中的代码:

public class SecondActivity extends AppCompatActivity {
    //声明控件对象
    private TextView textView;

    @SuppressLint("SetTextI18n")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);

        //获取控件的对象
        textView = findViewById(R.id.text_view);

        /**
         *读取数据
         */
        Intent intent = getIntent();
        //从Intent中取出Bundle
        Bundle bundle = intent.getExtras();

        //获取FunPerson里的数据数据
        assert bundle != null;
        FunPerson person = (FunPerson)bundle.getSerializable("Person");

        assert person != null;
        String name = person.getName();
        int num = person.getNum();

        //显示数据
        textView.setText(name + "\n" + num);
    }
}

看下运行后的结果:

在Activity to Fragment传递数据时使用Bundle

Activity重新创建时,会重新构建它所管理的Fragment,原先的Fragment的字段值将会全部丢失,但是通过Fragment.setArguments(Bundle bundle)方法设置的bundle会保留下来。所以尽量使用Fragment.setArguments(Bundle bundle)方式来传递参数。

有两种实现方案。

① 方法一:使用Fragment的静态方法newInstance()来传递数据

新建MainActivity:

public class MainActivity extends AppCompatActivity {

    private Button mButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mButton = findViewById(R.id.button);

        mButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //发送数据
                BlankFragment blankFragment =  BlankFragment.newInstance("Message_1 To Fragment", "Message_2 To Fragment");
                FragmentManager fragmentManager = getSupportFragmentManager();
                FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                //FrameLayout用于动态更新fragment
                fragmentTransaction.replace(R.id.frame_layout, blankFragment);
                fragmentTransaction.commit();
            }
        });
    }
}

MainActivity的布局文件如下:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginTop="8dp"
        android:text="SendMsg"
        android:textAllCaps="false"
        app:layout_constraintBottom_toTopOf="@+id/guide_line"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <androidx.constraintlayout.widget.Guideline
        android:id="@+id/guide_line"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintGuide_begin="255dp" />
    
    <FrameLayout
        android:id="@+id/frame_layout"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@+id/guide_line"
        app:layout_constraintVertical_bias="0.0" >
    </FrameLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

新建一个Fragment:

public class BlankFragment extends Fragment {
    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;


    public static BlankFragment newInstance(String param1, String param2) {
        BlankFragment fragment = new BlankFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_blank, container, false);

        TextView textView = view.findViewById(R.id.text_view);
        //Fragment获取数据
        Bundle bundle = getArguments();
        String message = null;
        if (bundle != null) {
            message = bundle.getString(ARG_PARAM1);
        }
        textView.setText(message);
        return view;
    }
}

BlankFragment的布局文件比较简单,就是一个显示用的TextView。

运行程序,点击Button,结果如下图bundle4所示:

② 方法二:

修改MainActivity的代码:

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button mButton = findViewById(R.id.button);
        mButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //发送数据
                ToFragment fragment = new ToFragment();
                //新建一个Bundle实例
                Bundle bundle = new Bundle();
                bundle.putString("data", "From Activity To Fragment");
                //将数据传递到Fragment
                fragment.setArguments(bundle);
                FragmentManager fragmentManager = getSupportFragmentManager();
                FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                //FrameLayout用于动态更新fragment
                fragmentTransaction.replace(R.id.frame_layout, fragment);
                fragmentTransaction.commit();
            }
        });
    }
}

新建一个碎片ToFragment,简单起见,就不给新建的碎片弄一个布局文件,直接使用BlankFragment的布局文件,节省时间:

public class ToFragment extends Fragment {
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        //简单起见,此处直接使用BlankFragment的布局文件
        View view = inflater.inflate(R.layout.fragment_blank, container, false);

        TextView textView = view.findViewById(R.id.text_view);
        //得到从Activity传来的数据
        Bundle bundle = this.getArguments();
        String message = null;
        if (bundle != null) {
            message = bundle.getString("data");
        }
        textView.setText(message);
        return view;
    }
}

运行程序后结果如下所示:

3. 在消息机制的Message中使用setData()传递数据时用到Bundle

这个栗子的思路也很简单,点击屏幕,给Activity发送一个Message,传递两个参数,并通过Toast显示出来,最后finish()掉这个Activity。

新建一个Activity:

public class MainActivity extends AppCompatActivity {

    final static int FLAG = 1;

    @SuppressLint("HandlerLeak")
    public Handler mHandler = new Handler() {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case FLAG:
                    //获取Message传递过来的数据
                    String data1 = msg.getData().getString("text1");
                    String data2 = msg.getData().getString("text2");
                    init(data1, data2);
                    break;
                default:
                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(new MyView(this, this));
    }

    public void init(String str1, String str2) {
        //将获取的数据Toast出来
        Toast.makeText(MainActivity.this, str1 + '\n' + str2, Toast.LENGTH_SHORT).show();
        finish();
    }
}

在建一个Java类:

@SuppressLint("ViewConstructor")
public class MyView extends View {
    private MainActivity activity;

    public MyView(Context context, MainActivity activity) {
        super(context);
        this.activity = activity;
    }

    @SuppressLint("ClickableViewAccessibility")
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        int x = (int)event.getX();
        int y = (int)event.getY();
        Rect rect = new Rect(0, 0, 320, 480);
        if (rect.contains(x, y)) {
            Message message = new Message();
            message.what = MainActivity.FLAG;
            //新建Bundle的实例
            Bundle bundle = new Bundle();
            //往Bundle中传入数据
            bundle.putString("text1", "Trump want to ban TimTok");
            bundle.putString("text2", "Make America great again");
            //message利用bundle传递数据
            message.setData(bundle);
            //用activity中的handler发送消息
            activity.mHandler.sendMessage(message);
        }
        return super.onTouchEvent(event);
    }
}

运行程序,得到如下结果:

点击屏幕指定区域,得到如下结果:

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/935410.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

无可用的防病毒提供方你的设备

今天安装软件时关闭了一下windows的Defender&#xff0c;然后安装后出现下面问题 莫名奇妙我的病毒防护就不能用了 后来请教了老师才知道是安装的软件把我系统设置改了&#xff0c;以后使用一键安装软件要谨慎 解决措施&#xff1a; CMD命令&#xff0c;输入“regedit”&#…

kubernetes/k8s驱逐机制总结篇

概述 k8s的驱逐机制是指在某些场景下&#xff0c;如node节点notReady、node节点压力较大等&#xff0c;将pod从某个node节点驱逐掉&#xff0c;让pod的上层控制器重新创建出新的pod来重新调度到其他node节点。这里也将kube-scheduler的抢占调度纳入到了驱逐的讨论范围内&#…

Kotlin开发笔记:协程基础

Kotlin开发笔记&#xff1a;协程基础 导语 本章内容与书的第十五章相关&#xff0c;主要介绍与协程相关的知识。总的来说&#xff0c;本文将会介绍Kotlin中关于异步编程的内容&#xff0c;主要就是与协程有关。在Kotlin中协程是利用continuations数据结构构建的&#xff0c;用…

【洛谷】P2678 跳石头

原题链接&#xff1a;https://www.luogu.com.cn/problem/P2678 目录 1. 题目描述 2. 思路分析 3. 代码实现 1. 题目描述 2. 思路分析 二分答案。&#xff08;使用二分需要满足两个条件。一个是有界&#xff0c;一个是单调。 这题的题面&#xff1a;使得选手们在比赛过程中…

RV64函数调用流程分析

RV64函数调用流程分析 1 RV64 函数调用实例2 对应代码的分析2.1 main函数及其对应的汇编程序2.1.1 main的C代码实现2.1.2 main函数对应汇编及其分析2.1.3 执行完成之后栈的存放情况 2.2 test_fun_a函数及其对应的汇编程序2.2.1 test_fun_a函数的C实现2.2.2 test_fun_a函数对应汇…

go vet中的那些检测项

go vet 是 Go 语言自带的一个工具&#xff0c;用于分析 Go 代码中的常见错误和潜在问题。它可以检查代码中可能存在的各种问题&#xff0c;例如&#xff1a; 未使用的变量、函数或包 可疑的函数调用 错误的函数签名 程序中的竞态条件 错误的类型转换等 本文意图指令当前go vet所…

单片机IO模拟串口协议

一、前言 嵌入式硬件平台调试中常用的debug方法是看串口打印定位问题&#xff0c;但有时候会遇到单片机没有串口外设或者串口引脚被占用的情况&#xff0c;这时候也可以在代码里操作空闲的IO输出不同个数的脉冲来达到调试的效果&#xff0c;但是要用逻辑分析仪抓线逐个看波形比…

快速了解:Mybatis-Plus

一、Mybatis-Plus介绍 MyBatis-Plus&#xff08;简称 MP&#xff09;是一个 MyBatis 的增强工具&#xff0c;在 MyBatis 的基础上只做增强不做改变&#xff0c;为简化开发、提高 效率而生。 官网&#xff1a;https://mybatis.plus/ 或 https://mp.baomidou.com/ 文档地址&…

【软件测试】如何用python连接Linux服务器

1.安装paramiko库 pip install paramiko 2.使用paramiko库连接linux #导入库 import paramiko#创建一个sshclient对象 ssh paramiko.SSHClient()#允许连接不在know_host中的主机 ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())#连接主机 ssh.connect(hostname…

C++模板中的必须要引用typename

1.看一下下面这个函数 但是如果传的是vector<double>或则是list类型,queue类型等等&#xff0c;都是不可以的&#xff0c;有些人就会说了&#xff0c;用模板呗&#xff0c;现在问题就出现了&#xff1a; 为什么会出现这个错误呢&#xff1f; 其实就是编译器不知道你这个…

MySQL索引连环18问!

1. 索引是什么&#xff1f; 索引是一种特殊的文件(InnoDB数据表上的索引是表空间的一个组成部分)&#xff0c;它们包含着对数据表里所有记录的引用指针。 索引是一种数据结构。数据库索引&#xff0c;是数据库管理系统中一个排序的数据结构&#xff0c;以协助快速查询、更新数…

await Promise内部执行setTimeout定时器,提前clearTimeout,导致卡死的情况分析及解决方案

背景概述 在我们日常开发中&#xff0c;我们常常需要在某个地方暂停某个动作一段时间。这个时候&#xff0c;我们的通常做法是使用setTimeout&#xff0c;配合promise实现。也就是如下代码。 function delay(ms) {return new Promise((resolve, reject) > {setTimeout(() …

redis数据库及其常用数据结构

redis数据库 非关系型数据库 非关系型数据库是什么&#xff1f; 非关系型数据库其实是相对于关系型数据库而言的&#xff0c;关系型数据库中同种数据存储在一张表中&#xff0c;使用表将不同种类的数据进行分隔并且表中的数据如果存在主键&#xff0c;也可以根据主键确定表与…

tableau基础学习1:数据源与绘图

文章目录 读取数据常用绘图方法1. 柱状图2. 饼图3. 散点图4. 热力图 第一部分是一些较容易上手的内容&#xff0c;以及比较常见的可视化内容&#xff0c;包括&#xff1a;柱状图、饼图、散点图与热力图 读取数据 打开界面后&#xff0c;选择数据源之后就可以导入数据&#xf…

【操作记录】CLion 中引入 Gurobi 并使用 C++ 编程

文章目录 一、前言二、具体操作2.1 创建项目2.2 修改编译工具2.3 修改 CMakeLists.txt2.4 修改 main.cpp2.5 运行测试 一、前言 虽然C编程大部分人都会选择使用VS&#xff0c;但是作为 IDEA 的长期用户&#xff0c;我还是比较习惯 JetBrains 风格的编译器&#xff0c;所以就选…

Go测试之.golden 文件

Go测试中的.golden 文件是干什么用的&#xff1f;请举例说明 在Go语言中&#xff0c;.golden文件通常用于测试中的黄金文件&#xff08;golden files&#xff09;。黄金文件是在测试期间记录预期输出结果的文件。测试用例运行时&#xff0c;黄金文件用于比较实际输出与预期输出…

Autosar存储入门系列03_NVM状态机及读写存储调用逻辑

本文框架 0.前言1. NVM状态机介绍2. NVM读/写基本逻辑2.1 NVM读操作2.2 NVM写操作2.2.1 实时写2.2.2 下电写 2.3 NVM写入注意事项 0.前言 本系列是Autosar存储入门系列&#xff0c;希望能从学习者的角度把存储相关的知识点梳理一遍&#xff0c;这个过程中如果大家觉得有讲得不…

Go 第三方库引起的线上问题、如何在线线上环境进行调试定位问题以及golang开发中各种问题精华整理总结

Go 第三方库引起的线上问题、如何在线线上环境进行调试定位问题以及golang开发中各种问题精华整理总结。 01 前言 在使用 Go 语言进行 Web 开发时&#xff0c;我们往往会选择一些优秀的库来简化 HTTP 请求的处理。其中&#xff0c;go-resty 是一个被广泛使用的 HTTP 客户端。…

还在为Compose组件管理苦恼?Jetpack Compose的版本控制清单(BOM)扫除你的苦恼

还在为Compose组件管理苦恼&#xff1f;Jetpack Compose的版本控制清单&#xff08;BOM&#xff09;扫除你的苦恼 Jetpack Compose通过简化和提高效率&#xff0c;彻底改革了Android UI开发。其中一个有助于此的功能是Jetpack Compose版本控制清单&#xff08;BOM&#xff09;…

vue2 组件组成部分,组件通信,进阶语法

一、学习目标 1.组件的三大组成部分&#xff08;结构/样式/逻辑&#xff09; ​ scoped解决样式冲突/data是一个函数 2.组件通信 组件通信语法父传子子传父非父子通信&#xff08;扩展&#xff09; 4.进阶语法 v-model原理v-model应用于组件sync修饰符ref和$refs$nextTic…