14
14
15
15
package com .firebase .uidemo .database ;
16
16
17
+ import android .arch .lifecycle .LifecycleRegistry ;
18
+ import android .arch .lifecycle .LifecycleRegistryOwner ;
17
19
import android .os .Bundle ;
18
20
import android .support .annotation .NonNull ;
19
21
import android .support .v7 .app .AppCompatActivity ;
37
39
import com .google .firebase .database .FirebaseDatabase ;
38
40
import com .google .firebase .database .Query ;
39
41
40
- public class ChatActivity extends AppCompatActivity implements FirebaseAuth .AuthStateListener , View .OnClickListener {
42
+ public class ChatActivity extends AppCompatActivity
43
+ implements FirebaseAuth .AuthStateListener , View .OnClickListener , LifecycleRegistryOwner {
41
44
private static final String TAG = "RecyclerViewDemo" ;
42
45
46
+ // TODO remove once arch components are merged into support lib
47
+ private final LifecycleRegistry mRegistry = new LifecycleRegistry (this );
48
+
43
49
private FirebaseAuth mAuth ;
44
50
protected DatabaseReference mChatRef ;
45
51
private Button mSendButton ;
46
52
protected EditText mMessageEdit ;
47
53
48
54
private RecyclerView mMessages ;
49
55
private LinearLayoutManager mManager ;
50
- protected FirebaseRecyclerAdapter <Chat , ChatHolder > mAdapter ;
56
+ private FirebaseRecyclerAdapter <Chat , ChatHolder > mAdapter ;
51
57
protected TextView mEmptyListMessage ;
52
58
53
59
@ Override
@@ -70,8 +76,10 @@ protected void onCreate(Bundle savedInstanceState) {
70
76
mManager .setReverseLayout (false );
71
77
72
78
mMessages = (RecyclerView ) findViewById (R .id .messagesList );
73
- mMessages .setHasFixedSize (false );
79
+ mMessages .setHasFixedSize (true );
74
80
mMessages .setLayoutManager (mManager );
81
+
82
+ if (isSignedIn ()) { attachRecyclerViewAdapter (); }
75
83
}
76
84
77
85
@ Override
@@ -81,19 +89,7 @@ public void onStart() {
81
89
// Default Database rules do not allow unauthenticated reads, so we need to
82
90
// sign in before attaching the RecyclerView adapter otherwise the Adapter will
83
91
// not be able to read any data from the Database.
84
- if (isSignedIn ()) {
85
- attachRecyclerViewAdapter ();
86
- } else {
87
- signInAnonymously ();
88
- }
89
- }
90
-
91
- @ Override
92
- public void onStop () {
93
- super .onStop ();
94
- if (mAdapter != null ) {
95
- mAdapter .cleanup ();
96
- }
92
+ if (!isSignedIn ()) { signInAnonymously (); }
97
93
}
98
94
99
95
@ Override
@@ -147,7 +143,8 @@ protected FirebaseRecyclerAdapter<Chat, ChatHolder> getAdapter() {
147
143
Chat .class ,
148
144
R .layout .message ,
149
145
ChatHolder .class ,
150
- lastFifty ) {
146
+ lastFifty ,
147
+ this ) {
151
148
@ Override
152
149
public void populateViewHolder (ChatHolder holder , Chat chat , int position ) {
153
150
holder .bind (chat );
@@ -182,4 +179,9 @@ private void updateUI() {
182
179
mSendButton .setEnabled (isSignedIn ());
183
180
mMessageEdit .setEnabled (isSignedIn ());
184
181
}
182
+
183
+ @ Override
184
+ public LifecycleRegistry getLifecycle () {
185
+ return mRegistry ;
186
+ }
185
187
}
0 commit comments