In my Android program, I've three Fragments
wrapped in a ViewPager
. I have an ArrayList<MyObject>
which is filled inside the onResume()
method of the MainActivity
. How can I send this ArrayList<MyObject>
from the MainActivity
to child Fragments
? What I want is to update my fragment with the ArrayList<MyObject>
, whenever the app comes to foreground. There are a lot of solutions for this specific problem, but none of them is working for me. I've already tried using Bundles
to pass arguments to my Fragments
. For testing, I'm trying to pass simple int value to child fragment: In the onCreate()
of MainActivity
:
Bundle bundle = new Bundle();
bundle.putInt("INTEGER",2);
In the onResume()
of MainActivity
:
Fragment frag = MyFragPageAdapter.getItem(1);
frag.setArguments(bundle);
In the onCreate()
method of my Fragment
:
final Bundle args = getArguments();
final int myInt = args.getInt("INTEGER");
Log.v("IN FOREGROUND", myInt+"");
Can anyone please help me? Thanks in advance!
Adapter:
public class MyFragPageAdapter extends FragmentPagerAdapter implements IconTabProvider {
private final int[] ICONS = { R.drawable.ic_action_cc_bcc, R.drawable.ic_action_call,
R.drawable.ic_action_chat};
private final String[] tabTitles = {"Contacts", "Calls", "Messages" };
public FragmentPageAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int arg0) {
switch (arg0) {
case 0:
return new ContactList();
case 1:
return new DialPad();
case 2:
return new BlockedList();
default:
break;
}
return null;
}
@Override
public int getCount() {
return 3;
}
@Override
public int getPageIconResId(int position) {
// TODO Auto-generated method stub
return ICONS[position];
}
/* (non-Javadoc)
* @see android.support.v4.view.PagerAdapter#getPageTitle(int)
*/
@Override
public CharSequence getPageTitle(int position) {
// TODO Auto-generated method stub
return tabTitles[position];
}
Inside the constructor of ContactList try put the arguements and check .
Bundle bundle = new Bundle();
bundle.putInt("INTEGER",2);
Fragment frag = MyFragPageAdapter.getItem(1);
frag.setArguments(bundle);
first make your object of your arraylist "Serializable"
than
in fragment A
String TabOfFragmentB = ((AndroidViewPagerActivity)getActivity()).getTabFragmentB();
MyFragmentB fragmentB = (MyFragmentB)getActivity()
.getSupportFragmentManager()
.findFragmentByTag(TabOfFragmentB);
fragmentB.sendData(yourArraylist<Object>);
in fragment B
global decalaration
private void myResultedData= null;
outside onCreateView
public yourArraylist<Object> sendData(yourArraylist<Object> t){
myResultedData = t;
}
use "myResultedData" in onCreateView
for more details and understand the concept of communication between fragments of view pager refer link
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.