how to pass string data from activity to fragment?

后端 未结 5 1328
被撕碎了的回忆
被撕碎了的回忆 2020-12-11 13:04

I want to pass string value from activity to fragment. For that I\'m using bundle for transferring string value.

PUT STRING ACTIVITY Passing string

5条回答
  •  生来不讨喜
    2020-12-11 13:31

    The savedInstance variable will be empty the first time you run your app, it only gets filled when the onSaveInstanceState method is called, this usually happens when you rotate the screen, then savedInstance variable will be populated. Try this :

    if(savedInstance != null){
      Log.d(TAG, "There's is something");
    }else{
      Log.d(TAG, "Nothing in there");
    }
    

    First time in logcat you will see the first message, try turning your device and the message in the else will show.

    As @james said, the recommended way is to use interfaces, or have a field class but this increases coupling and destroys the whole point of fragments.

提交回复
热议问题