开发者

How can I refresh the activity?

开发者 https://www.devze.com 2023-01-27 18:54 出处:网络
I want to refresh the activity as i want thatwithout firing any event some work gets performed and activity callsby itself. So, i want to know i开发者_开发问答s there any option in android to refresh

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 i开发者_开发问答s there any option in android to refresh the activity by itself.


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.


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


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()

0

精彩评论

暂无评论...
验证码 换一张
取 消