Fetches object from the bundle with com.google.gson.Gson - Android android.os

Android examples for android.os:Bundle

Description

Fetches object from the bundle with com.google.gson.Gson

Demo Code

import android.os.Bundle;
import java.util.ArrayList;

public class Main{
    public static final String DEFAULT_STRING_VALUE = null;
    /**/*  ww w .  j a  v  a  2s . com*/
     * Fetches object from the bundle with {@link com.google.gson.Gson}.
     *
     * @param clazz  of object
     * @param bundle to get from
     * @param <T>    type of object
     * @return fetched object or null if not found
     */
    public static <T> T fetchFromBundle(Class<T> clazz, Bundle bundle) {
        if (bundle != null) {
            return GsonUtils.fromJson(
                    bundle.getString(clazz.getSimpleName()), clazz);
        } else {
            return null;
        }
    }
    /**
     * Retrieves string value from bundle if present.
     *
     * @param bundle to get from
     * @param key    to search by
     * @return value or {@link #DEFAULT_STRING_VALUE} if nothing found
     */
    public static String getString(Bundle bundle, String key) {
        if (bundle != null && bundle.containsKey(key)) {
            return bundle.getString(key);
        } else {
            return DEFAULT_STRING_VALUE;
        }
    }

}

Related Tutorials