Generic method to get Value from Bundle - Android Android OS

Android examples for Android OS:Bundle Get

Description

Generic method to get Value from Bundle

Demo Code


import java.util.Set;
import android.os.Bundle;
import android.text.TextUtils;

public class Main{
    public static final String TAG = "BundleUtil";
    @SuppressWarnings("unchecked")
    public static <T> T getValue(Bundle bundle, String key, T df) {
        if (bundle == null || TextUtils.isEmpty(key)) {
            return df;
        }/*from   w  w  w  . j a v a2 s .c  om*/

        if (df == null) {
            return df;
        }

        if (!bundle.containsKey(key)) {
            return df;
        }
        T value = df;
        Object obj = bundle.get(key);
        if (obj != null
                && value.getClass().isAssignableFrom(obj.getClass())) {
            value = (T) obj;
        } else {
            LogUtil.w(TAG, "[key] " + key + " [value] " + obj);
        }
        return value;
    }
}

Related Tutorials