Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Auto refresh the activity

Tags:

android

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.

like image 816
Parmendra Singh Avatar asked May 26 '11 06:05

Parmendra Singh


People also ask

How do you refresh an activity on a resume?

I can use this intent to refresh the activity currently: Intent refresh = new Intent(this, Favorites. class); startActivity(refresh); this. finish();

How do you reload an activity?

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.

How do you refresh a method in Java?

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.


2 Answers

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.

like image 61
Luke Vo Avatar answered Sep 22 '22 10:09

Luke Vo


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*/
like image 34
Syed Avatar answered Sep 24 '22 10:09

Syed