Objective: To use fragment arguments to pass along the string value from a TextView
to a new fragments TextView
, BUT while using a ViewPager
with different layouts/fragments
in the FragmentPagerAdapter
.
Problem: The new fragment never receives the fragments arguments from the previous fragment.
My Set up: I have my Activity
hosting the ViewPager
and FragmentPagerAdapter
. I have overridden FragmentPagerAdapters
getItem(int position)
method to create a new fragment
instance depending on the current position of the ViewPager
. Right now I only have two Fragments
in my adapter, but will be adding more after getting over this obstacle. I am using the newInstance()
technique to pass along the Fragment
's arguments, but nothing is ever passed.
Pertinent Code:
My FragmentPagerAdapter code:
//this is a variable that I pass in as the newInstanct() methods parameter,
// I update this variable from my fragments constantly
public static String fragmentArgumentsValue = "";
mViewPager.setAdapter(new FragmentPagerAdapter(fm) {
@Override
public int getCount() {
return NUMBER_OF_VIEWS;
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
Log.d(TAG, "---In getPosition(), position 0---");
return Frag1
.newInstance(fragmentArgumentsValue);
case 1:
Log.d(TAG, "---In getPosition(), position 1---");
return frag2
.newInstance(fragmentArgumentsValue);
default:
Log.d(TAG, "---In getPosition(), DEFAULT---");
return frag1
.newInstance(fragmentArgumentsValue);
}
}
});
One of the fragments newInstance() method:
public static Fragment newInstance(String fragmentArgumentsValue) {
Frag1 f = new Frag1();
Bundle bun = new Bundle();
bun.putString(KEY_FRAGMENT_ARGUMENTS_STRING, fragmentArgumentsValue);
f.setArguments(bun);
return f;
}
Getting the fragments arguments:
String argString = getArguments().getString(
KEY_FRAGMENT_ARGUMENTS_STRING);
if (argString.length() != 0) {
Log.d(TAG, "Trying to set the frag args to:" + argString);
mWorkingTextView.setText("" + argString);
} else {
Log.d(TAG, "Couldn't set frag args to: " + argString);
}
What I've Tried: I've tried giving the Activity
that hosts the ViewPager
and FragmentPagerAdapter
a static
variable that I constantly update in each one of my fragments
, I include the static
variable in the fragment.newInstance(staticVariableInActivity)
method, but this doesn't seem to work.
I've also tried using the ViewPager
callback viewPager.setOnPageChangeListener()
I had overridden the onPageSelected(int pos)
and tried to pass the fragment arguments there, nevertheless it didn't work... so please help me S.O.!!!
My thoughts: I do not have the different fragments
and layouts
in an ArrayList
or any list for that matter, I just instantiate the Fragments
via the newInstance()
method depending on the positions of the FragementPagerAdapter
, could this be a problem? Should I create a singleton
list of the layouts/fragments
? So I can change the values of the Fragments
TextViews
via the singleton
list? (excuse me if that's a dumb or not possible thing to do).
Note: I am am saving away the TextViews values via public void onSaveInstanceState(Bundle outState)
to handle screen rotations. Could this be a problem?
Alright so I this link will help in the communication between fragments: Communication with other Fragments .
You need to define a interface that the Activity will implement and the fragments will use to send data onward to the activity, and the activity will then find the fragment by tag doing something like this:
frag = (Fragment2) getSupportFragmentManager()
.findFragmentByTag(
"android:switcher:" + R.id.viewPager + ":2");
and then update the fragment by calling a public method implemented within the fragment.
The link provided will help greatly, it is from the Android Development website.