-2

case 1 : I wanted to know why data should be passed to fragment using .setArguments() method?

case 2 : Why can't we use simple public setter property inside fragment class?

example for case 1 : (kind of Pseudo code)

Activty {
   FragmentA fa = new FragmentA();
   Bundle bundle = new Bundle();
   bundle.putString("key_1", "Hello");
   fa.setArguments(bundle);

   //begin fragment transistion
}

example for case 2 :

Activty {
   FragmentA fa = new FragmentA();

   //setter in frgment class
   fa.setPropertyForKey_1("Hello");

   //begin fragment transistion
}

What is the difference between the 2 cases and which one do you recommend?

Don't you think creating a bundle is overhead in this simple scenario?

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
NKR
  • 343
  • 3
  • 15
  • Check this, https://stackoverflow.com/questions/12739909/send-data-from-activity-to-fragment-in-android – Aditya Jan 08 '19 at 07:15
  • Possible duplicate of [Send data from activity to fragment in Android](https://stackoverflow.com/questions/12739909/send-data-from-activity-to-fragment-in-android) – Anmol Jan 08 '19 at 07:30

2 Answers2

1

Difference between your two cases:

Case 1: The bundle info is not lost after screen rotation. When FragmentA is re-created it receives the data from the bundle again so you still have the requiered data.

Case 2: When you rotate the screen of the device the Fragment is destroyed and then recreated by android. So the content of FragmentA.propertyForKey1 is lost after the rotation.

So no, it is no overhead, it is necessary. And I think it is in generell better to take the most out of the native APIs before trying to reinvent the wheel.

Christoph Mayr
  • 347
  • 3
  • 17
  • If you handle rotation in your activity then data will be not lost. You will get data using getter or setter method. – sushildlh Jan 08 '19 at 07:38
0

If you want to send data to Fragment from Activity, the best way is create to instance of fragment. You can take this code as an example;

Fragment;

class SampleFragment: Fragment{

    companion object {

        const val KEY_DATA = "data"
        const val KEY_OTHER_DATA = "other_data"

        fun instance(data:String, otherData:Int): SampleFragment {
            val fragment = SampleFragment()
            val bundle = Bundle()

            bundle.putString(KEY_DATA,data)
            bundle.putInt(KEY_OTHER_DATA,otherData)

            fragment.arguments = bundle

            return fragment
        }
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        val data = arguments.getString(KEY_DATA)
        val otherData = arguments.getInt(KEY_OTHER_DATA)
    }
}

Activity;

class SampleActivity: AppCompatActivity() {
    @Override
    protected void onCreate(Bundle savedInstanceState) {

        val sampleFragment = SampleFragment.instance("data",0)
    }
}

I hope it works for you

Cafer Mert Ceyhan
  • 1,640
  • 1
  • 13
  • 17