-1

I have an application with a couple of fragments. I would like a variable that I can edit from each fragment. I mean my main activity shows the first fragment at start. This first fragment has the following code:

button_1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            FragmentManager fragmentManager = getFragmentManager ();
            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction ();

            fragmentTransaction.setCustomAnimations(R.anim.enter_from_right, R.anim.exit_to_left).replace(R.id.content_frame, new Fragment_2());
            fragmentTransaction.commit ();


        }});

So this button opens the second fragment, but before that I would like to add 1 to that integer on button click. Then the second fragment has a similar button that should do the same. How can I do that?

Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
Tamas Koos
  • 1,053
  • 3
  • 15
  • 35
  • 1
    Possible duplicate of [Best practice for instantiating a new Android Fragment](http://stackoverflow.com/questions/9245408/best-practice-for-instantiating-a-new-android-fragment) – Egor Neliuba Feb 20 '16 at 11:28
  • make a global variable in calling activity or you can use preferences – UMESH0492 Feb 20 '16 at 11:31

1 Answers1

0
Fragment fragment= new Fragment_2();
Bundle bdl=new Bundle();
bdl.putInt("a",1);
fragment.setArguments(bdl);

 FragmentManager fragmentManager = getFragmentManager ();
            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction ();

            fragmentTransaction.setCustomAnimations(R.anim.enter_from_right, R.anim.exit_to_left).replace(R.id.content_frame,fragment);
            fragmentTransaction.commit ();

In your second fragment,

int a=getArguments().getInt("a");
Seshu Vinay
  • 13,560
  • 9
  • 60
  • 109