

It’s a common situation when developer facing this kind of a problem during application developer. Situation: How to access a variable in activity from a fragment? Maybe chance you have to another way or requirement, then you have to choose as per needs. MyFragment.receiveMsg("Hi Fragment how are you ?")

MyFragment myFragment = (MyFragment) manager.findFragmentById(R.id.myfrag) Method in fragment //Receive message - activity to fragmentĬreate an instance in activity and pass data to fragment FragmentManager manager = getSupportFragmentManager()

it’s a flexible way when the fragment is already launched and Fragments want data from an Activity. Its very easy to do, create a method in Fragments and in Activity create an instance of fragments and pass data in methods.
