Android-How To Reload Fragment & Activities

For Fragment Reload

// Reload current fragment
Fragment frg = null;
frg = getSupportFragmentManager().findFragmentByTag("Your_Fragment_TAG");
final FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.detach(frg);
ft.attach(frg);
ft.commit();

Your_Fragment_TAG is the name you gave your fragment when you created it

This code is for support library.

If you're not supporting older devices, just use getFragmentManager instead of getSupportFragmentManager


For Activity Reload

Intent i = new Intent(MainActivity.this, MainActivity.class);
finish();
overridePendingTransition(0, 0);
startActivity(i);
overridePendingTransition(0, 0);

In the above code, we have used overridePendingTransition(), it is used to remove activity create animation while re-creating activity.

Comments

Popular posts from this blog

Build a Responsive UI with ConstraintLayout

How to Download & Install Node.js - NPM on Windows

Android Building web apps in WebView