0

When i try to pass bundle variables to my Fragment in android they return as null in the fragment itself, yes I know this question has been asked a lot but this is my first 5 weeks in android and i am completely new to fragments and passing bundles

this is the hero activity code (inside on create)

// create bundle of variables
    final Bundle bundle = new Bundle();
    bundle.putString("description",description);
    bundle.putString("affiliation",affiliation);
    bundle.putString("role",role);
    bundle.putString("realName",realName);
    bundle.putString("occupation",occupation);
    bundle.putString("base",base);
    bundle.putString("backstory",backstory);
    bundle.putInt("difficulty",difficulty);
    bundle.putInt("age",age);

    // pass data to fragments
    FragmentTransaction transaction = getFragmentManager().beginTransaction();


    HeroDescriptionFragment descriptionFragment = new HeroDescriptionFragment();
    descriptionFragment.setArguments(bundle);

    HeroStoryFragment storyFragment = new HeroStoryFragment();
    storyFragment.setArguments(bundle);

    transaction.commit();

here i am trying to read the bundle in the fragment:

 @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View rootView = inflater.inflate(R.layout.fragment_hero_description, container, false);

    // populate hero description
    description = this.getArguments().getString("description");
    TextView heroDesc = (TextView) rootView.findViewById(R.id.heroDescription);
    heroDesc.setText(description);

    return rootView;
}

all fragments are created via this pager adapter :

    public class PageAdapter extends FragmentStatePagerAdapter {

    int numberOfTabs;

    public PageAdapter(FragmentManager manager, int numberOfTabs) {
        super(manager);
        this.numberOfTabs =  numberOfTabs;
    }
    @Override
    public Fragment getItem(int position) {
        switch (position) {
            case 0:
                return new HeroDescriptionFragment();
            case 1:
                return new HeroStoryFragment();
            default:
                return null;
        }
    }

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

I have the feeling i am really close to solving this but i can't figure this out on my own and my teacher has 0 experience in fragments.

2 Answers 2

1

Your problem is you are trying to manage Fragments in two different ways - manually and with a ViewPager. These are contradictory - choose one and only one.

You are setting the arguments Bundles in a transaction and then having the Adapter return Fragments that have no arguments set. To fix your problem, you will have to refactor your ViewPager method to set the arguments:

@Override
public Fragment getItem(int position) {
    switch (position) {
        case 0:
            Fragment frag = new HeroDescriptionFragment();
            frag.setArguments(createBundle());
            return frag;
        case 1:
            Fragment frag = new HeroStoryFragment();
            frag.setArguments(createBundle());
            return frag;
        default:
            return null;
    }
}

where createBundle() a method for creating the bundle as in your original code.

Sign up to request clarification or add additional context in comments.

6 Comments

though it's still good practice to keep an instance of the fragments and rather pass new data to the instance than constantly creating a new one, considering memory usage
@Zoe I'm sorry but I don't think that's correct - getItem basically means createItem and the PagerAdapter handles caching itself
if you try creating an Activity with tabs and create an AsyncThread that launches in onCreate (to delay the start) and set text in a TExtView from it, you''ll see that it's blank every time the fragment is opened if you return a new instance. There's no caching by default with it at least
@Zoe caching Fragments yourself can be a source of memory leaks. The FragmentManager manages these for you. For instance, if you pause and resume your Activity the previous instances of the Fragment will be reused without a call to getItem(). Also, the FragmentManager will attempt to save instance state of your Fragments on onSaveInstanceState in your Activity and then attempt to restore them in onCreate()
I've put the createBundle() in the heroActivity class and call it like so: heroActivity.createBundle() where the function is declared as a bundle and all the bundle code is moved inside of it and returned once the variables are set, however i get a null reference exeption in the PageAdapter class
|
0

use a static method in your fragment class like this

public static MyFragment newInstance(String data1,String data2){
 MyFragment fragment=new MyFragment();
Bundle budle=new Bundle();
bundle.putString("key",data1);
....
fragment.setArguments(bundle);
return fragment;
}


//then create new Fragment like this
MyFragment f=MyFragment.newInstance("a","b");
return f;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.