Skip to content

Commit 6531a35

Browse files
committed
fix a data syncing bug. wasn't unregistering receivers
1 parent 8f55032 commit 6531a35

File tree

6 files changed

+51
-37
lines changed

6 files changed

+51
-37
lines changed

android/src/main/java/com/hackthenorth/android/base/BaseListFragment.java

Lines changed: 41 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import android.content.Intent;
77
import android.content.IntentFilter;
88
import android.support.v4.content.LocalBroadcastManager;
9-
import android.widget.ArrayAdapter;
109

1110
import java.util.concurrent.ConcurrentHashMap;
1211

@@ -17,6 +16,9 @@ public abstract class BaseListFragment extends Fragment {
1716
private static ConcurrentHashMap<String, Object> mMemoryCache =
1817
new ConcurrentHashMap<String, Object>();
1918

19+
private BroadcastReceiver mReceiver;
20+
private Context mContext;
21+
2022
public static Object getCachedObject(String f) {
2123
return mMemoryCache.get(f);
2224
}
@@ -25,39 +27,51 @@ public static void setCachedObject(String f, Object o) {
2527
mMemoryCache.put(f, o);
2628
}
2729

28-
protected abstract void handleJSONUpdateInBackground(String json);
30+
protected abstract void handleJSONUpdateInBackground(String json, String action);
2931

3032
/**
31-
* Creates a BroadcastReceiver that is listening to broadcasts of the type action.
33+
* Given an action string, the handleJSONUpdateInBackground method will be called whenever there
34+
* is an update.
3235
*
33-
* @param context Used to set up the LocalBroadcastReceiver. Must be non-null.
34-
* @param action The action to listen to in the broadcast receiver.
35-
* @param adapter The adapter to pass to handleJSONInBackground in onReceive of the
36-
* BroadcastReceiver.
36+
* @param context The current Fragment context.
37+
* @param action The action to sync to. This is the same action string you would pass to
38+
* HTTPFirebase.GET().
3739
*/
38-
protected void registerForSync(Context context, final String action,
39-
final ArrayAdapter adapter) {
40-
41-
// Set up BroadcastReceiver for updates.
42-
BroadcastReceiver receiver = new BroadcastReceiver() {
43-
@Override
44-
public void onReceive(Context context, Intent intent) {
45-
if (action != null && action.equals(intent.getAction())) {
46-
47-
// Update with the new data
48-
String key = intent.getAction();
49-
String json = intent.getStringExtra(key);
50-
handleJSONUpdateInBackground(json);
40+
protected void registerForSync(Context context, final String action) {
41+
42+
final String className = getClass().getSimpleName();
43+
final String TAG = className;
44+
if (mReceiver == null) {
45+
// Set up BroadcastReceiver for updates.
46+
mContext = context;
47+
mReceiver = new BroadcastReceiver() {
48+
@Override
49+
public void onReceive(Context context, Intent intent) {
50+
if (action != null && action.equals(intent.getAction())) {
51+
52+
// Update with the new data
53+
String key = intent.getAction();
54+
String json = intent.getStringExtra(key);
55+
handleJSONUpdateInBackground(json, action);
56+
}
5157
}
52-
}
53-
};
58+
};
5459

55-
// Register our broadcast receiver.
56-
IntentFilter filter = new IntentFilter();
57-
filter.addAction(action);
60+
// Register our broadcast receiver.
61+
IntentFilter filter = new IntentFilter();
62+
filter.addAction(action);
63+
64+
LocalBroadcastManager manager = LocalBroadcastManager.getInstance(mContext);
65+
manager.registerReceiver(mReceiver, filter);
66+
}
67+
}
5868

59-
LocalBroadcastManager manager = LocalBroadcastManager.getInstance(context);
60-
manager.registerReceiver(receiver, filter);
69+
@Override
70+
public void onPause() {
71+
super.onPause();
6172

73+
// Unregister the broadcast receiver here
74+
LocalBroadcastManager manager = LocalBroadcastManager.getInstance(mContext);
75+
manager.unregisterReceiver(mReceiver);
6276
}
6377
}

android/src/main/java/com/hackthenorth/android/ui/MentorsFragment.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public void onAttach(Activity activity) {
4242
mAdapter = new MentorListAdapter(activity, R.layout.mentor_list_item, mData);
4343

4444
// Register for updates
45-
registerForSync(activity, HackTheNorthApplication.Actions.SYNC_MENTORS, mAdapter);
45+
registerForSync(activity, HackTheNorthApplication.Actions.SYNC_MENTORS);
4646

4747
HTTPFirebase.GET("/mentors", activity, HackTheNorthApplication.Actions.SYNC_MENTORS);
4848
}
@@ -86,7 +86,7 @@ public void endSearch() {
8686
}
8787

8888
@Override
89-
protected void handleJSONUpdateInBackground(final String json) {
89+
protected void handleJSONUpdateInBackground(final String json, String action) {
9090
final Activity activity = getActivity();
9191
new AsyncTask<Void, Void, Void>() {
9292
@Override

android/src/main/java/com/hackthenorth/android/ui/PrizesFragment.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public void onAttach(Activity activity) {
5757
mAdapter = new PrizesFragmentAdapter(activity, R.layout.prizes_list_item, mData);
5858
mAdapter.setFragment(this);
5959

60-
registerForSync(activity, HackTheNorthApplication.Actions.SYNC_PRIZES, mAdapter);
60+
registerForSync(activity, HackTheNorthApplication.Actions.SYNC_PRIZES);
6161

6262
HTTPFirebase.GET("/prizes", activity,
6363
HackTheNorthApplication.Actions.SYNC_PRIZES);
@@ -87,7 +87,7 @@ public void onResume() {
8787
}
8888

8989
@Override
90-
protected void handleJSONUpdateInBackground(final String json) {
90+
protected void handleJSONUpdateInBackground(final String json, String action) {
9191
final Activity activity = getActivity();
9292
new AsyncTask<Void, Void, Void>(){
9393
@Override

android/src/main/java/com/hackthenorth/android/ui/ScheduleFragment.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public void onAttach(Activity activity) {
6969
mAdapter.setFragment(this);
7070

7171
// Register for updates
72-
registerForSync(activity, HackTheNorthApplication.Actions.SYNC_SCHEDULE, mAdapter);
72+
registerForSync(activity, HackTheNorthApplication.Actions.SYNC_SCHEDULE);
7373

7474
HTTPFirebase.GET("/schedule", activity,
7575
HackTheNorthApplication.Actions.SYNC_SCHEDULE);
@@ -113,7 +113,7 @@ public void onNegativeClick(ConfirmDialogFragment fragment) {
113113
}
114114

115115
@Override
116-
protected void handleJSONUpdateInBackground(final String json) {
116+
protected void handleJSONUpdateInBackground(final String json, String actio) {
117117
final Activity activity = getActivity();
118118
new AsyncTask<Void, Void, Void>() {
119119
@Override

android/src/main/java/com/hackthenorth/android/ui/TeamFragment.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public void onAttach(Activity activity) {
5555
mAdapter = new TeamFragmentAdapter(activity, R.layout.team_list_item, mData);
5656

5757
// Register for updates
58-
registerForSync(activity, HackTheNorthApplication.Actions.SYNC_TEAM, mAdapter);
58+
registerForSync(activity, HackTheNorthApplication.Actions.SYNC_TEAM);
5959

6060
HTTPFirebase.GET("/team", activity, HackTheNorthApplication.Actions.SYNC_TEAM);
6161
}
@@ -78,7 +78,7 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container,
7878
}
7979

8080
@Override
81-
protected void handleJSONUpdateInBackground(final String json) {
81+
protected void handleJSONUpdateInBackground(final String json, String action) {
8282
final Activity activity = getActivity();
8383
new AsyncTask<Void, Void, Void>(){
8484
@Override

android/src/main/java/com/hackthenorth/android/ui/UpdatesFragment.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public void onAttach(Activity activity) {
5959
mAdapter = new UpdatesFragmentAdapter(activity, R.layout.update_list_item, mData);
6060

6161
// Register for updates
62-
registerForSync(activity, HackTheNorthApplication.Actions.SYNC_UPDATES, mAdapter);
62+
registerForSync(activity, HackTheNorthApplication.Actions.SYNC_UPDATES);
6363

6464
HTTPFirebase.GET("/updates", activity,
6565
HackTheNorthApplication.Actions.SYNC_UPDATES);
@@ -86,7 +86,7 @@ public void onResume() {
8686
}
8787

8888
@Override
89-
protected void handleJSONUpdateInBackground(final String json) {
89+
protected void handleJSONUpdateInBackground(final String json, String action) {
9090
final Activity activity = getActivity();
9191
new AsyncTask<Void, Void, Void>() {
9292
@Override

0 commit comments

Comments
 (0)