Giter Club home page Giter Club logo

fragmentbackhandler's Introduction

FragmentBackHandler

Release

FragmentBackHandler 是一个 Fragment拦截Back键的一个库,仅需两步即可搞定,同时支持ViewPager、多Fragment以及Fragment嵌套。

详细内容参见Blog 两步搞定Fragment的返回键

Download

jar or source

allprojects {
	repositories {
	    jcenter()
		maven { url "https://jitpack.io" }
	}
}
dependencies {
    compile 'com.github.ikidou:FragmentBackHandler:2.1'
}

Usage

1、 在Activity中覆盖onBackPressed()方法

public class MyActivity extends FragmentActivity {
    @Override
    public void onBackPressed() {
        if (!BackHandlerHelper.handleBackPress(this)) {
            super.onBackPressed();
        }
    }
}

2、实现 FragmentBackHandler

public class MyFragment extends Fragment implements FragmentBackHandler {
    @Override
    public boolean onBackPressed() {
        if (handleBackPressed) {
            //外理返回键
            return true;
        } else {
            // 如果不包含子Fragment
            // 或子Fragment没有外理back需求
            // 可如直接 return false;
            // 注:如果Fragment/Activity 中可以使用ViewPager 代替 this
            //
            return BackHandlerHelper.handleBackPress(this);
        }
    }
}

或让需要拦截Back键的Fragment及父Fragment继承BackHandledFragment

// [推荐] 适合所有Fragment,只要需要栏截时return true即可,其它的无需关心。
// 当然你也可以让你的BaseFragment 继承 BackHandledFragment
public class MyFragment extends BackHandledFragment {
    // 如果return false 将自动调用 BackHandlerHelper.handleBackPress(this);
    @Override
    public boolean interceptBackPressed() {
        if (handleBackPressed) {
            //外理返回键
            return true;
        } else {
            return false;
        }
    }

    // 如果ViewPager中需要栏截back,可覆盖该方法。
    // 非必须,从2.1版本开始,即何使用了ViewPager也没有问题
    @Override
    public ViewPager getBackHandleViewPager() {
        return null;
    }
}

fragmentbackhandler's People

Contributors

ikidou avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.