62

I my Android app to refresh its current activity on ButtonClick.

I have a button on the top of the activity layout which should do the job. When I click on the button, the current activity should reload, just like a device restart.

How can I do that?

Ola Ström
  • 4,136
  • 5
  • 22
  • 41
Android_Code_Chef
  • 905
  • 4
  • 13
  • 20
  • 1
    [for API before 11 you cannot use recreate(). I solved in this way:][1] [1]: http://stackoverflow.com/questions/2486934/programmatically-relaunch-recreate-an-activity – A.A Aug 04 '14 at 13:28
  • it's already answered here with a good approach https://stackoverflow.com/a/6283098/8713823 – Islam Alshnawey Nov 03 '21 at 08:21

14 Answers14

130
public void onClick (View v){
    Intent intent = getIntent();
    finish();
    startActivity(intent);
}
Otra
  • 8,108
  • 3
  • 34
  • 49
  • I had this code with me but it basically shows animation that we are moving from one activity to another. And also my previous activity is still alive. i want everything should be loaded again. – Android_Code_Chef Jul 01 '11 at 12:38
  • 51
    `intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);` – Otra Jul 01 '11 at 12:40
  • That is important thanks @Otra – Hilal Feb 01 '17 at 21:32
  • My listView is not getting refreshed with this method. Why? – Danger Apr 04 '18 at 06:37
  • `Intent intent = new Intent(YourActivity.this,YourActivity.class); finish(); startActivity(intent);` Best Solution it will refresh every thing . ofc if you want to save some data you can use shared Pref before finishing the page @Otra @Shine – Exutic Jun 15 '20 at 14:13
37

You may try this

finish();
startActivity(getIntent());

This question was asked before: How do I restart an Android Activity

Community
  • 1
  • 1
gregory561
  • 14,866
  • 2
  • 23
  • 25
  • I had this code with me but it basically shows animation that we are moving from one activity to another. And also my previous activity is still alive. i want everything should be loaded again. – Android_Code_Chef Jul 01 '11 at 12:38
  • From your own question "It should start again and delete all the instances of previous current activity." That is what @Greg's answer does – Blundell Jul 01 '11 at 12:41
  • If you don't want the activity loading animation. Figure out what you want to refresh and do it manually. – Blundell Jul 01 '11 at 12:41
29

In an activity you can call recreate() to "recreate" the activity (API 11+)

Sudara
  • 4,769
  • 3
  • 34
  • 39
12

This is a refresh button method, but it works well in my application. in finish() you kill the instances

public void refresh(View view){          //refresh is onClick name given to the button
    onRestart();
}

@Override
protected void onRestart() {

    // TODO Auto-generated method stub
    super.onRestart();
    Intent i = new Intent(lala.this, lala.class);  //your class
    startActivity(i);
    finish();

}
edwin rojas
  • 139
  • 2
  • 9
9

this is what worked for me as seen here.

Just use it or add it to a static class helper and just call it from anywher in your project.

/**
Current Activity instance will go through its lifecycle to onDestroy() and a new instance then created after it.
*/
@SuppressLint("NewApi")
  public static final void recreateActivityCompat(final Activity a) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
      a.recreate();
    } else {
      final Intent intent = a.getIntent();
      intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
      a.finish();
      a.overridePendingTransition(0, 0);
      a.startActivity(intent);
      a.overridePendingTransition(0, 0);
   }
}
Williem
  • 1,131
  • 1
  • 12
  • 19
  • after putting this... the activity keeps restarting itself. – Abu Ruqaiyah Jun 02 '16 at 12:09
  • @AbuRuqaiyah make sure you are calling this in an onClickListener or whatever trigger you want. Also make sure you are not calling this function from a loop or a function that might be executed each time your app starts up-- hope you understood the process – Williem Sep 26 '16 at 19:00
  • 1
    Thanks, this is worked for me – Krupa Kakkad Jan 04 '17 at 13:23
4

You can Try this:

        CookieSyncManager.createInstance(this);         
        CookieManager cookieManager = CookieManager.getInstance();        
        cookieManager.removeAllCookie();        
        Intent intent= new Intent(YourCurrent.this, YourCurrent.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
Md. Ilyas Hasan Mamun
  • 1,848
  • 2
  • 24
  • 15
4

It should start again and delete all the instances of previous current activity.

No, it shouldn't.

It should update its data in place (e.g., requery() the Cursor). Then there will be no "instances of previous current activity" to worry about.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Can u pls little more elaborate...In my activity one asynchronus thread is running in background. when i want to reoload this activity this asynchronus thread instance is not deleted and even two instances are created one previous and one new... – Android_Code_Chef Jul 01 '11 at 13:10
  • @Vivek_Android_Developer: You are the one forking the thread. When you refresh your UI, do not start a second thread. – CommonsWare Jul 01 '11 at 13:27
  • I ll check dat..and will let you know.. – Android_Code_Chef Jul 01 '11 at 14:02
  • @CommonsWare But what if you user clicks a stale piece of information in the time between resuming the old activity and updating the cursor backed view? – theblang Jan 15 '14 at 16:28
  • @mattblang: If that is a danger, clear the old data upon resuming, put up a `ProgressBar` or other indicator, then load in the fresh `Cursor` when it becomes available. – CommonsWare Jan 15 '14 at 17:00
3

You can use:

startActivity(MyClass.this, MyClass.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
Pratik Butani
  • 60,504
  • 58
  • 273
  • 437
3

You can use this to refresh an Activity from within itself.

finish();
startActivity(getIntent());
BatyrCan
  • 6,773
  • 2
  • 14
  • 23
1

The easiest way is to call onCreate(null); and your activity will be like new.

JSW189
  • 6,267
  • 11
  • 44
  • 72
Amir Foghel
  • 853
  • 1
  • 12
  • 28
1

Try this

Simple way

 Intent intent=new Intent(Current_Activity.this,Current_Activity.class);
    startActivity(intent);
    finish();
Community
  • 1
  • 1
Sunil
  • 3,785
  • 1
  • 32
  • 43
1

I think that the best choice for you is using SwipeRefreshLayout View in your layout. Then set RefreshListener like.

mySwipeRefreshLayout.setOnRefreshListener(
            new SwipeRefreshLayout.OnRefreshListener() {
                @Override
                public void onRefresh() {
                    Log.i("Fav ", "onRefresh called from SwipeRefreshLayout");

                    // This method performs the actual data-refresh operation.
                    // The method calls setRefreshing(false) when it's finished.
                    request();  // call what you want to update in this method
                    mySwipeRefreshLayout.setRefreshing(false);
                }
            }
    );

For more details click this link

Khalid Ali
  • 373
  • 1
  • 15
1

You can call this method:

recreate();
0

From dialog to activity that you want to refresh. If it not first activity!
Like this:mainActivity>>objectActivity>>dialog
In your dialog class:

  @Override
public void dismiss() {
    super.dismiss();
   getActivity().finish();
    Intent i = new Intent(getActivity(), objectActivity.class);  //your class
    startActivity(i);

}