How to Refresh the Fragment After Activity Finished in Android

How to refresh the fragment after activity finished in android?

For solving it you can start register Activity like below :

Intent intent = new Intent(getActivity(), register .class);
startActivityForResult(intent, 10001);

And when you finish your activity you should setResult like below :

setResult(Activity.RESULT_OK)

Now in your fragment you will get event of finishing your activity by overriding onActivityResult() in the fragment.

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
if ((requestCode == 10001) && (resultCode == Activity.RESULT_OK))
// recreate your fragment here
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.detach(frag).attach(frag).commit();
}

That's it.

Hope it will help you.

How can I refresh a fragment after coming back from an activity?

One way to solve this is to use startActivityForResult(intent, MyStatusCode) and then capture the return by using something like:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (requestCode == MyStatusCode) {
//Refresh the fragment here
}
}

Reload fragment when activity finish

override onResume in your fragment and notify your adapter

@Override
protected void onResume() {
super.onResume();
manageCmsAdapter.notifyDataSetChanged();
}

when you will go to another activity onPause of the fragment will call and when coming back onResume() will call.So you can notify about data changes in the listview to the adapter

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

How to refresh previous fragment after closing an activity who was started inside an Adapter?

In the activity of your fragment you can do something like this:

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
requestCode &= 0xffff;
for (Fragment fragment : getSupportFragmentManager().getFragments()) {
if (fragment != null) {
fragment.onActivityResult(requestCode, resultCode, data);
}
}
super.onActivityResult(requestCode, resultCode, data);
}

Or you can use a Event bus like: http://square.github.io/otto/

Refresh Fragment at reload

I think you want to refresh the fragment contents upon db update

If so, detach the fragment and reattach it

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

[EDIT]

This method requires the Fragment to have a tag.

In case you don't have it, then @Hammer's method is what you need.

refresh fragment after changing data in database

I think you are having this issue because of the below code in your MainActivity:

FragmentOne fragmentOne = new FragmentOne();
fragmentOne.getFragmentManager().beginTransaction().replace(R.id.card_view, fragmentOne).commit();

You try to get the instance of the fragment manager with getFragmentManager() who call Activity.getFragmentManager but your fragmentOne isn't attach to the activity so the method return null and you get a java.lang.NullPointerException

Instead of fragmentOne.getFragmentManager() use getSupportFragmentManager() directly from the activity.

Hope this helps.

Refresh Fragment From Activity After Updating Data on SQLite Xamarin Android

In you Fragment, you can override the OnResume method. If you back to the fragment from Acitivty. you should query the new data from DB, then set the new value to the controls in Fragment.

 public override void OnResume()
{
base.OnResume();
PHName.Text = photoDAO.GetFirstPhotos(Id).PhotoName;
}

Here is running GIF.

Sample Image

You can see this lifecycle about Fragment. Every time you display the Fragment,OnResume method will be executed. You get the newest data from DB, then set it to the Fragment page.
Sample Image

Here is my demo.

https://drive.google.com/file/d/11dROKS7TtqAaVYkG8w6ZKpqnQJRBD87E/view



Related Topics



Leave a reply



Submit