3

I do not understand how can I transfer data from fragmentactivity to fragment, i have:

MainScreen.class

public class MainScreen extends FragmentActivity {
CollectionPagerAdapter mCollectionPagerAdapter;
ViewPager mViewPager;

public static String currentCityId;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.tabs_collection);

    mCollectionPagerAdapter = new CollectionPagerAdapter(getSupportFragmentManager());

    final ActionBar actionBar = getActionBar();

    actionBar.setDisplayHomeAsUpEnabled(false);
    actionBar.setTitle(getString(R.string.app_name));
    actionBar.setIcon(R.drawable.device_access_brightness_high);

    mViewPager = (ViewPager) findViewById(R.id.pager);
    mViewPager.setAdapter(mCollectionPagerAdapter);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.mainmenu, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.action_refresh:
            Log.w("Refresh", "refresh");

            break;
        case R.id.action_options:

            break;
        case R.id.submenu_about:

            break;
        case R.id.submenu_help:

            break;
        case R.id.submenu_buy:

            break;
        case R.id.submenu_settings:

            break;
        case R.id.submenu_share:

            break;
        default:
            break;
    }

    return true;
}

public class CollectionPagerAdapter extends FragmentStatePagerAdapter {
    FragmentManager fm;

    public CollectionPagerAdapter(FragmentManager fm) {
        super(fm);
        this.fm = fm;
    }

    @Override
    public Fragment getItem(int position) {
        switch (position) {
            case 0:
                return new FragmentDay();
            case 1:
                return new FragmentHour();
            case 2:
                return new FragmentDaily();
            default:
                return new FragmentDay();
        }
    }

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

    @Override
    public CharSequence getPageTitle(int position) {
        switch (position) {
            case 0:
                return getString(R.string.tab_day);
            case 1:
                return getString(R.string.tab_hour);
            case 2:
                return getString(R.string.tab_daily);
            default:
                return getString(R.string.tab_day);
        }
    }
}

FragmentDay.class

public class FragmentDay extends Fragment {
TextView currentTemp;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.hour_screen, container, false);
    Bundle args = getArguments();
    currentTemp = (TextView)rootView.findViewById(R.id.day_current_temp);
    return rootView;
}

}

main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
            android:layout_height="match_parent"
            android:layout_width="match_parent"
            android:background="@drawable/bk_new">

<android.support.v4.view.ViewPager
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

    <android.support.v4.view.PagerTitleStrip
            android:id="@+id/pager_title_strip"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="top"
            android:background="#33b5e5"
            android:textColor="#fff"
            android:paddingTop="4dp"
            android:paddingBottom="4dp"/>
</android.support.v4.view.ViewPager>

and fragment.xml:

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/dayScreen"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/bk_new">

<TextView
        android:id="@+id/day_current_temp"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:layout_centerHorizontal="true"
        android:text="22 C"
        android:textColor="#FFFFFF"
        android:textSize="38dp"
        android:layout_marginTop="270dp"/>

Read these articles (ViewPager Activity to notify a Fragment of a specific event, Updating fragments from Activity in a ViewPager, ViewPager - Update fragment from activity) and do not understand what to do! I will be very grateful for the help!

Community
  • 1
  • 1
azusdex
  • 45
  • 1
  • 1
  • 7

1 Answers1

2

When you say "transfer data" I'm assuming you mean variables so you can tweak your fragment in whichever way.

You can access the activity object at any time within the fragment using getActivity() method and cast it to your Activity implementation...

// Code in the fragment
((MainScreen) getActivity()).getSomeDataFromActivity()

There is an article here also by Google which describes patterns of communication between Fragment and Activity.

Eurig Jones
  • 8,226
  • 7
  • 52
  • 74
  • I mean, let's say if I click on the menu button in the actionbar that is updated in fragment my textview – azusdex Mar 28 '13 at 21:53
  • well the click events are available in the fragment. So if you want to handle clicks of menu items from the actionbar then you can override onOptionsItemSelected() method in your fragment. – Eurig Jones Mar 28 '13 at 21:56
  • I did override onOptionsItemSelected() method in fragment it does not work! I doubt that something wrong! – azusdex Mar 28 '13 at 22:11
  • 1
    Well looking at your code you are overriding onOptionsItemSelected() in your activity as well. You are also returning true value in this which would mean that the activity would not pass the event down to the fragment. You only return true; if you have consumed the click event. – Eurig Jones Mar 28 '13 at 22:17
  • Please see my related question: http://stackoverflow.com/questions/24833912/refresh-fragment-ui-from-fragmentactivity – Dr.jacky Jul 19 '14 at 08:09