Giter Site home page Giter Site logo

zhangjianlin77 / safelyandroid Goto Github PK

View Code? Open in Web Editor NEW

This project forked from piasy/safelyandroid

0.0 2.0 0.0 2.83 MB

Build safely Android app, no more Activity not found, Activity state loss and NPE during fragment transaction!

Home Page: http://blog.piasy.com/2016/04/03/Safely-Android/

License: MIT License

Java 100.00%

safelyandroid's Introduction

SafelyAndroid

Download

Build safely Android app, no more Activity not found, Activity state loss and NPE during fragment transaction!

Pause-safe fragment transaction

safely-android-demo.gif

Dependency

Add to gradle dependency of your module build.gradle:

repositories {
    jcenter()
}

dependencies {
    compile 'com.github.piasy:safelyandroid:1.2.0'
}

Usage

Start activity safely

Start activity from context, fragment and support v4 fragment, and start activity for result from activity, fragment and support v4 fragment, with or without the optional bundle options. StartActivityDelegate will launch the intent only when its component exist, otherwise, it will return false. Never get activity not found error again!

StartActivityDelegate.startActivitySafely(fragment, intent)

StartActivityDelegate.startActivitySafely(fragment, intent, options)

StartActivityDelegate.startActivityForResultSafely(fragment, intent, requestCode)

StartActivityDelegate.startActivityForResultSafely(fragment, intent, requestCode, options)

StartActivityDelegate.startActivitySafely(context, intent)

StartActivityDelegate.startActivitySafely(context, intent, options)

StartActivityDelegate.startActivityForResultSafely(activity, intent, requestCode)

StartActivityDelegate.startActivityForResultSafely(activity, intent, requestCode, options)

Dismiss dialog fragment safely

Dismiss a dialog fragment or support v4 dialog fragment safely. dialogFragmentDismissDelegate will dismiss this dialog fragment only when it's resumed, otherwise, it will return true and dismiss it when dialogFragmentDismissDelegate.onResumed is called. Never get activity state loss error again!

dialogFragmentDismissDelegate.safeDismiss(dialogFragment)

dialogFragmentDismissDelegate.onResumed(dialogFragment)

supportDialogFragmentDismissDelegate.safeDismiss(dialogFragment)

supportDialogFragmentDismissDelegate.onResumed(dialogFragment)

Do fragment transaction safely

Do fragment or support v4 fragment transaction safely. Components (activity, fragment or support v4 fragment) should implement the TransactionCommitter interface, and the fragment transaction will only be committed when the components is resumed, otherwise it will return true and the transaction will be committed when onResumed is called. Never get activity state loss error again!

fragmentTransactionDelegate.safeCommit(transactionCommitter, transaction)

fragmentTransactionDelegate.onResumed()

supportFragmentTransactionDelegate.safeCommit(transactionCommitter, transaction)

supportFragmentTransactionDelegate.onResumed()

Build fragment transaction safely

See FragmentTransactionBuilder and SupportFragmentTransactionBuilder.

Just extends the safely base components

We provide the default safely components, including SafelyActivity, SafelyDialogFragment, SafelyFragment, SafelyAppCompatActivity, SafelySupportDialogFragment, and SafelySupportFragment, which encapsulate safely behaviours above, you can just extends them, and dismiss dialog fragment with safeDismiss(), do fragment transaction with safeCommit(transaction), then you can build safely Android app now!

Create your own base components

If your base components must extends other base components, you can make your base component implement the TransactionCommitter interface, and mimic the behaviours of safely base components provided by us, then you can also build safely Android app now!

Lint check

When using safeCommit, lint will warn "Missing commit() calls", you could disable it.

We provide a custom lint rule, to check the "unsafe dismiss() call" of dialog fragment.

safely-android-demo.gif

Just adding this dependency in your module's build.gradle:

repositories {
    jcenter()
}

dependencies {
    compile 'com.github.piasy:safelyandroid-lint:1.2.0'
}

The suppress of "Missing commit() calls" and "unsafe commit() call" lint rules may not be added in the long future. :(

safelyandroid's People

Contributors

piasy avatar

Watchers

James Cloos avatar  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.