Passing Value from Activity to Fragment

user10708780

I have bottom navigation activity in my project and contain two fragments. I am trying to pass value from Activity--->FragmentOne and then From FragmentOne--->FragmentTwo. Any help is appreciated.

Language Used

Kotlin

Expectation

1)Pass value from Activity to Fragment
2)Send value from Fragment to Fragment

Error

Null Pointer Exception

Code

Activity

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_test)
        var testName:String=intent.getStringExtra("name")
        println("TestCLLicked: $testName")
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener)
        replaceFragment(TestFragmentOne.newInstance(),TestFragmentOne.TAG)
    }

TestFragmentOne

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
            super.onViewCreated(view, savedInstanceState)
            var st:String=arguments!!.getString("name")
             println("TestCLLicked: $testName")
Orestis Gartaganis

You can go many ways but given your current implementation (using a newInstance), I'd go with using your parent activity as a mediator, going like this:

1) Create a BaseFragment class which your TestFragmentOne and TestFragmentTwo will extend and in there hold a reference to your parent Activity (here named "MainActivity"):

abstract class BaseFragment : Fragment() {

     lateinit var ACTIVITY: MainActivity

     override fun onAttach(context: Context) {
         super.onAttach(context)
         ACTIVITY = context as MainActivity
     }
}

2) Then, in your Activity make sure you declare your variable as a field:

class MainActivity : AppCompatActivity() {

     var textVariable = "This to be read from the fragments"
     ...
     override fun onCreate(savedInstanceState: Bundle?) {
         super.onCreate(savedInstanceState)
         textVariable = "I can also change this text"
         ...
     }
}

3) Then, from each fragment you can access your variable using the instance inherited from your BaseFragment:

 class TestFragmentOne : BaseFragment() {

      override fun onActivityCreated(savedInstanceState: Bundle?) {
          super.onActivityCreated(savedInstanceState)
          val incomingText = ACTIVITY.textVariable
          println("Incoming text: "+incomingText)

          // You can also set the value of this variable to be read from 
          // another fragment later
          ACTIVITY.textVariable = "Text set from TestFragmentOne"
      }
 }

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Passing a value from Activity to Fragment in Kotlin

Passing spinner value from activity to fragment

Get empty data when passing variable value from activity to fragment

passing string from an activity to a fragment

Passing Object from Fragment to Activity

Passing Interface To Fragment From Activity

Passing values from Activity to Fragment

Passing a long value from one fragment through the main activity to another fragment

Android, passing Extras from activity, to fragment activity, to fragment

Passing `SharedPreferences` data from Fragment to Activity

Passing data from list fragment to new activity

passing data from activity to tabs fragment

Passing data from Activity to Fragment shows nothing

Passing Data from Fragment to Activity using listview

Passing a data from Activity to Tabbed Layout Fragment?

Android passing ArrayList<Model> to Fragment from Activity

Passing data from activity to swipeable view fragment

Passing ArrayList from Fragment class to Activity

error passing arguments from an activity to a fragment in android

passing data from fragment to activity is not same

Passing data from activity to fragment in android is not working

Passing data from activty B to activity A fragment

Passing data from an Activity to a fragment that has no onCreate

Passing Value from string.xml from one Fragment to another activity?

NullPointer exception while passing a value from activity to Interface method and retrieve in fragment android

Passing data from one activity fragment to a different activity fragement

Passing data from Activity to Fragment (by accessing activity from fragment) giving Class Cast Exception

Updating value inside fragment from activity

Cant get bundle value from activity to fragment