0

I am working on the BitMapFun project. I am trying to call a fragment public void from the parent activity of the fragment.

Some source code :

Activity :

public void onCreate(Bundle savedInstanceState) {
    if (BuildConfig.DEBUG) {
        Utils.enableStrictMode();
    }
    super.onCreate(savedInstanceState);
    setContentView(R.layout.image_detail_pager);

    // Fetch screen height and width, to use as our max size when loading images as this
    // activity runs full screen
    final DisplayMetrics displayMetrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
    final int height = displayMetrics.heightPixels;
    final int width = displayMetrics.widthPixels;

    ImageCache.ImageCacheParams cacheParams =
            new ImageCache.ImageCacheParams(this, IMAGE_CACHE_DIR);
    cacheParams.setMemCacheSizePercent(0.25f); // Set memory cache to 25% of app memory

    // The ImageFetcher takes care of loading images into our ImageView children asynchronously
    mImageFetcher = new ImageFetcher(this, longest);
    mImageFetcher.addImageCache(getSupportFragmentManager(), cacheParams);
    mImageFetcher.setImageFadeIn(false);

    // Set up ViewPager and backing adapter
    mAdapter = new ImagePagerAdapter(getSupportFragmentManager(), Images.imageUrls.length);
    mPager = (ViewPager) findViewById(R.id.pager);
    mPager.setAdapter(mAdapter);
    mPager.setOffscreenPageLimit(2);

    // Set up activity to go full screen
    getWindow().addFlags(LayoutParams.FLAG_FULLSCREEN);

    // Enable some additional newer visibility and ActionBar features to create a more immersive photo viewing experience
    if (Utils.hasHoneycomb()) {
        final ActionBar actionBar = getActionBar();

        // Set home as up
        actionBar.setDisplayHomeAsUpEnabled(true);
        actionBar.setTitle("");

        // Start low profile mode
        mPager.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LOW_PROFILE);
        // actionBar.hide();
    }

    // Set the current item based on the extra passed in to this activity
    final int extraCurrentItem = getIntent().getIntExtra(EXTRA_IMAGE, -1);
    if (extraCurrentItem != -1) {
        mPager.setCurrentItem(extraCurrentItem);
    }

    // Événement changement d'image
    mPager.setOnPageChangeListener(new OnPageChangeListener() {
        @Override
        public void onPageSelected(int arg0) {
            // cache le bouton de retour situé dans le coin supérieur gauche si l'utilisateur change d'image
            mPager.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LOW_PROFILE);
        }

        @Override
        public void onPageScrollStateChanged(int arg0) { }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) { }
     });
}

(...)

ImageDetailFragment.SetPhotoView();

(...)


private class ImagePagerAdapter extends FragmentStatePagerAdapter {
    private final int mSize;

    public ImagePagerAdapter(FragmentManager fm, int size) {
        super(fm);
        mSize = size;
    }

    @Override
    public int getCount() {
        return mSize;
    }

    @Override
    public Fragment getItem(int position) {
        return ImageDetailFragment.newInstance(Images.imageUrls[position]);
    }
}

Fragment :

private ImageView mImageView;

(...)

    public void SetPhotoView() {
        mAttacher = new PhotoViewAttacher(mImageView);
    }

Layout file of detailfragment :

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <ProgressBar
        style="?android:attr/progressBarStyleLarge"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center" />

    <biz.jchambon.gedm.bitmapfun.ui.RecyclingImageView
        android:id="@+id/imageView"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:contentDescription="@string/imageview_description" />

</FrameLayout>

And layout_detail_pager.xml :

<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/pager"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

</android.support.v4.view.ViewPager>

The problem is I can't make a static reference to the non-static method SetPhotoView() ... If I change SetPhotoView to a static method, I have to change mImageView to be static too, and my fragment is not working anymore.

Edit:

If I use ImageDetailFragment fragment = (ImageDetailFragment)getSupportFragmentManager().findFragmentById(mPager.getId()); fragment.SetPhotoView();

Then it works but with the wrong fragment ... How can I find the ID of the actual fragment ?

In fact, I have the same problem as here : Getting the current Fragment instance in the viewpager But for me, findFragmentByTag("android:switcher:" + R.id.pager + ":" + mPager.getCurrentItem()) return nothing :/

Someone could tell me how can I get around this ??

Community
  • 1
  • 1
Jerry
  • 1,141
  • 1
  • 13
  • 18

1 Answers1

2

From your activity you can get the fragment object like this.

ImageDetailFragment fragment = (ImageDetailFragment) getSupportFragmentManager().findFragmentById(android.R.id.content);

If you fragment takes up the whole activity, then use android.R.id.content otherwise put the id of the layout where the fragment is placed.

Then you can call

fragment.SetPhotoView();
Aswin Rajendiran
  • 3,399
  • 1
  • 21
  • 18
  • Thanks for your answer ! Your answer seems to be right, I have no compilation error, but the application crashes when I call setPhotoView() from the parent Activity (java.lang.NullPointerException) – Jerry Jan 27 '14 at 21:16
  • It might be that the fragment is null. Did you change the id of the fragment? Maybe you can paste the layout file and onCreate function in the Activity? We can take a look. – Aswin Rajendiran Jan 27 '14 at 21:21
  • Aswin > of course, I have updated my post, and thanks for your contributions – Jerry Jan 27 '14 at 21:37
  • Jerry, could you post this layout file? image_detail_pager. – Aswin Rajendiran Jan 28 '14 at 02:27
  • Aswin > oh , sorry , I forgot it. That's ok now. Thanks ! – Jerry Jan 28 '14 at 17:01
  • Aswin > That's OK ! But the BitmapFun project is BUGGED. The pictures (Fragments) can be showed in random order if we set the quality to 100%, and The fragment actually shown is not necessarily the fragment that receives the call. I don't know why .. – Jerry Feb 01 '14 at 17:38