Skip to content

Commit 3e2e382

Browse files
committed
Retain fragments and refactor. Add user list fragment programatically.
1 parent bd05f39 commit 3e2e382

File tree

8 files changed

+50
-59
lines changed

8 files changed

+50
-59
lines changed

domain/src/main/java/com/fernandocejas/android10/sample/domain/interactor/UseCase.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ protected UseCase(ThreadExecutor threadExecutor,
5252
/**
5353
* Executes the current use case.
5454
*
55-
* @param UseCaseSubscriber The guy who will be listen to the observable build with {@link #buildUseCaseObservable()}.
55+
* @param UseCaseSubscriber The guy who will be listen to the observable build
56+
* with {@link #buildUseCaseObservable()}.
5657
*/
5758
@SuppressWarnings("unchecked")
5859
public void execute(Subscriber UseCaseSubscriber) {

presentation/src/main/java/com/fernandocejas/android10/sample/presentation/view/activity/UserDetailsActivity.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public static Intent getCallingIntent(Context context, int userId) {
5555
private void initializeActivity(Bundle savedInstanceState) {
5656
if (savedInstanceState == null) {
5757
this.userId = getIntent().getIntExtra(INTENT_EXTRA_PARAM_USER_ID, -1);
58-
addFragment(R.id.fl_fragment, UserDetailsFragment.newInstance(this.userId));
58+
addFragment(R.id.fl_fragment, UserDetailsFragment.create(this.userId));
5959
} else {
6060
this.userId = savedInstanceState.getInt(INSTANCE_STATE_PARAM_USER_ID);
6161
}

presentation/src/main/java/com/fernandocejas/android10/sample/presentation/view/activity/UserListActivity.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/**
22
* Copyright (C) 2014 android10.org. All rights reserved.
3+
*
34
* @author Fernando Cejas (the android10 coder)
45
*/
56
package com.fernandocejas.android10.sample.presentation.view.activity;
@@ -33,6 +34,10 @@ public static Intent getCallingIntent(Context context) {
3334
setContentView(R.layout.activity_user_list);
3435

3536
this.initializeInjector();
37+
38+
if (savedInstanceState == null) {
39+
addFragment(R.id.fragmentContainer, new UserListFragment());
40+
}
3641
}
3742

3843
private void initializeInjector() {
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,18 @@
11
/**
22
* Copyright (C) 2014 android10.org. All rights reserved.
3+
*
34
* @author Fernando Cejas (the android10 coder)
45
*/
56
package com.fernandocejas.android10.sample.presentation.view.fragment;
67

78
import android.app.Fragment;
8-
import android.os.Bundle;
99
import android.widget.Toast;
1010
import com.fernandocejas.android10.sample.presentation.internal.di.HasComponent;
1111

1212
/**
1313
* Base {@link android.app.Fragment} class for every fragment in this application.
1414
*/
1515
public abstract class BaseFragment extends Fragment {
16-
17-
@Override public void onCreate(Bundle savedInstanceState) {
18-
super.onCreate(savedInstanceState);
19-
setRetainInstance(true);
20-
}
21-
2216
/**
2317
* Shows a {@link android.widget.Toast} message.
2418
*
@@ -33,6 +27,6 @@ protected void showToastMessage(String message) {
3327
*/
3428
@SuppressWarnings("unchecked")
3529
protected <C> C getComponent(Class<C> componentType) {
36-
return componentType.cast(((HasComponent<C>)getActivity()).getComponent());
30+
return componentType.cast(((HasComponent<C>) getActivity()).getComponent());
3731
}
3832
}

presentation/src/main/java/com/fernandocejas/android10/sample/presentation/view/fragment/UserDetailsFragment.java

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -43,30 +43,35 @@ public class UserDetailsFragment extends BaseFragment implements UserDetailsView
4343
@Bind(R.id.rl_retry) RelativeLayout rl_retry;
4444
@Bind(R.id.bt_retry) Button bt_retry;
4545

46-
public UserDetailsFragment() { super(); }
46+
public UserDetailsFragment() {
47+
setRetainInstance(true);
48+
}
4749

48-
public static UserDetailsFragment newInstance(int userId) {
50+
public static UserDetailsFragment create(int userId) {
4951
UserDetailsFragment userDetailsFragment = new UserDetailsFragment();
50-
5152
Bundle argumentsBundle = new Bundle();
5253
argumentsBundle.putInt(ARGUMENT_KEY_USER_ID, userId);
5354
userDetailsFragment.setArguments(argumentsBundle);
54-
5555
return userDetailsFragment;
5656
}
5757

58+
@Override public void onCreate(Bundle savedInstanceState) {
59+
super.onCreate(savedInstanceState);
60+
this.getComponent(UserComponent.class).inject(this);
61+
}
62+
5863
@Override public View onCreateView(LayoutInflater inflater, ViewGroup container,
5964
Bundle savedInstanceState) {
60-
61-
View fragmentView = inflater.inflate(R.layout.fragment_user_details, container, false);
65+
final View fragmentView = inflater.inflate(R.layout.fragment_user_details, container, false);
6266
ButterKnife.bind(this, fragmentView);
63-
6467
return fragmentView;
6568
}
6669

67-
@Override public void onActivityCreated(Bundle savedInstanceState) {
68-
super.onActivityCreated(savedInstanceState);
69-
this.initialize();
70+
@Override public void onViewCreated(View view, Bundle savedInstanceState) {
71+
super.onViewCreated(view, savedInstanceState);
72+
this.userDetailsPresenter.setView(this);
73+
this.userId = getArguments().getInt(ARGUMENT_KEY_USER_ID);
74+
this.userDetailsPresenter.initialize(this.userId);
7075
}
7176

7277
@Override public void onResume() {
@@ -89,13 +94,6 @@ public static UserDetailsFragment newInstance(int userId) {
8994
this.userDetailsPresenter.destroy();
9095
}
9196

92-
private void initialize() {
93-
this.getComponent(UserComponent.class).inject(this);
94-
this.userDetailsPresenter.setView(this);
95-
this.userId = getArguments().getInt(ARGUMENT_KEY_USER_ID);
96-
this.userDetailsPresenter.initialize(this.userId);
97-
}
98-
9997
@Override public void renderUser(UserModel user) {
10098
if (user != null) {
10199
this.iv_cover.setImageUrl(user.getCoverUrl());

presentation/src/main/java/com/fernandocejas/android10/sample/presentation/view/fragment/UserListFragment.java

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/**
22
* Copyright (C) 2014 android10.org. All rights reserved.
3+
*
34
* @author Fernando Cejas (the android10 coder)
45
*/
56
package com.fernandocejas.android10.sample.presentation.view.fragment;
@@ -51,7 +52,9 @@ public interface UserListListener {
5152

5253
private UserListListener userListListener;
5354

54-
public UserListFragment() { super(); }
55+
public UserListFragment() {
56+
setRetainInstance(true);
57+
}
5558

5659
@Override public void onAttach(Activity activity) {
5760
super.onAttach(activity);
@@ -60,19 +63,22 @@ public interface UserListListener {
6063
}
6164
}
6265

66+
@Override public void onCreate(Bundle savedInstanceState) {
67+
super.onCreate(savedInstanceState);
68+
this.getComponent(UserComponent.class).inject(this);
69+
}
70+
6371
@Override public View onCreateView(LayoutInflater inflater, ViewGroup container,
6472
Bundle savedInstanceState) {
65-
66-
View fragmentView = inflater.inflate(R.layout.fragment_user_list, container, true);
73+
final View fragmentView = inflater.inflate(R.layout.fragment_user_list, container, false);
6774
ButterKnife.bind(this, fragmentView);
6875
setupUI();
69-
7076
return fragmentView;
7177
}
7278

73-
@Override public void onActivityCreated(Bundle savedInstanceState) {
74-
super.onActivityCreated(savedInstanceState);
75-
this.initialize();
79+
@Override public void onViewCreated(View view, Bundle savedInstanceState) {
80+
super.onViewCreated(view, savedInstanceState);
81+
this.userListPresenter.setView(this);
7682
this.loadUserList();
7783
}
7884

@@ -96,11 +102,6 @@ public interface UserListListener {
96102
ButterKnife.unbind(this);
97103
}
98104

99-
private void initialize() {
100-
this.getComponent(UserComponent.class).inject(this);
101-
this.userListPresenter.setView(this);
102-
}
103-
104105
private void setupUI() {
105106
this.usersLayoutManager = new UsersLayoutManager(getActivity());
106107
this.rv_users.setLayoutManager(usersLayoutManager);
@@ -130,7 +131,7 @@ private void setupUI() {
130131

131132
@Override public void renderUserList(Collection<UserModel> userModelCollection) {
132133
if (userModelCollection != null) {
133-
this.usersAdapter.setUsersCollection(userModelCollection);
134+
this.usersAdapter.setUsersCollection(userModelCollection);
134135
}
135136
}
136137

@@ -162,9 +163,9 @@ private void loadUserList() {
162163
private UsersAdapter.OnItemClickListener onItemClickListener =
163164
new UsersAdapter.OnItemClickListener() {
164165
@Override public void onUserItemClicked(UserModel userModel) {
165-
if (UserListFragment.this.userListPresenter != null && userModel != null) {
166-
UserListFragment.this.userListPresenter.onUserClicked(userModel);
167-
}
166+
if (UserListFragment.this.userListPresenter != null && userModel != null) {
167+
UserListFragment.this.userListPresenter.onUserClicked(userModel);
168+
}
168169
}
169170
};
170171
}

presentation/src/main/res/layout/activity_user_list.xml

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,10 @@
33
android:layout_height="match_parent"
44
>
55

6-
<fragment
7-
android:name="com.fernandocejas.android10.sample.presentation.view.fragment.UserListFragment"
8-
android:id="@+id/fragmentUserList"
6+
<FrameLayout
7+
android:id="@+id/fragmentContainer"
98
android:layout_width="match_parent"
109
android:layout_height="match_parent"
1110
/>
1211

13-
</RelativeLayout>
12+
</RelativeLayout>
Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,12 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
33
android:layout_width="match_parent"
4-
android:layout_height="match_parent"
5-
>
4+
android:layout_height="match_parent">
65

7-
<include
8-
layout="@layout/view_user_details"
9-
/>
6+
<include layout="@layout/view_user_details" />
107

11-
<include
12-
layout="@layout/view_progress"
13-
/>
8+
<include layout="@layout/view_progress" />
149

15-
<include
16-
layout="@layout/view_retry"
17-
/>
10+
<include layout="@layout/view_retry" />
1811

19-
</RelativeLayout>
12+
</RelativeLayout>

0 commit comments

Comments
 (0)