Skip to content

Android Library : Provides Double Back Press functionality along with common templates

License

Notifications You must be signed in to change notification settings

kaushikthedeveloper/DoubleBackPress

Repository files navigation

Double Back Press : Android Library

Bintray License

About

Usage

The library provides double back press functionality, i.e., actions taken upon clicking of back button twice in short (custom predefined) interval of time.

Main Functionalities

  1. Action to be performed upon double click of back button
  2. Action to be performed upon single click of back button, before the double click occurs
  3. Standard responses (templates) after back button click

Examples

To run the Examples, build and execute the example module.

  1. Simple program

simple-program.gif

2. ToastDisplay + Exit Activity

toastdisplay-exit.gif

3. SnackbarDisplay + Default back press behaviour

snackbardisplay-back

Documentation

Load the library

Add the dependency to your module's build.gradle :

dependencies {
    implementation 'com.github.kaushikthedeveloper:double-back-press:0.0.1'
}

API Index


  1. Setup DoubleBackPress

    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();
    }

  1. 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 the doublePressDuration 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 the doubleBackPressAction 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

  1. 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 the doublePressDuration 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 the doubleBackPressAction 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

  1. 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();
        }
    };

  1. 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();
        }
    };

  1. Standard displays after the first action to be shown to the user:

    HELPER CLASSES

    • ToastDisplay : standard Toast

      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 Snackbar

      Note : 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)

  1. 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 the context 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 the context is not set

  1. 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 the parentView is not set
      • GeneralException : if the parentView 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 the parentView is not set
      • GeneralException : if the parentView was passed to the SnackbarDisplay before it was created

About

Android Library : Provides Double Back Press functionality along with common templates

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages