Stop listening for broadcasts when view not shown.
This commit is contained in:
parent
7b77919432
commit
385a16448c
@ -39,6 +39,22 @@ class MainViewAdapter extends RecyclerView.Adapter<MainViewController> {
|
||||
positionToId.put(4, R.id.settings);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onViewDetachedFromWindow(MainViewController holder) {
|
||||
long viewType = getItemId(holder.getAdapterPosition());
|
||||
if (viewType == R.id.updates) {
|
||||
holder.unbindUpdates();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onViewAttachedToWindow(MainViewController holder) {
|
||||
long viewType = getItemId(holder.getAdapterPosition());
|
||||
if (viewType == R.id.updates) {
|
||||
holder.bindUpdates();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public MainViewController onCreateViewHolder(ViewGroup parent, int viewType) {
|
||||
MainViewController holder = createEmptyView();
|
||||
|
@ -1,6 +1,7 @@
|
||||
package org.fdroid.fdroid.views.main;
|
||||
|
||||
import android.content.Intent;
|
||||
import android.support.annotation.Nullable;
|
||||
import android.support.v4.app.Fragment;
|
||||
import android.support.v7.app.AppCompatActivity;
|
||||
import android.support.v7.widget.RecyclerView;
|
||||
@ -24,6 +25,9 @@ class MainViewController extends RecyclerView.ViewHolder {
|
||||
private final AppCompatActivity activity;
|
||||
private final FrameLayout frame;
|
||||
|
||||
@Nullable
|
||||
private UpdatesViewBinder updatesView = null;
|
||||
|
||||
MainViewController(AppCompatActivity activity, FrameLayout frame) {
|
||||
super(frame);
|
||||
this.activity = activity;
|
||||
@ -41,7 +45,17 @@ class MainViewController extends RecyclerView.ViewHolder {
|
||||
* @see UpdatesViewBinder
|
||||
*/
|
||||
public void bindUpdates() {
|
||||
new UpdatesViewBinder(activity, frame);
|
||||
if (updatesView == null) {
|
||||
updatesView = new UpdatesViewBinder(activity, frame);
|
||||
}
|
||||
|
||||
updatesView.bind();
|
||||
}
|
||||
|
||||
public void unbindUpdates() {
|
||||
if (updatesView != null) {
|
||||
updatesView.unbind();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -258,6 +258,10 @@ public class UpdatesAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder
|
||||
LocalBroadcastManager.getInstance(activity).registerReceiver(receiverAppStatusChanges, filter);
|
||||
}
|
||||
|
||||
public void stopListeningForStatusUpdates() {
|
||||
LocalBroadcastManager.getInstance(activity).unregisterReceiver(receiverAppStatusChanges);
|
||||
}
|
||||
|
||||
private void onManyAppStatusesChanged(String reasonForChange) {
|
||||
switch (reasonForChange) {
|
||||
case AppUpdateStatusManager.REASON_UPDATES_AVAILABLE:
|
||||
|
@ -10,17 +10,24 @@ import org.fdroid.fdroid.R;
|
||||
|
||||
public class UpdatesViewBinder {
|
||||
|
||||
private final UpdatesAdapter adapter;
|
||||
|
||||
public UpdatesViewBinder(AppCompatActivity activity, FrameLayout parent) {
|
||||
View view = activity.getLayoutInflater().inflate(R.layout.main_tab_updates, parent, true);
|
||||
|
||||
UpdatesAdapter adapter = new UpdatesAdapter(activity);
|
||||
|
||||
// TODO: Find the right time to stop listening for status updates.
|
||||
adapter.listenForStatusUpdates();
|
||||
adapter = new UpdatesAdapter(activity);
|
||||
|
||||
RecyclerView list = (RecyclerView) view.findViewById(R.id.list);
|
||||
list.setHasFixedSize(true);
|
||||
list.setLayoutManager(new LinearLayoutManager(activity));
|
||||
list.setAdapter(adapter);
|
||||
}
|
||||
|
||||
public void bind() {
|
||||
adapter.listenForStatusUpdates();
|
||||
}
|
||||
|
||||
public void unbind() {
|
||||
adapter.stopListeningForStatusUpdates();
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user