<commit_before>SugarTask
===

Android lifecycle safety task with sugar code style.

## Gradle

__UNDER PREPARE__.

## Usage

At your MainThread(UIThread), start a background thread just like this:

```java
SugarTask.with(this) // Activity/FragmentActivity(v4)/Fragment/Fragment(v4)
        .assign(new SugarTask.TaskDescription() {
            @Override
            public Object onBackground() {
                // Do what you want to do on background thread.
                // If you want to post something to MainThread,
                // just call SugarTask.post(YOUR_MESSAGE).

                // Return your finally result(Nullable).
                return null;
            }
        })
        .handle(new SugarTask.MessageListener() {
            @Override
            public void handleMessage(@NonNull Message message) {
                // Receive message in MainThread which sent from WorkerThread,
                // update your UI just in time.
            }
        })
        .finish(new SugarTask.FinishListener() {
            @Override
            public void onFinish(@Nullable Object result) {
                // If WorkerThread finish without Exception and lifecycle safety,
                // deal with your WorkerThread result at here.
            }
        })
        .broken(new SugarTask.BrokenListener() {
            @Override
            public void onBroken(@NonNull Exception e) {
                // If WorkerThread finish with Exception and lifecycle safety,
                // deal with Exception at here.
            }
        })
        .execute();
```

Your don't need to conside about Activity/Fragment lifecycle, no matter screen rotating or some others.

Really nice for you :)

And [here is a simple example](https://github.com/mthli/SugarTask/blob/master/app/src/main/java/io/github/mthli/sugartaskdemo/MainFragment.java "SugarTaskDemo.MainFragment") for your.

__Notice__:

 - `.with()`, `.assign()`, `.execute()` is __MUST__.

 - `.handle()`, `.finish()`, `broken()` is __OPTION__. Every method just call once, otherwise the newer with replace the older.

## Theory

How to get Activity/Fragment lifecycle state real-time?

It's easy, just add a hook fragment to Activity/Fragment by their FragmentManager, the hook fragment will follow it's parent lifecycle, so we get state real-time :)
<commit_msg>README.md<commit_after>SugarTask
===

Android lifecycle safety task with sugar code style.

## Gradle

__UNDER PREPARE__.

## Usage

At your MainThread(UIThread), start a background thread just like this:

```java
SugarTask.with(this) // Activity|FragmentActivity(v4)|Fragment|Fragment(v4)
        .assign(new SugarTask.TaskDescription() {
            @Override
            public Object onBackground() {
                // Do what you want to do on background thread.
                // If you want to post something to MainThread,
                // just call SugarTask.post(YOUR_MESSAGE).

                // Return your finally result(Nullable).
                return null;
            }
        })
        .handle(new SugarTask.MessageListener() {
            @Override
            public void handleMessage(@NonNull Message message) {
                // Receive message in MainThread which sent from WorkerThread,
                // update your UI just in time.
            }
        })
        .finish(new SugarTask.FinishListener() {
            @Override
            public void onFinish(@Nullable Object result) {
                // If WorkerThread finish without Exception and lifecycle safety,
                // deal with your WorkerThread result at here.
            }
        })
        .broken(new SugarTask.BrokenListener() {
            @Override
            public void onBroken(@NonNull Exception e) {
                // If WorkerThread finish with Exception and lifecycle safety,
                // deal with Exception at here.
            }
        })
        .execute();
```

Your don't need to conside about Activity/Fragment lifecycle, no matter screen rotating or some others.

Really nice for you :)

And [here is a simple example](https://github.com/mthli/SugarTask/blob/master/app/src/main/java/io/github/mthli/sugartaskdemo/MainFragment.java "SugarTaskDemo.MainFragment") for your.

__Notice__:

 - `.with()`, `.assign()`, `.execute()` is __MUST__.

 - `.handle()`, `.finish()`, `broken()` is __OPTION__. Every method just call once, otherwise the newer with replace the older.

 - Use `SugarTask.post()` To send message from WorkerThread to MainThread just in time.

## Theory

How to get Activity/Fragment lifecycle state real-time?

It's easy, just add a hook fragment to Activity/Fragment by their FragmentManager, the hook fragment will follow it's parent lifecycle, so we get state real-time :)

{'fraction_non_alphanumeric': 0.09165097300690521, 'fraction_numerical': 0.0008369951872776732, 'mean_word_length': 2.218855218855219, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1608294', 'n_tokens_mistral': 1270, 'n_tokens_neox': 1203, 'n_words': 430}