Java Code Examples for android.app.Activity#onBackPressed()

The following examples show how to use android.app.Activity#onBackPressed() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: EasySwipeConfig.java    From EasySwipeLayout with MIT License 6 votes vote down vote up
@Override
public void onSwipe(@NonNull Activity activity, @Edge int side) {
    activity.onBackPressed();
}
 
Example 2
Source File: ActivityScreenSwitcher.java    From u2020-mvp with Apache License 2.0 6 votes vote down vote up
@Override
public void goBack() {
    final Activity activity = getAttachedObject();
    if (activity != null) {
        activity.onBackPressed();
    }
}
 
Example 3
Source File: BaseActivity.java    From RetailStore with Apache License 2.0 5 votes vote down vote up
/**
 * 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>
 * 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);
        }
    }
}
 
Example 4
Source File: FragmentWebView.java    From rss with GNU General Public License v3.0 5 votes vote down vote up
@Override
public
boolean onOptionsItemSelected(MenuItem item)
{
    // If the back button in the ActionBar is selected, call the Activity's onBackPressed().
    if(android.R.id.home == item.getItemId())
    {
        Activity activity = getActivity();
        activity.onBackPressed();
        return true;
    }
    return super.onOptionsItemSelected(item);
}