Walter Gandarella
Walter Gandarella

Reputation: 858

Calling fragment method from activity

I have one in my project ViewPage with two fragments (FragmentView1 and FragmentView2).

This is my ViewPage MainActivity, which has a ActionBar with a menu.

In this menu, each option should call a method that is in FragmentView1 to update it.

But I really do not know how to do this!

This is the method in my FragmentView1:

public void loadPoema(){
    // my code here
}

And here is where I call it from my Activity:

@Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle presses on the action bar items
        switch (item.getItemId()) {
            case R.id.actBack:
                postAtual--;   
                        // Here I call the method in FragmentView1          
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

To learn the method in question in FragmentView1 serves to update it (the very Fragment) loading new data.

once again apologize here for my level, I'm using Google Translator

EDIT

MainActivity XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <android.support.v4.view.ViewPager
       android:id="@+id/viewpager"
       android:layout_width="fill_parent"
       android:layout_height="0dp"
       android:layout_weight="1">

            <android.support.v4.view.PagerTabStrip
                android:id="@+id/pagerTabStrip"
                android:layout_gravity="bottom"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="#cccccc"/>

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

</LinearLayout>

Fragment XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="@dimen/activity_horizontal_margin"
    android:id="@+id/fragPoema"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/txtTitulo"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:textStyle="bold"
        android:textSize="18sp"
        android:textColor="#0099FF"
        android:typeface="serif" />

    <TextView
        android:id="@+id/txtPoema"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_marginTop="10dp"
        android:textColor="#333333"
        android:layout_gravity="center"
        android:scrollbars="vertical"
        android:typeface="serif" />

</LinearLayout>

And MainActivity code:

private void loadViews(){
        mViewPager = (ViewPager) findViewById(R.id.viewpager);

        TitleAdapter titleAdapter = new TitleAdapter(getSupportFragmentManager());
        mViewPager.setAdapter(titleAdapter);
        mViewPager.setCurrentItem(0);
    }

Upvotes: 1

Views: 7608

Answers (1)

Sean
Sean

Reputation: 5266

When you need your fragment, you can get a reference to it using findFragmentByTag and then perform you action on it. Make sure you initially give the fragment a tag. It can be made both from code or XML.

EDIT: I see now you are using a Pager Adapter to create the fragments. It's a bit more tricky. Follow this post to extract the Tag of the fragments you are creating. reusing fragments in a fragmentpageradapter

Upvotes: 1

Related Questions