0

I have this basic Preference subclass that I have a Handler in. It will fire every second but it will not stop if the custom Preference is no more visible.

<PreferenceCategory
    android:layout="@layout/pref_category_text"
    android:title="@string/pref_category_stat_out_title">

    <com.sunlux.smartpower.extended.preferences.ShowStatPreference android:key="@string/pref_key_show_stat" />
</PreferenceCategory>


public class ShowStatPreference extends Preference {

    private long MEGABYTE = 1024L * 1024L;
    private TextView bytesStat;
    private Handler mHandler;

    /**
     * Timer for updating stat
     */
    private final Runnable statRunnable = new Runnable() {
        @Override
        public void run() {
            setStatSomething();
            startStatTimer();
        }
    };

    public ShowStatPreference(Context context) {
        super(context);
    }

    public ShowStatPreference(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ShowStatPreference(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        setWidgetLayoutResource(R.layout.show_stat_preference);
        mHandler = new Handler();
        startStatTimer();
    }

    @Override
    public void onBindViewHolder(PreferenceViewHolder holder) {
        holder.itemView.setClickable(false); // disable parent click
        bytesStat = (TextView) holder.findViewById(R.id.bytes);
        super.onBindViewHolder(holder);
    }

    private void startStatTimer() {
        mHandler.postDelayed(statRunnable, 1000);
    }

    private void setStatSomething(){
        // do stuff
    }
}

I could through listeners and send a message to make it stop when I change tab in the PagerAdapter, or if app is closing.

Is this my only chose here?

Any idea?

ישו אוהב אותך
  • 28,609
  • 11
  • 78
  • 96
Tord Larsen
  • 2,670
  • 8
  • 33
  • 76

1 Answers1

0

You can use onDetached() to remove the handler when your setting page is closed. Something like this:

@Override public void onDetached() {
  super.onDetached();
  mHandler.removeCallbacks(statRunnable);
}
ישו אוהב אותך
  • 28,609
  • 11
  • 78
  • 96
  • Thanks that would work but In my 'FragmentPagerAdapter' I have 5 'Fragment' and the 'getCount()' return 5 so the 'PreferenceFragmentCompat' is never removed. Think I will go with greenrobot:eventbus – Tord Larsen Aug 12 '19 at 14:12
  • Is that a dirty solution using greenrobot:eventbus? – Tord Larsen Aug 12 '19 at 14:21
  • Since I have the Handler on delay already set up, when that fire every second it could read som global variable that was set by the 'ViewPager', that will tell if its the current visible 'Fragment' right? – Tord Larsen Aug 12 '19 at 14:26
  • `Is that a dirty solution using greenrobot:eventbus?` yes, your preference will dependent to the EventBus. – ישו אוהב אותך Aug 12 '19 at 15:16
  • `Since I have the Handler on delay already set up, when that fire every second it could read som global variable that was set by the 'ViewPager', that will tell if its the current visible 'Fragment' right?` that should work, but the preference still become dependent to the ViewPager. How about listening to your View visibility changes inside the `onBindViewHolder`? See https://stackoverflow.com/questions/32777597/handle-a-view-visibility-change-without-overriding-the-view – ישו אוהב אותך Aug 12 '19 at 15:17
  • Or just check the View visibility in the handler. – ישו אוהב אותך Aug 12 '19 at 15:18
  • But the 'PreferenceFragmentCompat' is only loaded once, going back and forth between tabs and the 'PreferenceFragmentCompat' is alive, 'onBindViewHolder' is only called once. To test for the View visibility seam to be so much problem with I read – Tord Larsen Aug 12 '19 at 15:36
  • I created a 'getViewTreeObserver' for one of the 'TextViews' on the 'Pref' and when 'onGlobalLayout()' fire the 'getVisibility()' is always 'View.VISIBLE' even I go to other tabs in the 'ViewPager'. This must be becauase the Pref Frag is not unloaded – Tord Larsen Aug 12 '19 at 15:48