2

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?

Community
  • 1
  • 1
Dr.jacky
  • 3,341
  • 6
  • 53
  • 91
  • 1
    jgriffsta's answer is right. The tag it will be null in the `onCreate()` method(as fragment transactions are asynchronous but it should be ok if you use that code in the `onConnected()` callback. – user Jul 19 '14 at 08:39
  • @Luksprog You right. that's work. But i changed my technique to this: http://stackoverflow.com/questions/8785221/retrieve-a-fragment-from-a-viewpager. But in getItem() i don't have any idea about newInstance(). 1-What is that function's codes? 2-Which techniques are better? – Dr.jacky Jul 19 '14 at 09:01
  • 1
    1. it's a static method that returns an object of the class in which it was declared(it's a common java pattern) 2. The code you linked to is better because it doesn't rely on some tag that could be changed(breaking your app) – user Jul 19 '14 at 09:06
  • @Luksprog I don't get point of 1! Does my question's getItem(){...} is right? Or should i check if fragmentObject is null then create it, by makeFragmentName? Or new technique handle this?(this means: null, recreate, restore,...) – Dr.jacky Jul 19 '14 at 09:12
  • 1
    Your `getItem()` method is correct, in there you need to return a fragment object(which you can do either by instantiating the class or using that `newInstance()` static method). – user Jul 19 '14 at 09:16
  • https://stackoverflow.com/questions/16345129/java-lang-illegalstateexception-cant-change-tag-of-fragment/59835511#59835511 – Peter Jan 21 '20 at 06:48

1 Answers1

6

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);
        }
johng
  • 825
  • 1
  • 9
  • 15
  • 1
    I changed my technique to this: http://stackoverflow.com/questions/8785221/retrieve-a-fragment-from-a-viewpager ;;;;; But thanks a lot and your answer is right anyway. – Dr.jacky Jul 19 '14 at 09:07