Reload activity in Android
You can Simply use
finish();
startActivity(getIntent());
to refresh an Activity
from within itself.
Reload activity from same activity
This is the best way to refresh your activity:
public void refresh() {
Intent intent = getIntent();
overridePendingTransition(0, 0);
intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
finish();
overridePendingTransition(0, 0);
startActivity(intent);
}
EDIT 06.11.2021
Kotlin way to refresh activity
private fun refresh() {
val intent = Intent(applicationContext, YourActivity::class.java)
startActivity(intent)
finish()
}
How to reload same activity with giving data with intent?
Try the following steps...
Refresh
RestaurantDetailActivity
Intent intent = new Intent(RestaurantDetailActivity.this, RestaurantDetailActivity.class);
Bundle bundle = new Bundle();
bundle.putInt("restaurant_id", mDataset.get(position).getRestaurantId());
intent.putExtras(bundle);
startActivity(intent);
RestaurantDetailActivity.this.finish();In
OnCreate()
you should do like this in the same activityif(getIntent().getExtras() != null) {
Bundle extras = getIntent().getExtras();
int restaurantId = extras.getInt("restaurant_id");
// todo with restaurantId
}
reload activity without close app in android
hi you can do startActivity after finish;
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.item_delete:
// refrash page
if(db.getAllDataBuyit().size()>0) {
db.RemoveBuy_AllItem();
/////////////////////////
finish();
startActivity(getIntent());
/////////////////////////
}
return true;
default:
}
return super.onOptionsItemSelected(item);
}
Android Studio refresh the activity and display the current fragment
Instead of start new with same Activity you should call recreate
and inside onCreate
method you have to check savedInstanceState == null
before init default fragment
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
Configuration configuration = new Configuration();
Intent intent = new Intent();
switch (item.getItemId()){
case R.id.action_polski:
Locale localePl = new Locale("pl");
Locale.setDefault(localePl);
configuration.locale = localePl;
getBaseContext().getResources().updateConfiguration(configuration,getBaseContext().getResources().getDisplayMetrics());
recreate()
break;
case R.id.action_english:
Locale localeEn = new Locale("en");
Locale.setDefault(localeEn);
configuration.locale = localeEn;
getBaseContext().getResources().updateConfiguration(configuration,getBaseContext().getResources().getDisplayMetrics());
recreate()
break;
}
return true;
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
if (savedInstanceState == null) {
// init default fragment
}
}
Related Topics
How Do Detect Android Tablets in General. Useragent
How to Open Email Program via Intents (But Only an Email Program)
Android Ci Build: Could Not Find Aapt2-Proto.Jar
Scrollable Content Inside an HTML Does Not Scroll in Android Emulator or Phone
How to Get Text on an Actionbar Icon
Passing Data of a Non-Primitive Type Between Activities in Android
How to Convert Views to Bitmaps
Edittext Loses Content on Scroll in Listview
How to Remove Lines Between Listviews on Android
Xamarin.Forms Listview: Set the Highlight Color of a Tapped Item
Version Conflict Updating to Play-Services 9.4.0 Android Studio 2.2
How to Create Spinner-List Using Customadapter in Android
HTML Email Looking Different in Gmail App
Android Automatic Horizontally Scrolling Textview