【问题标题】:Passing Value from Activity to Fragment将价值从活动传递到片段
【发布时间】:2019-01-03 18:19:19
【问题描述】:

我的项目中有底部导航活动并包含两个片段。我正在尝试从 Activity--->FragmentOne 传递值,然后从 FragmentOne--->FragmentTwo 传递值。任何帮助表示赞赏。

使用的语言

Kotlin

期待

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

错误

Null Pointer Exception

代码

活动

    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")

【问题讨论】:

  • 请阅读如何使用newInstance模式medium.com/@azjkjensen/…
  • 在添加之前使用 Fragment 的参数包
  • 哦,顺便说一句,您的 newInstance 是做什么的?你有点把它排除在你的问题之外

标签: android android-fragments kotlin android-fragmentactivity kotlin-android-extensions


【解决方案1】:

您可以采用多种方式,但鉴于您当前的实现(使用 newInstance),我会使用您的父活动作为中介,如下所示:

1) 创建一个 BaseFragment 类,您的 TestFragmentOne 和 TestFragmentTwo 将扩展该类,并在其中保存对您的父 Activity(此处命名为“MainActivity”)的引用:

abstract class BaseFragment : Fragment() {

     lateinit var ACTIVITY: MainActivity

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

2) 然后,在您的 Activity 中确保将变量声明为字段:

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) 然后,您可以从每个片段中使用从 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"
      }
 }

【讨论】:

  • 只要您实现 onSaveInstanceState 以持久化该可变字符串并在 onCreate 中恢复它(如果 savedInstanceState 包不为空),此方法就可以正常工作。
  • 我创建了 BaseFragnent 类并遵循上述方法,但仍然得到空值
  • @EpicPandaForce true
  • @Test 你什么时候得到空值?从片段中读取变量?什么是 null,ACTIVITY 还是 textVariable?
  • testFragmentOne 上的 println($incomingText) 为空
【解决方案2】:

对于这种情况,我使用静态 Intent 并根据需要通过它传输数据。

    static final Intent storageIntent = new Intent();

    storageIntent.putExtra("name", "value");

【讨论】:

  • 如何从片段中获取这个值?
  • 这是错误的。意图不应该是静态的。这将在进程死亡后丢失,并将失去其值。对活动使用 Intents,对 Fragments 使用参数包 (setArguments)。
  • 放在公共类中时它保持活动状态,因为它也是最终的。应用死了之后,我想数据就不再需要交换了。
  • 你获取数据例如:String s = storageIntent.getStringExtra("name");
  • 感谢您的回复,但我已经尝试过了,但它不起作用。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-02-26
  • 2022-01-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多