In my application I have a activity that displays contents from internet..... I just want to know how can I auto refresh the activity.....
Please suggest and provide some code block if possible.
I can use this intent to refresh the activity currently: Intent refresh = new Intent(this, Favorites. class); startActivity(refresh); this. finish();
In some situations, we need to recall activity again from onCreate(). This example demonstrates how to reload activity in Android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main.
ServiceLoader. reload() method clears this loader's provider cache so that all providers will be reloaded. After invoking this method, subsequent invocations of the iterator method will lazily look up and instantiate providers from scratch, just as is done by a newly-created loader.
You can use handler to do a loop process, like this:
Handler handler = new Handler();
Runnable refresh;
In the first call time:
refresh = new Runnable() {
public void run() {
// Do something
handler.postDelayed(refresh, 5000);
}
};
handler.post(refresh);
Since you cannot call a non-final variable inside an annonymous class, you will have to declare refresh
in the containing class.
try this one, it works well :)
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
this.mHandler = new Handler();
this.mHandler.postDelayed(m_Runnable,5000);
}//onCreate
private final Runnable m_Runnable = new Runnable()
{
public void run()
{
Toast.makeText(refresh.this,"in runnable",Toast.LENGTH_SHORT).show();
refresh.this.mHandler.postDelayed(m_Runnable, 5000);
}
};//runnable
@Override
protected void onPause() {
super.onPause();
mHandler.removeCallbacks(m_Runnable);
finish();
}
/*Above method needs to be there otherwise activity will be updating itself again and again even if the activity is paused i.e. back button or home button is pressed*/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With