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

refresh fragment UI from fragmentActivity

From Dev

Refresh Fragment in FragmentActivity

From Dev

Call FragmentActivity from Fragment

From Dev

Import text from fragmentactivity to fragment

From Dev

Open new Fragment from FragmentActivity

From Dev

Android FragmentActivity How to refresh fragment when it become visible

From Dev

Get id of TextView in Fragment from FragmentActivity in ViewPager

From Dev

Android - How to access a Fragment from a FragmentActivity

From Dev

(Android) how to call method in fragment from fragmentActivity

From Dev

Android - Back button from FragmentActivity to another Fragment

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 pass parameter to Fragment from FragmentActivity

From Dev

Refresh fragment from adapter

From Dev

call method from fragment to fragment ( refresh adapter )

From Dev

How to refresh fragment B from fragment A?

From Dev

Refresh a TextView from inside a fragment

From Dev

How to call fragment from activity without using fragmentactivity?

From Dev

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

From Dev

fragment run before fragmentActivity

From Dev

Android problems with fragment and fragmentActivity

From Dev

Fragment transaction with a FragmentActivity instead of a Fragment

From Dev

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

From Dev

How to Refresh Fragment from Action Bar Menu

From Dev

refresh a fragment from its parent activity

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

How to Refresh Fragment from Action Bar Menu

From Dev

refresh a fragment from its parent activity

Related Related

  1. 1

    refresh fragment UI from fragmentActivity

  2. 2

    Refresh Fragment in FragmentActivity

  3. 3

    Call FragmentActivity from Fragment

  4. 4

    Import text from fragmentactivity to fragment

  5. 5

    Open new Fragment from FragmentActivity

  6. 6

    Android FragmentActivity How to refresh fragment when it become visible

  7. 7

    Get id of TextView in Fragment from FragmentActivity in ViewPager

  8. 8

    Android - How to access a Fragment from a FragmentActivity

  9. 9

    (Android) how to call method in fragment from fragmentActivity

  10. 10

    Android - Back button from FragmentActivity to another Fragment

  11. 11

    Fragment being launched from FragmentActivity not showing up

  12. 12

    Sending a string from FragmentActivity to Fragment through FragmentPagerAdaptor

  13. 13

    How to pass parameter to Fragment from FragmentActivity

  14. 14

    Refresh fragment from adapter

  15. 15

    call method from fragment to fragment ( refresh adapter )

  16. 16

    How to refresh fragment B from fragment A?

  17. 17

    Refresh a TextView from inside a fragment

  18. 18

    How to call fragment from activity without using fragmentactivity?

  19. 19

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

  20. 20

    fragment run before fragmentActivity

  21. 21

    Android problems with fragment and fragmentActivity

  22. 22

    Fragment transaction with a FragmentActivity instead of a Fragment

  23. 23

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

  24. 24

    How to Refresh Fragment from Action Bar Menu

  25. 25

    refresh a fragment from its parent activity

  26. 26

    Refresh or Update the listadapter from a fragment in android eclipse

  27. 27

    How to refresh a fragment from an activity in android?

  28. 28

    How to Refresh Fragment from Action Bar Menu

  29. 29

    refresh a fragment from its parent activity

HotTag

Archive