Jerry
Jerry

Reputation: 1238

Call a fragment public void from the parent activity

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 ??

Upvotes: 0

Views: 1303

Answers (1)

Aswin Rajendiran
Aswin Rajendiran

Reputation: 3409

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

Upvotes: 2

Related Questions