2

So currently I have a application with different activities, and I use buttons to navigate between activities. I later decided that I should add the navigation drawer and use fragments instead. So one of my fragments has a bunch of fields that the user fills out that I need to pass onto the next fragment. So my question is, Do I keep all the work in activities and call the activity from the fragment? Or do I just include all he java in my activity in the java for the fragment? For the most part I am taking the fields from the first fragment, and I'd like to pass the values to the next fragment so it can handle some calculations.

final EditText FinalPriceText = (EditText)         v.findViewById(R.id.editTextPrice);
final EditText TradeInPriceText = (EditText) v.findViewById(R.id.editTextTrade);
i.putExtra("FinalAutoPrice", FinalAutoPriceText.getText().toString());
i.putExtra("TradeInPrice", TradeInPriceText.getText().toString());

startActivity(i);
Riva
  • 59
  • 1
  • 7

3 Answers3

3

As far as calculations go, if you are going to use them a lot and they have nothing to do with the activity or android lifecycle I would separate them out into a different class and then you can call them from anywhere.

If they do rely on the activity you could still separate them out but pass a reference to the activity when doing your calculations. You can get the parent activity by calling this.getActivity() from any fragment

You can cast this.getActivity() to whatever the parent activity is and you can call the methods from that object as well. This works fine but your fragment will only work with the activity you specify and it can get sloppy if you are not careful.

Otherwise put them in the fragment where you need them. I would consider this least recommended if you need to use calculations anywhere else in the app. Duplicate code is just asking for bugs in the future.

As far as passing data, create a static instance method in fragment2 and pass it what you need there.

For example

public Fragment2 extends Fragment {

    public static fragment2 newInstance(MyData myDataIPass) {
         Fragment2 fragment = new Fragment2();
         Bundle args = new Bundle();
         args.putInt("someInt", myDataIPass.someInt);
         fragment.setArguments(args);
         return fragment;
    }

}

Call this new instance method when creating your fragment transaction like this

FragmentManager fm = getActivity().getFragmentManager();
fm.beginTransaction()
   .replace(R.id.container, Fragment2.newInstance(MyData myDataIPass))
   .commit();
Zach
  • 1,964
  • 2
  • 17
  • 28
  • 1
    Assign values to any fields in fragment while instantiating is dangerous. If you have screen rotation, after rotation Fragment will be recreated and all passed parameters will be lost. Use fragment's 'setArguments' instead. – Raiv Feb 16 '16 at 01:45
  • Good point, I have edited the answer. I have to say the lack of ability to safely pass complex data types without serializing it or bundling it has got to be my least favorite things about android development. – Zach Feb 16 '16 at 01:54
  • So I added my normal intent, I'm assuming by adding .replace(R.id.container, Fragment2.newInstance(MyData myDataIPass)) , myDataIPass is same as my startActivity(i); ? – Riva Feb 16 '16 at 04:36
2

Well, you have mainly two options here:

  1. use Intent.putExtra() with fragments. Just like Activities, you can use this method with Fragments as well. See the following links for the implementation in Fragments
  2. The other option is to use SharedPreferences and store data as key value pairs from one fragment, and can be accessed from any other activity/fragments. See this nice tutorial to understand better!
Community
  • 1
  • 1
OBX
  • 6,044
  • 7
  • 33
  • 77
0

You have a special callback in Fragment to get Activity. It is called:

@Override
public void onAttach(Context context) {
    super.onAttach(context);
    YourActivity activity  = (YourActivity) context;
}
Anton Kizema
  • 1,072
  • 3
  • 13
  • 27