Reload Activity in Android

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...

  1. 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();
  2. In OnCreate() you should do like this in the same activity

     if(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



Leave a reply



Submit