4

I want to refresh the activity as i want thatwithout firing any event some work gets performed and activity calls by itself. So, i want to know is there any option in android to refresh the activity by itself.

Mathias Conradt
  • 28,420
  • 21
  • 138
  • 192
Nikki
  • 3,314
  • 13
  • 36
  • 59
  • 1
    If you are using BaseAdapter or ArrayAdapter simply call notifyDataSetChanged() to update your view – 100rabh Nov 30 '10 at 06:43
  • @100rabh : right. if he is refreshing a list. he should write, what and how he is actually trying to refresh. than we could give better information. – Patrick Boos Nov 30 '10 at 07:04
  • well i am trying to refresh whole activity – Nikki Nov 30 '10 at 07:06
  • Chk this out: http://stackoverflow.com/questions/6134832/auto-refresh-the-activity/6840666#6840666 – Syed Jul 27 '11 at 07:26

3 Answers3

7

You can do this by yourself through a Handler on which you call postDelayed(..)

http://developer.android.com/reference/android/os/Handler.html#postDelayed(java.lang.Runnable, long)

Put this in your class:

private final Handler handler = new Handler();

make a function called: doTheAutoRefresh() that does:

private void doTheAutoRefresh() {
    handler.postDelayed(new Runnable() {
             @Override
             public void run() {
                 doRefreshingStuff(); // this is where you put your refresh code
                 doTheAutoRefresh();                
             }
         }, 1000);
}

Call this function in your onCreate.

NOTE: this is the basic approach. consider stopping this after onPause has been called and to resume it after onResume. Look at the handler class to see how to remove.

Patrick Boos
  • 6,789
  • 3
  • 35
  • 36
1

You can create a thread and and call the refresh() with the task you want to refresh

-1

for other questions I've pulled the most effective ways to do this are:

finish();startActivity(getIntent());

OR

// Refresh main activity upon close of dialog box
Intent refresh =new Intent(this, ActivityWeAreIn.class);
startActivity(refresh);

Note: this also works with Activity objects or from Fragments using getActivity()