1

I wan't to refresh a list of website every X Minutes.

After reading this thread: nested postDelayed / Runnable / Handler Android

I decided to use a Runnable + Handler instead of a TimerTask. Unfortunately my Runnable won't run a second time. It will refresh the data once, then nothing happens.

my onCreate() Method looks like this:

...    
fooWebsitesListView = (ListView) findViewById(R.id.fooWebsitesListView);

fooWebsitesAdapter = new ArrayAdapter<fooWebsite>(
    getApplicationContext(), R.layout.list_black_text,
    R.id.list_content, websiteList);

    fooWebsitesListView.setAdapter(fooWebsitesAdapter);
    final fooWebsitesActivity fooWebsitesRefreshActivity = new fooWebsitesActivity();

    mHandler = new Handler();

    refreshfooWebsitesRunnable = new Runnable() {

        @Override
        public void run() {
            // TODO Auto-generated method stub
            Log.d("scheduled task", "---Scheduled Task: Refresh fooWebsites");
            try {
                fooWebsitesRefreshActivity.execute();
                //mHandler.postDelayed(refreshfooWebsitesRunnable,3000);
                mHandler.postDelayed(this, 3000);
            } catch (Exception e) {
                // Toast.makeText(getApplicationContext(),
                // "Unable to receive Data", Toast.LENGTH_SHORT)
                // .show();
            }
        }
    };
    startRepeatingTask();
}

void startRepeatingTask() {
    refreshfooWebsitesRunnable.run();
}

LOG:

12-14 14:56:47.624: D/scheduled task(7995): ---Scheduled Task: Refresh fooWebsites

And there has been no chance since yet. The UI Thread is working fine, the app won't freeze or anything like that. Why won't my runnable loop?

Fábio Nascimento
  • 2,644
  • 1
  • 21
  • 27
Lama
  • 2,886
  • 6
  • 43
  • 59
  • `fooWebsitesRefreshActivity.execute();` is this causing an exception? – JiTHiN Dec 14 '13 at 14:23
  • no. the code is running fine and the runnable will run exactly one time and refresh the UI properly. Ill do some debugging and then update my question. – Lama Dec 14 '13 at 14:49
  • Possible duplicate of [Why my refreshing is slower after few secounds than immediately after start](https://stackoverflow.com/questions/57490798/why-my-refreshing-is-slower-after-few-secounds-than-immediately-after-start) – Peter Staranchuk Aug 14 '19 at 09:24

1 Answers1

1

Try to call mHandler.post() instead of refreshfooWebsitesRunnable.run() and call a mHandler.removeCallbacks() in the beginning of your run() method.

Dmide
  • 6,422
  • 3
  • 24
  • 31
  • Have you debugged/logged it? maybe `run()` is actually executing but problem is in your `fooWebsitesRefreshActivity`? Code of your activity will be helpful. – Dmide Dec 14 '13 at 14:23