Android refresh current activity
AndroidAndroid Problem Overview
I want to program my android app to refresh its current activity on ButtonClick. I have one button on the top of the activity layout which will do the job. When I click on button the current activity should reload again - just like a device restart.
Thanks
Android Solutions
Solution 1 - Android
public void onClick (View v){
Intent intent = getIntent();
finish();
startActivity(intent);
}
Solution 2 - Android
You may try this
finish();
startActivity(getIntent());
This question was asked before: https://stackoverflow.com/questions/1397361/how-do-i-restart-an-android-activity
Solution 3 - Android
In an activity you can call recreate()
to "recreate" the activity
(API 11+)
Solution 4 - Android
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();
}
Solution 5 - Android
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);
}
}
Solution 6 - Android
> 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.
Solution 7 - Android
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);
Solution 8 - Android
You can use:
startActivity(MyClass.this, MyClass.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
Solution 9 - Android
You can use this to refresh an Activity from within itself.
finish();
startActivity(getIntent());
Solution 10 - Android
The easiest way is to call onCreate(null);
and your activity will be like new.
Solution 11 - Android
Try this
Simple way
Intent intent=new Intent(Current_Activity.this,Current_Activity.class);
startActivity(intent);
finish();
Solution 12 - Android
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
Solution 13 - Android
You can call this method:
recreate();
Solution 14 - Android
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);
}