create a new Bundle with one key-value parameter - Android Android OS

Android examples for Android OS:Bundle Key

Description

create a new Bundle with one key-value parameter

Demo Code


//package com.book2s;
import java.io.Serializable;
import java.security.InvalidParameterException;
import android.os.Bundle;
import android.os.Parcelable;

public class Main {
    /**/* w w  w . ja  va  2 s .  c om*/
     * create a new bundle with one key-value parameter
     * @param key
     * @param value
     * @return
     */
    public static Bundle newBundle(String key, Object value) {
        Bundle bundle = new Bundle();
        if (value instanceof Byte) {
            bundle.putByte(key, (Byte) value);
        } else if (value instanceof Short) {
            bundle.putShort(key, (Short) value);
        } else if (value instanceof Integer) {
            bundle.putInt(key, (Integer) value);
        } else if (value instanceof Long) {
            bundle.putLong(key, (Long) value);
        } else if (value instanceof Double) {
            bundle.putDouble(key, (Double) value);
        } else if (value instanceof Float) {
            bundle.putFloat(key, (Float) value);
        } else if (value instanceof Boolean) {
            bundle.putBoolean(key, (Boolean) value);
        } else if (value instanceof String) {
            bundle.putString(key, (String) value);
        } else if (value instanceof Parcelable) {
            bundle.putParcelable(key, (Parcelable) value);
        } else if (value instanceof Serializable) {
            bundle.putSerializable(key, (Serializable) value);
        } else if (value instanceof Bundle) {
            bundle.putBundle(key, (Bundle) value);
        } else {
            throw new InvalidParameterException(
                    "Value Type Error: type of value must be Byte, Short, Integer, Long, Double, Float, Boolean, String, Parcelabel, Serializable, Bundle!");
        }
        return bundle;
    }
}

Related Tutorials