Skip to content

Commit feb85b3

Browse files
committed
ActivityMviDelegateImpl now checks if presenter != null if Activity.finish() is called in Activity.onCreate() sockeqwe#290§
1 parent 513d1e9 commit feb85b3

File tree

6 files changed

+162
-8
lines changed

6 files changed

+162
-8
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Copyright 2016 Hannes Dorfmann.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
*/
17+
18+
package com.hannesdorfmann.mosby3.mvi.integrationtest.lifecycle.activity;
19+
20+
import android.content.pm.ActivityInfo;
21+
import android.support.test.rule.ActivityTestRule;
22+
import android.support.test.runner.AndroidJUnit4;
23+
24+
import com.hannesdorfmann.mosby3.mvi.integrationtest.lifecycle.LifecycleTestPresenter;
25+
26+
import org.junit.AfterClass;
27+
import org.junit.Assert;
28+
import org.junit.Rule;
29+
import org.junit.Test;
30+
import org.junit.runner.RunWith;
31+
32+
@RunWith(AndroidJUnit4.class) public class MviFinishInOnCreateActivityTest {
33+
34+
@Rule public ActivityTestRule<MviFinishInOnCreateActivity> rule =
35+
new ActivityTestRule<>(MviFinishInOnCreateActivity.class);
36+
37+
@Test public void finishInOnCreate() throws Exception {
38+
// Context of the app under test.
39+
MviFinishInOnCreateActivity activity = rule.getActivity();
40+
activity.onDestroyReached.blockingFirst(); // Waits until onDestroy() is reached
41+
}
42+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Copyright 2016 Hannes Dorfmann.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
*/
17+
18+
package com.hannesdorfmann.mosby3.mvi.integrationtest.lifecycle.activity;
19+
20+
import android.support.test.rule.ActivityTestRule;
21+
import android.support.test.runner.AndroidJUnit4;
22+
23+
import org.junit.Rule;
24+
import org.junit.Test;
25+
import org.junit.runner.RunWith;
26+
27+
@RunWith(AndroidJUnit4.class) public class MviFinishInOnStartActivityTest {
28+
29+
@Rule public ActivityTestRule<MviFinishInOnStartActivity> rule =
30+
new ActivityTestRule<>(MviFinishInOnStartActivity.class);
31+
32+
@Test public void finishInOnCreate() throws Exception {
33+
// Context of the app under test.
34+
MviFinishInOnStartActivity activity = rule.getActivity();
35+
activity.onDestroyReached.blockingFirst(); // Waits until onDestroy() is reached
36+
}
37+
}

mvi-integration-test/src/main/AndroidManifest.xml

+2
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535
<activity android:name=".lifecycle.fragment.childfragment.MviLifecycleChildFragmentActivity" />
3636
<activity android:name=".lifecycle.fragment.backstack.MviLifecycleBackstackActivity" />
3737
<activity android:name=".lifecycle.viewgroup.MviViewGroupContainerActivity" />
38+
<activity android:name=".lifecycle.activity.MviFinishInOnCreateActivity" />
39+
<activity android:name=".lifecycle.activity.MviFinishInOnStartActivity" />
3840
<activity
3941
android:name=".backstack.BackstackActivity"
4042
android:label="@string/title_activity_backstack"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.hannesdorfmann.mosby3.mvi.integrationtest.lifecycle.activity;
2+
3+
import android.os.Bundle;
4+
import android.support.annotation.NonNull;
5+
6+
import com.hannesdorfmann.mosby3.mvi.MviActivity;
7+
import com.hannesdorfmann.mosby3.mvi.integrationtest.lifecycle.LifecycleTestPresenter;
8+
import com.hannesdorfmann.mosby3.mvi.integrationtest.lifecycle.LifecycleTestView;
9+
import io.reactivex.subjects.BehaviorSubject;
10+
import io.reactivex.subjects.Subject;
11+
12+
public class MviFinishInOnCreateActivity extends MviActivity<LifecycleTestView, LifecycleTestPresenter> implements LifecycleTestView {
13+
14+
@NonNull
15+
@Override
16+
public LifecycleTestPresenter createPresenter() {
17+
return new LifecycleTestPresenter();
18+
}
19+
20+
public Subject<Boolean> onDestroyReached = BehaviorSubject.create();
21+
22+
23+
@Override
24+
protected void onCreate(Bundle savedInstanceState) {
25+
super.onCreate(savedInstanceState);
26+
finish(); // finish imeediately is what we would like to test --> App should not crash
27+
}
28+
29+
30+
@Override
31+
protected void onDestroy() {
32+
super.onDestroy();
33+
onDestroyReached.onNext(true);
34+
onDestroyReached.onComplete();
35+
}
36+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.hannesdorfmann.mosby3.mvi.integrationtest.lifecycle.activity;
2+
3+
import android.os.Bundle;
4+
import android.support.annotation.NonNull;
5+
6+
import com.hannesdorfmann.mosby3.mvi.MviActivity;
7+
import com.hannesdorfmann.mosby3.mvi.integrationtest.lifecycle.LifecycleTestPresenter;
8+
import com.hannesdorfmann.mosby3.mvi.integrationtest.lifecycle.LifecycleTestView;
9+
10+
import io.reactivex.subjects.BehaviorSubject;
11+
import io.reactivex.subjects.Subject;
12+
13+
public class MviFinishInOnStartActivity extends MviActivity<LifecycleTestView, LifecycleTestPresenter> implements LifecycleTestView {
14+
15+
@NonNull
16+
@Override
17+
public LifecycleTestPresenter createPresenter() {
18+
return new LifecycleTestPresenter();
19+
}
20+
21+
public Subject<Boolean> onDestroyReached = BehaviorSubject.create();
22+
23+
24+
@Override
25+
protected void onStart() {
26+
super.onStart();
27+
finish(); // finish --> this is what we would like to test --> App should not crash
28+
}
29+
30+
@Override
31+
protected void onDestroy() {
32+
super.onDestroy();
33+
onDestroyReached.onNext(true);
34+
onDestroyReached.onComplete();
35+
}
36+
}

mvi/src/main/java/com/hannesdorfmann/mosby3/ActivityMviDelegateImpl.java

+9-8
Original file line numberDiff line numberDiff line change
@@ -207,16 +207,17 @@ static boolean retainPresenterInstance(boolean keepPresenterInstance, Activity a
207207
}
208208

209209
@Override public void onDestroy() {
210-
211-
boolean retainPresenterInstance = retainPresenterInstance(keepPresenterInstance, activity);
212-
if (!retainPresenterInstance){
213-
presenter.destroy();
214-
if (mosbyViewId != null) { // mosbyViewId == null if keepPresenterInstance == false
215-
PresenterManager.remove(activity, mosbyViewId);
210+
if (presenter != null) { // Presenter is only null if Activity.finish() called before Activity.onStart() has been reached
211+
boolean retainPresenterInstance = retainPresenterInstance(keepPresenterInstance, activity);
212+
if (!retainPresenterInstance) {
213+
presenter.destroy();
214+
if (mosbyViewId != null) { // mosbyViewId == null if keepPresenterInstance == false
215+
PresenterManager.remove(activity, mosbyViewId);
216+
}
217+
Log.d(DEBUG_TAG, "Destroying Presenter permanently " + presenter);
216218
}
217-
Log.d(DEBUG_TAG, "Destroying Presenter permanently " + presenter);
218-
}
219219

220+
}
220221
presenter = null;
221222
activity = null;
222223
delegateCallback = null;

0 commit comments

Comments
 (0)