dump Bundle to string recursively - Android android.os

Android examples for android.os:Bundle

Description

dump Bundle to string recursively

Demo Code

import android.os.Bundle;

public class Main {

  public static String dump(Bundle bundle) {
    if (bundle == null) {
      return "null";
    }//from  w  w w.  jav  a2s.  c  o m

    final StringBuilder builder = new StringBuilder("Bundle{");

    boolean isFirst = true;
    Object val;

    for (String key : bundle.keySet()) {
      if (isFirst) {
        isFirst = false;
      } else {
        builder.append(", ");
      }
      builder.append(key).append(": ");
      val = bundle.get(key);
      if (val instanceof Bundle) {
        builder.append(dump((Bundle) val));
      } else {
        builder.append(String.valueOf(val));
      }
    }
    builder.append("}");
    return builder.toString();
  }

}

Related Tutorials