Skip to content

Commit 364fe0f

Browse files
committed
create AutoLifecycle
1 parent 6a0dd87 commit 364fe0f

21 files changed

+640
-55
lines changed

app/src/main/AndroidManifest.xml

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3-
package="com.wingjay.autolifecycle.app" >
3+
package="com.wingjay.autolifecycle.app">
44

55
<application
66
android:allowBackup="true"
77
android:icon="@mipmap/ic_launcher"
88
android:label="@string/app_name"
99
android:roundIcon="@mipmap/ic_launcher_round"
1010
android:supportsRtl="true"
11-
android:theme="@style/AppTheme" >
12-
<activity android:name=".MainActivity" >
11+
android:theme="@style/AppTheme">
12+
<activity android:name=".MainActivity">
1313
<intent-filter>
14-
<action android:name="android.intent.action.MAIN" />
14+
<action android:name="android.intent.action.MAIN"/>
1515

16-
<category android:name="android.intent.category.LAUNCHER" />
16+
<category android:name="android.intent.category.LAUNCHER"/>
1717
</intent-filter>
1818
</activity>
1919
</application>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
package com.wingjay.jayandroid;
2+
3+
import android.os.Bundle;
4+
import android.support.annotation.NonNull;
5+
import android.support.v7.app.AppCompatActivity;
6+
import android.util.Log;
7+
import butterknife.ButterKnife;
8+
import com.wingjay.jayandroid.autolifecycle.ActivityLifecycle;
9+
import com.wingjay.jayandroid.autolifecycle.IContextLifecycle;
10+
import com.wingjay.jayandroid.autolifecycle.ILifecycleProvider;
11+
import com.wingjay.jayandroid.autolifecycle.LifecycleProviderDelegate;
12+
import rx.Observable;
13+
import rx.Observable.Transformer;
14+
import rx.subjects.PublishSubject;
15+
16+
public class BaseActivity extends AppCompatActivity implements ILifecycleProvider {
17+
18+
protected final PublishSubject<IContextLifecycle> lifecycleSubject = PublishSubject.create();
19+
private LifecycleProviderDelegate lifecycleProviderDelegate = new LifecycleProviderDelegate();
20+
protected String TAG = "jayDebug";
21+
22+
@Override
23+
protected void onCreate(Bundle savedInstanceState) {
24+
super.onCreate(savedInstanceState);
25+
Log.i(TAG, "onCreate");
26+
lifecycleSubject.onNext(ActivityLifecycle.CREATE);
27+
}
28+
29+
@Override
30+
public void setContentView(int layoutResID) {
31+
lifecycleSubject.onNext(ActivityLifecycle.PRE_INFLATE);
32+
Log.i(TAG, "setContentView");
33+
super.setContentView(layoutResID);
34+
ButterKnife.bind(this);
35+
}
36+
37+
@Override
38+
protected void onStart() {
39+
super.onStart();
40+
Log.i(TAG, "onStart");
41+
lifecycleSubject.onNext(ActivityLifecycle.START);
42+
}
43+
44+
@Override
45+
protected void onResume() {
46+
super.onResume();
47+
Log.i(TAG, "onResume");
48+
lifecycleSubject.onNext(ActivityLifecycle.RESUME);
49+
}
50+
51+
@Override
52+
protected void onPostResume() {
53+
super.onPostResume();
54+
Log.i(TAG, "onPostResume");
55+
}
56+
57+
@Override
58+
protected void onPause() {
59+
lifecycleSubject.onNext(ActivityLifecycle.PAUSE);
60+
super.onPause();
61+
Log.i(TAG, "onPause");
62+
}
63+
64+
@Override
65+
protected void onStop() {
66+
lifecycleSubject.onNext(ActivityLifecycle.STOP);
67+
super.onStop();
68+
Log.i(TAG, "onStop");
69+
}
70+
71+
@Override
72+
protected void onDestroy() {
73+
lifecycleSubject.onNext(ActivityLifecycle.DESTROY);
74+
super.onDestroy();
75+
Log.i(TAG, "onDestroy");
76+
}
77+
78+
@Override
79+
protected void onSaveInstanceState(Bundle outState) {
80+
super.onSaveInstanceState(outState);
81+
Log.i(TAG, "onSaveInstanceState");
82+
}
83+
84+
@Override
85+
protected void onResumeFragments() {
86+
super.onResumeFragments();
87+
Log.i(TAG, "onResumeFragments");
88+
}
89+
90+
@Override
91+
protected void onRestoreInstanceState(Bundle savedInstanceState) {
92+
super.onRestoreInstanceState(savedInstanceState);
93+
Log.i(TAG, "onRestoreInstanceState");
94+
}
95+
96+
@Override
97+
public <T> Transformer<T, T> bindUntilEvent(@NonNull IContextLifecycle event) {
98+
return lifecycleProviderDelegate.bindUntilEvent(lifecycleSubject, event);
99+
}
100+
101+
@Override
102+
public <T> Transformer<T, T> bindDefault() {
103+
return lifecycleProviderDelegate.bindUntilEvent(lifecycleSubject, ActivityLifecycle.DESTROY);
104+
}
105+
106+
@Override
107+
public void executeWhen(@NonNull Observable observable, @NonNull IContextLifecycle event) {
108+
lifecycleProviderDelegate.executeWhen(lifecycleSubject, observable, event);
109+
}
110+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.wingjay.jayandroid.autolifecycle;
2+
3+
import android.util.Log;
4+
5+
/**
6+
* IKnowLifecycle
7+
*
8+
* @author wingjay
9+
* @date 2017/10/22
10+
*/
11+
public class IKnowLifecycle {
12+
13+
public IKnowLifecycle(ILifecycleProvider lifecycleProvider) {
14+
AutoLifecycle.getInstance().init(this, lifecycleProvider);
15+
}
16+
17+
@AutoLifecycleEvent(activity = ActivityLifecycle.CREATE)
18+
private void onCreate() {
19+
Log.e("jaydebug", "IKnowLifecycle onCreate");
20+
}
21+
22+
@AutoLifecycleEvent(activity = ActivityLifecycle.START)
23+
private void onSTART() {
24+
Log.e("jaydebug", "IKnowLifecycle onSTART");
25+
}
26+
27+
@AutoLifecycleEvent(activity = ActivityLifecycle.RESUME)
28+
private void onRESUME() {
29+
Log.e("jaydebug", "IKnowLifecycle onRESUME");
30+
}
31+
32+
@AutoLifecycleEvent(activity = ActivityLifecycle.PAUSE)
33+
private void onPAUSE() {
34+
Log.e("jaydebug", "IKnowLifecycle onPAUSE");
35+
}
36+
37+
@AutoLifecycleEvent(activity = ActivityLifecycle.STOP)
38+
private void onSTOP() {
39+
Log.e("jaydebug", "IKnowLifecycle onSTOP");
40+
}
41+
42+
@AutoLifecycleEvent(activity = ActivityLifecycle.DESTROY)
43+
private void onDESTROY() {
44+
Log.e("jaydebug", "IKnowLifecycle onDESTROY");
45+
}
46+
}

app/src/main/java/com/wingjay/autolifecycle/app/MainActivity.java

+26-2
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,35 @@
33
import android.support.v7.app.AppCompatActivity;
44
import android.os.Bundle;
55

6-
public class MainActivity extends AppCompatActivity {
6+
public class MainActivity extends BaseActivity {
7+
private IKnowLifecycle mIKnowLifecycle = new IKnowLifecycle(this);
78

89
@Override
910
protected void onCreate(Bundle savedInstanceState) {
1011
super.onCreate(savedInstanceState);
11-
setContentView(R.layout.activity_main);
12+
Observable.interval(1, TimeUnit.SECONDS)
13+
.compose(this.<Long>bindUntilEvent(ActivityLifecycle.STOP))
14+
.subscribe(new Action1<Long>() {
15+
@Override
16+
public void call(Long aLong) {
17+
Log.e(TAG, "auto-stop when Activity onDestroy: interval " + aLong);
18+
}
19+
});
20+
21+
Observable loadDataObservable = Observable.defer(new Func0<Observable<Object>>() {
22+
@Override
23+
public Observable<Object> call() {
24+
return Observable.just(null).map(new Func1<Object, Object>() {
25+
@Override
26+
public Object call(Object o) {
27+
Log.e(TAG, "auto-execute when Activity PRE_INFLATE: loadData()");
28+
return null;
29+
}
30+
});
31+
}
32+
});
33+
executeWhen(loadDataObservable, ActivityLifecycle.PRE_INFLATE);
34+
35+
setContentView(R.layout.activity_test);
1236
}
1337
}
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,114 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<vector
3-
android:height="24dp"
3+
xmlns:android="http://schemas.android.com/apk/res/android"
44
android:width="24dp"
5+
android:height="24dp"
56
android:viewportHeight="108.0"
6-
android:viewportWidth="108.0"
7-
xmlns:android="http://schemas.android.com/apk/res/android">
8-
<path android:fillColor="#26A69A"
7+
android:viewportWidth="108.0">
8+
<path
9+
android:fillColor="#26A69A"
910
android:pathData="M0,0h108v108h-108z"
10-
android:strokeColor="#66FFFFFF" android:strokeWidth="0.8"/>
11-
<path android:fillColor="#00000000" android:pathData="M19,0L19,108"
12-
android:strokeColor="#33FFFFFF" android:strokeWidth="0.8"/>
13-
<path android:fillColor="#00000000" android:pathData="M9,0L9,108"
14-
android:strokeColor="#66FFFFFF" android:strokeWidth="0.8"/>
15-
<path android:fillColor="#00000000" android:pathData="M39,0L39,108"
16-
android:strokeColor="#66FFFFFF" android:strokeWidth="0.8"/>
17-
<path android:fillColor="#00000000" android:pathData="M29,0L29,108"
18-
android:strokeColor="#66FFFFFF" android:strokeWidth="0.8"/>
19-
<path android:fillColor="#00000000" android:pathData="M59,0L59,108"
20-
android:strokeColor="#66FFFFFF" android:strokeWidth="0.8"/>
21-
<path android:fillColor="#00000000" android:pathData="M49,0L49,108"
22-
android:strokeColor="#66FFFFFF" android:strokeWidth="0.8"/>
23-
<path android:fillColor="#00000000" android:pathData="M79,0L79,108"
24-
android:strokeColor="#66FFFFFF" android:strokeWidth="0.8"/>
25-
<path android:fillColor="#00000000" android:pathData="M69,0L69,108"
26-
android:strokeColor="#66FFFFFF" android:strokeWidth="0.8"/>
27-
<path android:fillColor="#00000000" android:pathData="M89,0L89,108"
28-
android:strokeColor="#33FFFFFF" android:strokeWidth="0.8"/>
29-
<path android:fillColor="#00000000" android:pathData="M99,0L99,108"
30-
android:strokeColor="#66FFFFFF" android:strokeWidth="0.8"/>
31-
<path android:fillColor="#00000000" android:pathData="M0,89L108,89"
32-
android:strokeColor="#33FFFFFF" android:strokeWidth="0.8"/>
33-
<path android:fillColor="#00000000" android:pathData="M0,99L108,99"
34-
android:strokeColor="#66FFFFFF" android:strokeWidth="0.8"/>
35-
<path android:fillColor="#00000000" android:pathData="M0,69L108,69"
36-
android:strokeColor="#66FFFFFF" android:strokeWidth="0.8"/>
37-
<path android:fillColor="#00000000" android:pathData="M0,79L108,79"
38-
android:strokeColor="#66FFFFFF" android:strokeWidth="0.8"/>
39-
<path android:fillColor="#00000000" android:pathData="M0,49L108,49"
40-
android:strokeColor="#66FFFFFF" android:strokeWidth="0.8"/>
41-
<path android:fillColor="#00000000" android:pathData="M0,59L108,59"
42-
android:strokeColor="#66FFFFFF" android:strokeWidth="0.8"/>
43-
<path android:fillColor="#00000000" android:pathData="M0,29L108,29"
44-
android:strokeColor="#66FFFFFF" android:strokeWidth="0.8"/>
45-
<path android:fillColor="#00000000" android:pathData="M0,39L108,39"
46-
android:strokeColor="#66FFFFFF" android:strokeWidth="0.8"/>
47-
<path android:fillColor="#00000000" android:pathData="M0,19L108,19"
48-
android:strokeColor="#33FFFFFF" android:strokeWidth="0.8"/>
49-
<path android:fillColor="#00000000" android:pathData="M0,9L108,9"
50-
android:strokeColor="#66FFFFFF" android:strokeWidth="0.8"/>
11+
android:strokeColor="#66FFFFFF"
12+
android:strokeWidth="0.8"/>
13+
<path
14+
android:fillColor="#00000000"
15+
android:pathData="M19,0L19,108"
16+
android:strokeColor="#33FFFFFF"
17+
android:strokeWidth="0.8"/>
18+
<path
19+
android:fillColor="#00000000"
20+
android:pathData="M9,0L9,108"
21+
android:strokeColor="#66FFFFFF"
22+
android:strokeWidth="0.8"/>
23+
<path
24+
android:fillColor="#00000000"
25+
android:pathData="M39,0L39,108"
26+
android:strokeColor="#66FFFFFF"
27+
android:strokeWidth="0.8"/>
28+
<path
29+
android:fillColor="#00000000"
30+
android:pathData="M29,0L29,108"
31+
android:strokeColor="#66FFFFFF"
32+
android:strokeWidth="0.8"/>
33+
<path
34+
android:fillColor="#00000000"
35+
android:pathData="M59,0L59,108"
36+
android:strokeColor="#66FFFFFF"
37+
android:strokeWidth="0.8"/>
38+
<path
39+
android:fillColor="#00000000"
40+
android:pathData="M49,0L49,108"
41+
android:strokeColor="#66FFFFFF"
42+
android:strokeWidth="0.8"/>
43+
<path
44+
android:fillColor="#00000000"
45+
android:pathData="M79,0L79,108"
46+
android:strokeColor="#66FFFFFF"
47+
android:strokeWidth="0.8"/>
48+
<path
49+
android:fillColor="#00000000"
50+
android:pathData="M69,0L69,108"
51+
android:strokeColor="#66FFFFFF"
52+
android:strokeWidth="0.8"/>
53+
<path
54+
android:fillColor="#00000000"
55+
android:pathData="M89,0L89,108"
56+
android:strokeColor="#33FFFFFF"
57+
android:strokeWidth="0.8"/>
58+
<path
59+
android:fillColor="#00000000"
60+
android:pathData="M99,0L99,108"
61+
android:strokeColor="#66FFFFFF"
62+
android:strokeWidth="0.8"/>
63+
<path
64+
android:fillColor="#00000000"
65+
android:pathData="M0,89L108,89"
66+
android:strokeColor="#33FFFFFF"
67+
android:strokeWidth="0.8"/>
68+
<path
69+
android:fillColor="#00000000"
70+
android:pathData="M0,99L108,99"
71+
android:strokeColor="#66FFFFFF"
72+
android:strokeWidth="0.8"/>
73+
<path
74+
android:fillColor="#00000000"
75+
android:pathData="M0,69L108,69"
76+
android:strokeColor="#66FFFFFF"
77+
android:strokeWidth="0.8"/>
78+
<path
79+
android:fillColor="#00000000"
80+
android:pathData="M0,79L108,79"
81+
android:strokeColor="#66FFFFFF"
82+
android:strokeWidth="0.8"/>
83+
<path
84+
android:fillColor="#00000000"
85+
android:pathData="M0,49L108,49"
86+
android:strokeColor="#66FFFFFF"
87+
android:strokeWidth="0.8"/>
88+
<path
89+
android:fillColor="#00000000"
90+
android:pathData="M0,59L108,59"
91+
android:strokeColor="#66FFFFFF"
92+
android:strokeWidth="0.8"/>
93+
<path
94+
android:fillColor="#00000000"
95+
android:pathData="M0,29L108,29"
96+
android:strokeColor="#66FFFFFF"
97+
android:strokeWidth="0.8"/>
98+
<path
99+
android:fillColor="#00000000"
100+
android:pathData="M0,39L108,39"
101+
android:strokeColor="#66FFFFFF"
102+
android:strokeWidth="0.8"/>
103+
<path
104+
android:fillColor="#00000000"
105+
android:pathData="M0,19L108,19"
106+
android:strokeColor="#33FFFFFF"
107+
android:strokeWidth="0.8"/>
108+
<path
109+
android:fillColor="#00000000"
110+
android:pathData="M0,9L108,9"
111+
android:strokeColor="#66FFFFFF"
112+
android:strokeWidth="0.8"/>
51113
</vector>
52114

app/src/main/res/layout/activity_main.xml

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<android.support.constraint.ConstraintLayout
33
xmlns:android="http://schemas.android.com/apk/res/android"
4-
xmlns:tools="http://schemas.android.com/tools"
54
xmlns:app="http://schemas.android.com/apk/res-auto"
5+
xmlns:tools="http://schemas.android.com/tools"
66
android:layout_width="match_parent"
77
android:layout_height="match_parent"
88
tools:context="com.wingjay.autolifecycle.app.MainActivity">
@@ -14,6 +14,6 @@
1414
app:layout_constraintBottom_toBottomOf="parent"
1515
app:layout_constraintLeft_toLeftOf="parent"
1616
app:layout_constraintRight_toRightOf="parent"
17-
app:layout_constraintTop_toTopOf="parent" />
17+
app:layout_constraintTop_toTopOf="parent"/>
1818

1919
</android.support.constraint.ConstraintLayout>

0 commit comments

Comments
 (0)