• Stars
    star
    130
  • Rank 277,575 (Top 6 %)
  • Language
    Java
  • License
    Apache License 2.0
  • Created about 9 years ago
  • Updated about 9 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Android lifecycle safety task with sugar code style.

SugarTask

Android Arsenal

Android lifecycle safety task with sugar code style.

Support Android API 14 and UP.

If you have any question or want to discuss, just open an issue. Pull request is always welcome :)

Gradle

At your top-level build.gradle file:

repositories {
    // ...
    maven { url 'https://jitpack.io' }
}

And then at your project build.gradle file:

dependencies {
    compile 'com.github.mthli:SugarTask:v0.4'
}

Done!

Usage

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

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 for your.

Notice:

  • MUST: .with(), .assign(), .execute().

  • OPTION: .handle(), .finish(), broken(). 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 :)

When Activity/Fragment is onStop(), we just cancel all MainThread callback, so that avoid OOM/NPE.

Get more information from our source code.

More

SugarTask is so simple that it just works good for easy task, if you need more functions, just have a look at RxAndroid.

Thanks

License

Copyright 2015 Matthew Lee

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

More Repositories

1

Knife

Knife is a rich text editor component for writing documents in Android.
Java
2,107
star
2

Ninja

Yet another web browser for Android.
Java
956
star
3

Slice

Android drawable that allows you custom round rect position.
Java
597
star
4

Mount

mount/umount apps that you don't like on Android, without root.
Java
309
star
5

lemonsqueepy

Lemon Squeezy with Python 🐍
Python
243
star
6

Bitocle

Bitocle is a simple android app which allows you to view your source code in GitHub.
Java
204
star
7

better-youtube-summary-server

Literally Better YouTube Summary 🎯
Python
199
star
8

webrtc-tutorial

Learning WebRTC the Hard Way 👀
JavaScript
179
star
9

Tweetin

Yet another Twitter unofficial client for Lollipop.
Java
174
star
10

Java

《Core Java》学习笔记。
Java
133
star
11

Cracker

Collect UncaughtException when your Android App crash.
Java
65
star
12

Type

Typing experiment on Android.
Java
56
star
13

blog

Yet another blog of myself 👀
Assembly
42
star
14

Nongfu

A IntelliJ IDEA plugin that help you move files or directories to target module quickly.
Java
36
star
15

RxCoroutineSchedulers

Kotlin Coroutines as RxJava Schedulers 😈
Kotlin
30
star
16

Mp3

Windows下一个简陋的MP3播放器。使用C语言调用Windows API实现。
C
15
star
17

Ninja2

Yet another web browser for Android, version 2.
14
star
18

AndroidPlayground

A sample playground for (re)learning Android.
Kotlin
12
star
19

mthli.github.io

Deploy my blog on GitHub.
HTML
10
star
20

PyRobot

一些自动抓取内容的Python脚本集合。
Python
8
star
21

Geeky

GeekPark Unofficial Android Client.
Java
8
star
22

better-youtube-summary-extension

Literally Better YouTube Summary 🎯
TypeScript
7
star
23

EatWhat

吃神马是一个永恒的话题。让我们摇起来!
Java
7
star
24

EasyRust

一份简单的Rust中文入门教程。
6
star
25

mthli

My GitHub profile repo 👀
6
star
26

Send2SN

班级信息推送小应用,可以将班级信息发送到班级成员邮箱、微信公众平台和人人网。采用GTK+2.24、C语言和Python2开发。
C
4
star
27

COIMS

COIMS是应西安某救助站需求,义务为其开发的简易流浪者病历管理软件,基于GTK+2.24版本、SQLite3并采用C语言开发。不以成败论英雄。
C
4
star
28

jni_zero

Python
3
star
29

SwitcherPanel

Try to implement Firefox(for Android)'s switcher panel.
Java
2
star
30

Dribbble4J

Dribbble API for Java.
Java
2
star
31

gatsby-theme-apollo-docs

JavaScript
1
star
32

Bingw

抓取Bing每日图片并设置为桌面背景,目前支持Gnome桌面环境和Windows桌面环境。
C
1
star
33

Dara

Shielding notifications that you don't want to see.
Java
1
star
34

invest

Learning Invest the Hard Way 💸
JavaScript
1
star
35

lemontree

A lemonsqueepy example 🍋
TypeScript
1
star