mirror of
https://github.com/zhigang1992/react-native.git
synced 2026-04-11 22:40:37 +08:00
Android: Implement cancelable option for Alerts
Summary: **Motivation** In iOS you cannot dismiss alerts by clicking outside of their box, while on Android you can. This can create some inconsistency if you want to have identical behavior on both platforms. This change makes it possible for Android apps to have irremovable/required alert boxes just like in iOS. This adds an additional parameter to the Alert method. The way to use it is by providing an object with the cancelable property. The cancelable property accepts a boolean value. This utilizes the Android DialogFragment method [setCancelable](https://developer.android.com/reference/android/app/DialogFragment.html#setCancelable(boolean)) **Usage example** ```js Alert.alert( 'Alert Title', null, [ {text: 'OK', onPress: () => console.log('OK Pressed!')}, ], { cancelable: false } ); ``` **Test plan (required)** I added an additional alert to the UIExplorer project where it can be tested. I also added a part in the Dialog Module test to make sure setting canc Closes https://github.com/facebook/react-native/pull/8652 Differential Revision: D3690093 fbshipit-source-id: 4cf6cfc56f464b37ce88451acf33413393454721
This commit is contained in:
committed by
Facebook Github Bot 8
parent
44b3cfe6bf
commit
8e2906ae89
@@ -46,6 +46,7 @@ public class DialogModule extends ReactContextBaseJavaModule implements Lifecycl
|
||||
/* package */ static final String KEY_BUTTON_NEGATIVE = "buttonNegative";
|
||||
/* package */ static final String KEY_BUTTON_NEUTRAL = "buttonNeutral";
|
||||
/* package */ static final String KEY_ITEMS = "items";
|
||||
/* package */ static final String KEY_CANCELABLE = "cancelable";
|
||||
|
||||
/* package */ static final Map<String, Object> CONSTANTS = MapBuilder.<String, Object>of(
|
||||
ACTION_BUTTON_CLICKED, ACTION_BUTTON_CLICKED,
|
||||
@@ -129,6 +130,9 @@ public class DialogModule extends ReactContextBaseJavaModule implements Lifecycl
|
||||
if (isUsingSupportLibrary()) {
|
||||
SupportAlertFragment alertFragment = new SupportAlertFragment(actionListener, arguments);
|
||||
if (isInForeground) {
|
||||
if (arguments.containsKey(KEY_CANCELABLE)) {
|
||||
alertFragment.setCancelable(arguments.getBoolean(KEY_CANCELABLE));
|
||||
}
|
||||
alertFragment.show(mSupportFragmentManager, FRAGMENT_TAG);
|
||||
} else {
|
||||
mFragmentToShow = alertFragment;
|
||||
@@ -136,6 +140,9 @@ public class DialogModule extends ReactContextBaseJavaModule implements Lifecycl
|
||||
} else {
|
||||
AlertFragment alertFragment = new AlertFragment(actionListener, arguments);
|
||||
if (isInForeground) {
|
||||
if (arguments.containsKey(KEY_CANCELABLE)) {
|
||||
alertFragment.setCancelable(arguments.getBoolean(KEY_CANCELABLE));
|
||||
}
|
||||
alertFragment.show(mFragmentManager, FRAGMENT_TAG);
|
||||
} else {
|
||||
mFragmentToShow = alertFragment;
|
||||
@@ -241,6 +248,9 @@ public class DialogModule extends ReactContextBaseJavaModule implements Lifecycl
|
||||
}
|
||||
args.putCharSequenceArray(AlertFragment.ARG_ITEMS, itemsArray);
|
||||
}
|
||||
if (options.hasKey(KEY_CANCELABLE)) {
|
||||
args.putBoolean(KEY_CANCELABLE, options.getBoolean(KEY_CANCELABLE));
|
||||
}
|
||||
|
||||
fragmentManagerHelper.showNewAlert(mIsInForeground, args, actionCallback);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user