SwipeBack for Android Activities does pretty the same as the android “back-button” does, but in a really intuitive way by using a swipe gesture

The Samsung Galaxy Nexus was one of the first device without hardware buttons for “back”, “home” and “app switching (multitasking)” but used the androids navigation bar on screen (introduced in Android 4.0). The navigation bar was at least in my opinion a big step forward, especially on screen rotation from landscape to portrait and vice versa. But I asked myself, do we really need a navigation bar? I mean the navigation bar takes ca. 10 % of the whole screen. Even at the home screen the “back” and “home” button are useless (because they do nothing). So I thought to myself: Why do we not use swipe gestures instead of a navigation bar? But maybe this idea is to futuristic and not suitable for all kind of android user. A few years later apple introduced the swipe back gesture in iOS 7. Why doesn’t Android Apps use swipe gesture as alternative to the back button. Pinterest and Tumblr do so:

pinterest

How Pinterest implement that? They use a single Activity and a ViewPager with custom page transformer. Something like this:

The problems with this approach are:

  1. You will lost a little bit the ability to jump to any screen by using intents. Take Pinterest as an example: If you get a push notification from Pinterest and you click on it you will see a loading dialog on screen. Internal the navigation stack is generated by adding Fragments to the ViewPager.

  2. ActionBar: The ActionBar is as default not part of a fragment, but it’s part of the activity. So you can not (by using a ViewPager) use the default ActionBar to swipe back to the previous Fragment, because the ActionBar will remain sticky. So you have to implement you own ActionBar and attach that to the fragments view.

SwipeBack

My approach can be used for activities. It does pretty the same as the android menu drawers do. It adds an additional layout and slides the content or the window to the side. It’s pretty the same as many android navigation drawer libraries do. So we will use overridePendingTransition() to start an activity and we will override onBackPressed() and setting there the reverse animation. For instance we can use a slide-in animation when starting an Activity and a slide-out animation onBackPressed. So far so good. SwipeBack will detect the swipe gesture for you and slide the activity or window content. It will show a “hint” to give the user feedback that by dropping the finger from screen the Activity will be finished by triggering onBackPressed().

Examples

The kicker app for android uses SwipeBack.

kicker

How to use it

You simply have to set it up in you Activities onCreate() method. Instead of Activity.setContentView() call SwipeBack.setContentView(). It’s not supported yet to build it from xml.

public class SwipeBackActivity extends FragmentActivity {

  @Override
  public void onCreate(Bundle saved){
    super.onCreate(saved);

    // Init the swipe back
    SwipeBack.attach(this, Position.LEFT)
        .setContentView(R.layout.activity_simple)
        .setSwipeBackView(R.layout.swipeback_default);

  }


  @Override
  public void onBackPressed(){
    super.onBackPressed();
    overridePendingTransition(R.anim.swipeback_stack_to_front,
        R.anim.swipeback_stack_right_out);
  }
}

Library

The SwipeBack library is available on Github and on maven central.

Comments