Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - How do I manage multiple instances of a single fragment with different content?

I want to be able to setText and getText of Views of individual Fragments. As it is now, when I setText of a Framgent's TextView it changes the text of that View in all Fragments.

I've been experimenting by moving things around, but here is my code as of this moment:

Fragment class

public class TestFragment extends Fragment{

    View view;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
    Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.test_fragment, container, false);
        TextView tv = (TextView) view.findViewById(R.id.huh);
        //tv.setText("AAAAAAAAAAAAAAAAAAA");

        return view;
    }

    public void setText(String asdf) {
        TextView test = (TextView) view.findViewById(R.id.huh);
        test.setText(asdf);
    }


}

Activity Class

    public class Manage extends BaseActivity {
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.manage);

        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        TestFragment fragment = new TestFragment();
        //fragment.setText("ASDF");
        fragmentTransaction.add(R.id.test_fragment, fragment, "testtag");
        fragmentTransaction.commit();
    }
}

The framgent.xml is pretty plain; just a single TextView.

like image 510
thekthuser Avatar asked Nov 03 '13 20:11

thekthuser


1 Answers

Fragments are added to stack with a parameter named tag. In your case you've added your fragment with "testtag".

fragmentTransaction.add(R.id.test_fragment, fragment, "testtag");

If you create multiple instances of same fragment and add them with unique tags, then you are able to get them with that unique tags. When you get a fragment then you can reach its content.

FragmentManager fm = this.getSupportFragmentManager();
Fragment testtagFragment = fm.findFragmentByTag("testtag"); 
View targetView = testtagFragment.getView().findViewById(R.id.anyViewInsideContentOfYourFragment);

Edit:

I want to be able to setText and getText of Views of individual Fragments.

This question has 2 parts.

  1. To setText while initializing you have to pass your initial parameters to your fragment while creating its instance. I suggest you to use a static newInstance method for this. See sample here
  2. To getText read my answer above. Note that, you can get the content of a fragment after its onCreateView method is executed. So If you try to call getView method of a fragment at your activities onCreate method (after you add the fragment), that will return null. You can get its content successfully under a click event to test that, and use get or set operations of any view on that fragment's content.
like image 82
Devrim Avatar answered Oct 31 '22 13:10

Devrim