refresh fragment UI from fragmentActivity

Dr.jacky

I want to change a ImageView in fragment(that create in code), from FragmentActivity. (there is no fragment and tag in XML).

MainActivity that extends from FragmentActivity and implements from GooglePlayServicesClient.ConnectionCallbacks:

...
FragmentAdapter mAdapter;
ViewPager mPager;
...

public void onCreate(Bundle savedInstanceState){
    ...
    FragmentManager fm = getSupportFragmentManager();
    mAdapter = new FragmentAdapter(fm);
    mPager.setAdapter(mAdapter);
    ...}

@Override
public void onConnected(Bundle connectionHint){
//*** I want to change a ImageView on Fragment1 here
}

FragmentAdapter that extends from FragmentPagerAdapter:

...
private FragmentManager mFragmentManager;

public FragmentAdapter(FragmentManager fm){
    super(fm);
}

@Override
public Fragment getItem(int pos){
// String name = makeFragmentName(R.id.mainPager, pos);
// Fragment fragmentObject = mFragmentManager.findFragmentByTag(name);
Fragment fragmentObject = null;

switch(pos){
    case 0:
        fragmentObject = new Fragment1();
        break;
    ...
}

return fragmentObject;
}
...

//private static String makeFragmentName(int viewId, iont index){
    //return "android:switcher:" + viewId + ":" + index;
//}

According to this topic for change a element in fragment, from fragmentActivity:

Fragment1 that extends from Fragment:

private ImageView IMGThisImageShouldChange = null;

public View onCreateView(...){
    IMGThisImageShouldChange = (ImageView)v.findViewById(R.id.TargetImage);
    ...
}

public void changeImageView(int resourceId){
    IMGThisImageShouldChange.setImageResource(resouirceId);
}

A comment line in this topic says:

//Do not create new object each time you set text. Use the same fragment object which you use for view pager.

So how can i access to fragmentObject from MainActivity, to call changeImageView method?

If i should find fragment via tag, according to this topic, where i should set tag and how to find?

johng

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Get id of TextView in Fragment from FragmentActivity in ViewPager

From Dev

Fragment transaction with a FragmentActivity instead of a Fragment

From Dev

How to Refresh Fragment from Action Bar Menu

From Dev

refresh a fragment from its parent activity

From Dev

Call FragmentActivity from Fragment

From Dev

fragment run before fragmentActivity

From Dev

refresh fragment UI from fragmentActivity

From Dev

How to call fragment from activity without using fragmentactivity?

From Dev

Import text from fragmentactivity to fragment

From Dev

Android - How to access a Fragment from a FragmentActivity

From Dev

Refresh or Update the listadapter from a fragment in android eclipse

From Dev

How to refresh a fragment from an activity in android?

From Dev

Refresh fragment from adapter

From Dev

Android FragmentActivity How to refresh fragment when it become visible

From Dev

How to Refresh Fragment from Action Bar Menu

From Dev

refresh a fragment from its parent activity

From Dev

Refresh a TextView from inside a fragment

From Dev

(Android) how to call method in fragment from fragmentActivity

From Dev

How to call a fragment layout from the FragmentActivity to avoid NullPointerException

From Dev

Android - Back button from FragmentActivity to another Fragment

From Dev

Android problems with fragment and fragmentActivity

From Dev

Fragment being launched from FragmentActivity not showing up

From Dev

Sending a string from FragmentActivity to Fragment through FragmentPagerAdaptor

From Dev

How to get a Fragment instance from FragmentActivity which contains a ViewPager for fragment transactions?

From Dev

How to pass parameter to Fragment from FragmentActivity

From Dev

call method from fragment to fragment ( refresh adapter )

From Dev

Open new Fragment from FragmentActivity

From Dev

Refresh Fragment in FragmentActivity

From Dev

How to refresh fragment B from fragment A?

Related Related

  1. 1

    Get id of TextView in Fragment from FragmentActivity in ViewPager

  2. 2

    Fragment transaction with a FragmentActivity instead of a Fragment

  3. 3

    How to Refresh Fragment from Action Bar Menu

  4. 4

    refresh a fragment from its parent activity

  5. 5

    Call FragmentActivity from Fragment

  6. 6

    fragment run before fragmentActivity

  7. 7

    refresh fragment UI from fragmentActivity

  8. 8

    How to call fragment from activity without using fragmentactivity?

  9. 9

    Import text from fragmentactivity to fragment

  10. 10

    Android - How to access a Fragment from a FragmentActivity

  11. 11

    Refresh or Update the listadapter from a fragment in android eclipse

  12. 12

    How to refresh a fragment from an activity in android?

  13. 13

    Refresh fragment from adapter

  14. 14

    Android FragmentActivity How to refresh fragment when it become visible

  15. 15

    How to Refresh Fragment from Action Bar Menu

  16. 16

    refresh a fragment from its parent activity

  17. 17

    Refresh a TextView from inside a fragment

  18. 18

    (Android) how to call method in fragment from fragmentActivity

  19. 19

    How to call a fragment layout from the FragmentActivity to avoid NullPointerException

  20. 20

    Android - Back button from FragmentActivity to another Fragment

  21. 21

    Android problems with fragment and fragmentActivity

  22. 22

    Fragment being launched from FragmentActivity not showing up

  23. 23

    Sending a string from FragmentActivity to Fragment through FragmentPagerAdaptor

  24. 24

    How to get a Fragment instance from FragmentActivity which contains a ViewPager for fragment transactions?

  25. 25

    How to pass parameter to Fragment from FragmentActivity

  26. 26

    call method from fragment to fragment ( refresh adapter )

  27. 27

    Open new Fragment from FragmentActivity

  28. 28

    Refresh Fragment in FragmentActivity

  29. 29

    How to refresh fragment B from fragment A?

HotTag

Archive