1

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);
    }
Walter Gandarella
  • 708
  • 1
  • 5
  • 17
  • You need to keep a reference to your `Fragment`, then just call `mFragmentView1.loadPoema();`. – Phil Oct 12 '13 at 12:52
  • @Phil I tried to call the method as follows, without having to instantiate: `FragmentView1.loadPoema();` but apparently does not work as when I want something of my activity, which simply call `MainActivity.sameMathone` – Walter Gandarella Oct 12 '13 at 12:59
  • Well, at first ran the method call, but in this method in FragmentView1 it runs a `mDialog = new ProgressDialog(getActivity());` that when I call the MainActivity returns an error because the getActivity() – Walter Gandarella Oct 12 '13 at 13:06
  • you are attempting to call a `local` method as a `static` method. You need to call `loadPoema()` on an `instance` of `mFragmentView1`. – Phil Oct 12 '13 at 13:07
  • I resolved my problem by turning any method in FragmentView1 in static – Walter Gandarella Oct 12 '13 at 13:52
  • 1
    @WaltterBarreiroNeto, that is definitely not the right approach to use. Check out [this](http://stackoverflow.com/questions/5142070/difference-between-static-and-global-variable-in-java) question to help get the right way figured out. – Phil Oct 13 '13 at 03:27

1 Answers1

1

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

Community
  • 1
  • 1
Sean
  • 5,176
  • 2
  • 34
  • 50