get generic Value from Bundle with default value - Android android.os

Android examples for android.os:Bundle

Description

get generic Value from Bundle with default value

Demo Code

import android.os.Bundle;
import android.text.TextUtils;

public class Main {


  @SuppressWarnings("unchecked")
  public static <T> T getValue(Bundle bundle, String key, T df) {
    if (bundle == null || key== null || key.equals("")) {
      return df;//w w  w. java  2s  .com
    }

    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;
    }
    return value;
  }

}

Related Tutorials