Refresh Fragment UI from Fragmentactivity

refresh fragment UI from fragmentActivity

So obviously adjust for your specific needs, but this is what I use for my Application.

 Fragment frag = getSupportFragmentManager().findFragmentByTag("android:switcher:" + R.id.pager + ":" + mPager.getCurrentItem());
if(frag!=null&&mPager.getCurrentItem()==0)
{
((Fragment1) frag).changeImageView(1);
}

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 a fragment from its parent activity

You can find your fragment by the Tag, but of course you need to give this tag to it while adding the fragment.

First add your fragment with a tag:

        fragmentManager = getFragmentManager();
fragmentTransaction = fragmentManager.beginTransaction();
SomeFragment fragment = new ManageLinksFragment();
fragmentTransaction.add(R.id.fragment_container1,fragment, "sometag");
fragmentTransaction.commit();

And then on the Activity's site:

SomeFragment mSomeFragment = (SomeFragment) getFragmentManager().findFragmentByTag("sometag");
// now mSomeFragment.callsomething();

Method to refresh Fragment content when data changed ( like recall onCreateView)

Detach and attach it with

Fragment currentFragment = getFragmentManager().findFragmentByTag("YourFragmentTag");
FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
fragmentTransaction.detach(currentFragment);
fragmentTransaction.attach(currentFragment);
fragmentTransaction.commit();

or search fragment with

Fragment currentFragment = getActivity().getSupportFragmentManager().findFragmentById(R.id.container);

Android, How to restart/refresh a fragment from FragmentActivty?

Executing this

onRefreshSelected = (OnRefreshSelected) MainScreen.this;

you are assigning your activity in onRefreshSelected field, trying to cast it to your interface, but your activity doesn't implement the interface, that's why ClassCastException raised.

Instead use something like this

    @Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_refresh:
CategoryFragment fragment = (CategoryFragment) getFragmentManager()
.findFragmentById(R.id.category_fragment);
if (fragment != null) {
fragment.getData();
}
return true;
default:
return super.onOptionsItemSelected(item);
}

Your activity can call methods in the fragment by acquiring a reference to the Fragment from FragmentManager, using findFragmentById() or findFragmentByTag().

Reload Fragment

Create a method that begins a FragmentTransaction, detaches the fragment, and commits. Then begin a new FragmentTransaction which attachs the fragment and commits. This should destroy the view hierarchy, create it again, attach it to the UI and display it. Whenever you need the fragment refreshed, jsut call this method.

Not able to update Fragment UI from onActivityResult

...but when I click on the button I'm Calling startActivityForResult()
which in turn receives results in OnActivityResult() in which I'm
trying to update the text of EditText , but it is not happening. When
I clicked back button I'm able to see the text on EditText changed

I think this is happening because of the use of LoadScreenHelper in the onResume method of the FragmentActivity. Keep in mind that onResume will always be called when the activity comes to the foregound, this will also happen after the Activity started with startActivityForResult will return. Now by calling LoadScreenHelper's loadTargetScreen() method, you'll always add a new MultiFormScreenFragment to the initial FragmentActivity. When you come back from the child Activity the onResume method will be called again and a new MultiFormScreenFragment will be created most likely covering the initially added fragment. If you click the back button this top fragment will be removed from the screen leaving the initially added fragment.

As I don't know what you're trying to do in the end with the LoadScreenHelper class I would suggest to either move the line:

new LoadScreenHelper((FragmentActivity)context, R.id.fragment_container).loadTargetScreen("", 1,"",1,false,"","","");

in the onCreate method so it's executed only once, or insert a check in the loadTargetScreen method to find out if an instance of the MultiFormScreenFragment isn't already in the layout(use the FragmentManager to find the fragment). If it is already in the layout then do nothing else add a new instance of the fragment.



Related Topics



Leave a reply



Submit