The library provides double back press functionality, i.e., actions taken upon clicking of back button twice in short (custom predefined) interval of time.
- Action to be performed upon double click of back button
- Action to be performed upon single click of back button, before the double click occurs
- Standard responses (templates) after back button click
To run the Examples, build and execute the example module.
2. ToastDisplay + Exit Activity
3. SnackbarDisplay + Default back press behaviour
Add the dependency to your module's build.gradle
:
dependencies {
implementation 'com.github.kaushikthedeveloper:double-back-press:0.0.1'
}
-
- Constructor
- Methods
- Behaviour modifiers
-
- constructor
-
- constructor
-
DoubleBackPress doubleBackPress = new DoubleBackPress() .withDoublePressDuration(...) //required .withDoubleBackPressAction(...) //required .withFirstBackPressAction(...); //optional
or
DoubleBackPress doubleBackPress = new DoubleBackPress(doublePressDuration, doubleBackPressAction) //required .withFirstBackPressAction(...); //optional
or
DoubleBackPress doubleBackPress = new DoubleBackPress(); doubleBackPress.set...(...); //setter methods
Assign the behaviour of the DoubleBackPress as the desired behaviour upon back button click.
@Override public void onBackPressed() { doubleBackPress.onBackPressed(); }
-
Setting the environment using
with...
methods for the DoubleBackPress constructor.-
Set the duration within which the second back press needs to occur to be considered a Double Back Press.
REQUIRED
.withDoublePressDuration(int doublePressDuration)
Parameters :
doublePressDuration
: int <milli seconds> => timeout period, within which the back press should occur again to be counted as a DoubleBackPress
Returns :
DoubleBackPress
Exceptions possible later if not called :
RequirementsNotMetException
: if thedoublePressDuration
is not set
-
Set the action to be performed after the DoubleBackPress occurs.
REQUIRED
.withDoubleBackPressAction(DoubleBackPressAction doubleBackPressAction)
Parameters :
doubleBackPressAction
: DoubleBackPressAction => The action that should be performed after DoubleBackPress
Returns :
DoubleBackPress
Exceptions possible later if not called :
RequirementsNotMetException
: if thedoubleBackPressAction
is not set
-
Set the action to be performed after the first back press occurs, before the second back press.
OPTIONAL
.withFirstBackPressAction(FirstBackPressAction firstBackPressAction)
Parameters :
firstBackPressAction
: FirstBackPressAction => The action that should be performed after the first back button click, before the second back press.
Returns :
DoubleBackPress
-
-
Setting the environment using
set...
methods for the DoubleBackPress object.-
Set the duration within which the second back press needs to occur to be considered a Double Back Press.
REQUIRED
.setDoublePressDuration(int doublePressDuration)
Parameters :
doublePressDuration
: int => msec duration period, within which the back press should occur again to be counted as a DoubleBackPress
Returns :
void
Exceptions possible later if not called :
RequirementsNotMetException
: if thedoublePressDuration
is not set
-
Set the action to be performed after the DoubleBackPress occurs.
REQUIRED
.setDoubleBackPressAction(DoubleBackPressAction doubleBackPressAction)
Parameters :
doubleBackPressAction
: DoubleBackPressAction => The action that should be performed after DoubleBackPress
Returns :
void
Exceptions possible later if not called :
RequirementsNotMetException
: if thedoubleBackPressAction
is not set
-
Set the action to be performed after the first back press occurs, before the second back press.
OPTIONAL
.setFirstBackPressAction(FirstBackPressAction firstBackPressAction)
Parameters :
firstBackPressAction
: FirstBackPressAction => The action that should be performed after the first back button click, before the second back press.
Returns :
void
-
-
Create object of
DoubleBackPressAction
Passed in :
.withDoubleBackPressAction(DoubleBackPressAction)
Override the
actionCall()
method when creating the Object. This method is called when the second back press occurs.Below is an example where after the DoubleBackPress, the Activity calls its original
onBackPressed()
method.DoubleBackPressAction doubleBackPressAction = new DoubleBackPressAction() { @Override public void actionCall() { ExampleActivity.super.onBackPressed(); } };
-
Create object of
FirstBackPressAction
Passed in :
.withFirstBackPressAction(FirstBackPressAction)
Override the
actionCall()
method when creating the Object. This method is called when the first back press occurs.Below is an example where after the first back button press, the Activity shows a Toast to the user.
FirstBackPressAction firstBackPressAction = new FirstBackPressAction() { @Override public void actionCall() { Toast.makeText(ExampleActivity.this, "Press back again to Exit", Toast.LENGTH_SHORT).show(); } };
-
Standard displays after the first action to be shown to the user:
HELPER CLASSES
-
Example to show toast for
Toast.LENGTH_SHORT
period of time upon the first back button press, with a message reading "Press back button to confirm".FirstBackPressAction firstBackPressAction = new ToastDisplay() .standard(this); //required DoubleBackPress doubleBackPress = new DoubleBackPress() .withFirstBackPressAction(firstBackPressAction) ...
Example to show toast for
Toast.LENGTH_SHORT
period of time upon the first back button press, with a message reading "Press back button to Exit".FirstBackPressAction firstBackPressAction = new ToastDisplay() .standard(this, "Press back button to Exit"); //required DoubleBackPress doubleBackPress = new DoubleBackPress() .withFirstBackPressAction(firstBackPressAction) ...
-
SnackbarDisplay
: standard SnackbarNote : Since Snackbar require the callers's parent view, the SnackbarDisplay class needs to be set and provided to the DoubleBackPress object after the View is set. Example, in an Activity, this would be inside the onCreate() function, after the setContentView() function is called.
Example to show snackbar for
Snackbar.LENGTH_SHORT
period of time upon the first back button press, with a message reading "Press back button to confirm".//after the view is initialized FirstBackPressAction firstBackPressAction = new Snackbar() .standard(parentView); //required doubleBackPress.setFirstBackPressAction(firstBackPressAction)
Example to show snackbar for
Snackbar.LENGTH_SHORT
period of time upon the first back button press, with a message reading "Press back button to Exit".//after the view is initialized FirstBackPressAction firstBackPressAction = new Snackbar() .standard(parentView, "Press back button to Exit"); //required doubleBackPress.setFirstBackPressAction(firstBackPressAction)
-
-
Options provided for the ToastDisplay constructor :
-
Set the context for the Toast
.standard(Context context)
Parameters :
context
: Context => Set the Context for the Toast to be displayed
Returns :
ToastDisplay
Exceptions possible later if not called :
RequirementsNotMetException
: if thecontext
is not set
-
Set the context and the message for the Toast
.standard(Context context, String message)
Parameters :
-
context
: Context => Set the Context for the Toast to be displayed -
message
: String => Set the message to be displayed in the Toast
Returns :
ToastDisplay
Exceptions possible later if not called :
RequirementsNotMetException
: if thecontext
is not set
-
-
-
Options provided for the SnackbarDisplay constructor :
Note again : The standard method needs to be called after the view is created.
-
Set the parent view for the Toast
.standard(View parentView)
Parameters :
parentView
: View => Set the Parent View for the Snackbar to be displayed
Returns :
SnackbarDisplay
Exceptions possible later if not called :
RequirementsNotMetException
: if theparentView
is not setGeneralException
: if theparentView
was passed to the SnackbarDisplay before it was created
-
Set the parent view and the message for the Toast
.standard(View parentView, String message)
Parameters :
-
parentView
: View => Set the Parent View for the Snackbar to be displayed -
message
: String => Set the message to be displayed in the Snackbar
Returns :
SnackbarDisplay
Exceptions possible later if not called :
RequirementsNotMetException
: if theparentView
is not setGeneralException
: if theparentView
was passed to the SnackbarDisplay before it was created
-
-