Example usage for android.app Activity onBackPressed

List of usage examples for android.app Activity onBackPressed

Introduction

In this page you can find the example usage for android.app Activity onBackPressed.

Prototype

public void onBackPressed() 

Source Link

Document

Called when the activity has detected the user's press of the back key.

Usage

From source file:Main.java

/**
 * to programatically press back button//from   ww  w.  j  a va 2s .c  om
 *
 * @param activity calling activity
 */
public static void backPress(Activity activity) {
    activity.onBackPressed();
}

From source file:Main.java

public static boolean defaultOnOptionsItemSelected(Activity activity, MenuItem item) {
    switch (item.getItemId()) {
    case android.R.id.home:
        activity.onBackPressed();
        return true;
    default://from www.j a  va 2 s.c om
        return false;
    }
}

From source file:com.ouyangzn.github.utils.UiUtils.java

/**
 * ?activityonBackPressed//w w  w  .  j  a  v a  2  s  .c  o  m
 *
 * @param toolbar Toolbar
 * @param activity Activity
 */
public static void addWhiteBackBtn(Toolbar toolbar, Activity activity) {
    toolbar.setNavigationIcon(R.drawable.ic_back_white);
    toolbar.setNavigationOnClickListener(v -> activity.onBackPressed());
}

From source file:com.github.baoti.pioneer.ui.Navigator.java

/**
 *  Toolbar  /*from  w  w  w .  j  av  a 2 s  .c  o m*/
 * @return ???
 */
public static boolean setupToolbarNavigation(final Activity activity, Toolbar toolbar) {
    if (canNavigateBack(activity)) {
        toolbar.setNavigationIcon(R.drawable.abc_ic_ab_back_mtrl_am_alpha);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                activity.onBackPressed();
            }
        });
        return true;
    } else {
        toolbar.setNavigationIcon(null);
        return false;
    }
}

From source file:com.sythealth.fitness.util.Utils.java

public static void goBack(Button btn, final Activity activity) {
    btn.setOnClickListener(new OnClickListener() {
        @Override/*from ww  w.java2  s  . co  m*/
        public void onClick(View v) {
            activity.onBackPressed();
        }
    });
}

From source file:ru.yandex.subtitles.ui.fragment.AbstractFragment.java

public void onNavigationClick() {
    final Activity activity = getActivity();
    if (activity != null) {
        activity.onBackPressed();
    }//from  w  w  w.j a  v  a2  s. c  o  m
}

From source file:com.crazymin2.retailstore.ui.BaseActivity.java

/**
 * This utility method handles Up navigation intents by searching for a parent activity and
 * navigating there if defined. When using this for an activity make sure to define both the
 * native parentActivity as well as the AppCompat one when supporting API levels less than 16.
 * when the activity has a single parent activity. If the activity doesn't have a single parent
 * activity then don't define one and this method will use back button functionality. If "Up"
 * functionality is still desired for activities without parents then use
 * {@code syntheticParentActivity} to define one dynamically.
 * <p>//from w w  w .j a  v a 2s.c o  m
 * Note: Up navigation intents are represented by a back arrow in the top left of the Toolbar
 * in Material Design guidelines.
 *
 * @param currentActivity         Activity in use when navigate Up action occurred.
 * @param syntheticParentActivity Parent activity to use when one is not already configured.
 */
public static void navigateUpOrBack(Activity currentActivity,
        Class<? extends Activity> syntheticParentActivity) {
    // Retrieve parent activity from AndroidManifest.
    Intent intent = NavUtils.getParentActivityIntent(currentActivity);

    // Synthesize the parent activity when a natural one doesn't exist.
    if (intent == null && syntheticParentActivity != null) {
        try {
            intent = NavUtils.getParentActivityIntent(currentActivity, syntheticParentActivity);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
    }

    if (intent == null) {
        // No parent defined in manifest. This indicates the activity may be used by
        // in multiple flows throughout the app and doesn't have a strict parent. In
        // this case the navigation up button should act in the same manner as the
        // back button. This will result in users being forwarded back to other
        // applications if currentActivity was invoked from another application.
        currentActivity.onBackPressed();
    } else {
        if (NavUtils.shouldUpRecreateTask(currentActivity, intent)) {
            // Need to synthesize a backstack since currentActivity was probably invoked by a
            // different app. The preserves the "Up" functionality within the app according to
            // the activity hierarchy defined in AndroidManifest.xml via parentActivity
            // attributes.
            TaskStackBuilder builder = TaskStackBuilder.create(currentActivity);
            builder.addNextIntentWithParentStack(intent);
            builder.startActivities();
        } else {
            // Navigate normally to the manifest defined "Up" activity.
            NavUtils.navigateUpTo(currentActivity, intent);
        }
    }
}