add ability to save/access Activity instanceState from/to JS

Differential Revision: D2591821

fb-gh-sync-id: c9c28b653611e65ab5b6e6cc8d34771bf8589055
This commit is contained in:
Aaron Chiu 2015-10-28 12:34:48 -07:00 committed by facebook-github-bot-7
parent a2d58203b8
commit 28b66be7b0
1 changed files with 50 additions and 0 deletions

View File

@ -9,6 +9,8 @@
package com.facebook.react.bridge;
import javax.annotation.Nullable;
import android.os.Bundle;
public class Arguments {
@ -139,4 +141,52 @@ public class Arguments {
}
return map;
}
/**
* Covert a {@link WritableMap} to a {@link Bundle}.
* @param readableMap the {@link WritableMap} to convert.
* @return the converted {@link Bundle}.
*/
@Nullable
public static Bundle toBundle(@Nullable ReadableMap readableMap) {
if (readableMap == null) {
return null;
}
ReadableMapKeySetIterator iterator = readableMap.keySetIterator();
if (!iterator.hasNextKey()) {
return null;
}
Bundle bundle = new Bundle();
while (iterator.hasNextKey()) {
String key = iterator.nextKey();
ReadableType readableType = readableMap.getType(key);
switch (readableType) {
case Null:
bundle.putString(key, null);
break;
case Boolean:
bundle.putBoolean(key, readableMap.getBoolean(key));
break;
case Number:
// Can be int or double.
bundle.putDouble(key, readableMap.getDouble(key));
break;
case String:
bundle.putString(key, readableMap.getString(key));
break;
case Map:
bundle.putBundle(key, toBundle(readableMap.getMap(key)));
break;
case Array:
// TODO t8873322
throw new UnsupportedOperationException("Arrays aren't supported yet.");
default:
throw new IllegalArgumentException("Could not convert object with key: " + key + ".");
}
}
return bundle;
}
}