create a new bundle with one key-value parameter - Android android.os

Android examples for android.os:Bundle

Description

create a new bundle with one key-value parameter

Demo Code

import android.os.Bundle;
import android.os.Parcelable;
import java.io.Serializable;
import java.security.InvalidParameterException;

public class Main{

    /**//from w ww .j  a  v a  2 s.  c o m
     * 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