将数据从 Activity 1 中的 Fraggment_A 发送到 Activity 2 中的 Fragment_B

Sending data from Fraggment_A in Activity 1 to Fragment_B in Activity 2

我在 Activity_1 上有一个 Fragment_A 并且每当在 Fragment_A我要开始Activity_2有个Fragment_B

所以我需要从Fragment_A发送一个user_IdActivity 1activity_2中的Fragment_B。 有什么方法可以直接从Frament_A发送数据到Activity_1Fragment_BActivity_2?

我是一名学生,所以我是初学者,我确实在这里发现了这个问题,但我不知道该怎么做才能解决这个问题 我确实找到了一些关于界面的解决方案,但我不知道该怎么做

我不断得到 class_id null

语言:Kotlin

Class : ClassesAdapter

class ClassesAdapter(val c:Fragment,val l:android.content.Context? ,val classeslist: ArrayList<Classes>) : RecyclerView.Adapter <ClassesAdapter.ClassesViewHolder> (){
    lateinit var auth: FirebaseAuth
    lateinit var DataBase : DatabaseReference
    lateinit var DataBase2 : DatabaseReference
    lateinit var clipboardManager: ClipboardManager


    private lateinit var mListener :onItemClickListener

    interface onItemClickListener {

        fun onItemClick(position :Int)

    }

    /*fun setOnItemClickListener(listener : onItemClickListener){

       // mListener=listener

    }*/

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ClassesViewHolder {
        val itemView = LayoutInflater.from(parent.context).inflate(R.layout.class_items2,parent,false)
        //return ClassesViewHolder(itemView,mListener)
        return ClassesViewHolder(itemView,)
    }


  
    
        override fun onBindViewHolder(holder: ClassesViewHolder, position: Int) {
            val currentitem = classeslist[position]
            holder.classname.text = currentitem.name
            holder.classrole.text = currentitem.role
    
            holder.itemView.setOnClickListener {
    
    
                val intent = Intent(l, DetailedClassActivity::class.java)
                intent.putExtra("classId", currentitem.class_id)
                l!!.startActivity(intent)
    
            }

Class : Activity2 谁有我想发送的片段 class_id`

 package com.example.myclass1
    
    import android.content.Intent
    import android.os.Bundle
    import android.widget.Toast
    import androidx.viewpager2.widget.ViewPager2
    import com.example.myclass1.databinding.ActivityDetailedClassBinding
    import com.google.android.material.tabs.TabLayout
    import com.google.android.material.tabs.TabLayoutMediator
    import com.google.firebase.auth.FirebaseAuth
    
class DetailedClassActivity : DrawerBaseActivity() {

    private lateinit var binding: ActivityDetailedClassBinding
    //lateinit var toggle: ActionBarDrawerToggle
    override lateinit var auth: FirebaseAuth

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding= ActivityDetailedClassBinding.inflate(layoutInflater)
        setContentView(binding.root)

        // add it to change the toolbar title's to the activity name
        val actionBar = supportActionBar
        if (actionBar != null) {
            val dynamicTitle: String = HomeActivity::class.java.simpleName
            //Setting a dynamic title at runtime. Here, it displays the current activity name
            actionBar.setTitle("Home")
        }


        // navigate between documents ,notification and chatroom  :  tabLayout
        val tablayout2 : TabLayout =findViewById(R.id.tab_layout2)
        val viewpager21 : ViewPager2 =findViewById(R.id.viewPager21)
        val adapter =PagerAdapter2(supportFragmentManager,lifecycle)
        viewpager21.adapter = adapter
        TabLayoutMediator(tablayout2, viewpager21) { tab, position ->
            when(position){
                0->{tab.text="Documents"}
                1->{tab.text="Note"}
                2->{tab.text="ChatRoom"}
            }
        }.attach()


       

    val intent=getIntent()
    var classId= intent.getStringExtra("classId")
      

fun getMyData(): String? {
    return classId
    }

    }
       

class : 片段 B

   enter code here
     btnAddcourse.setOnClickListener {
                var nameofthecourse = Coursename.text.toString().trim()
                var course_id :String?=DataBase3.push().key
                
                var classId = arguments?.getString("classId")
               
                
                val dateofthecourse: String = formatTimeStamp()
    
                if (TextUtils.isEmpty(nameofthecourse)) {
                    // No name added
                    Coursename.error = "No name added !! Please enter the name of the class"
                }
                else{
                    courseList.add(Course(course_id,nameofthecourse,dateofthecourse,classId))
                    coursesAdapter.notifyDataSetChanged()
                    Toast.makeText(activity,"Adding Course Success", Toast.LENGTH_SHORT).show()
                    //Toast.makeText(activity,"class_id is null ", Toast.LENGTH_SHORT).show()
                    if (classId != null) {
                        addCourseToDataBase(course_id!!,nameofthecourse,dateofthecourse,classId)
                    }else{
                        Toast.makeText(activity,"class_id is null ", Toast.LENGTH_SHORT).show()
                    }
                }
            }

片段A

button.setOnClickListener { 
    val intent = Intent(requireContext(), Activity2::class.java)
                intent.putExtra("user_Id", user_Id)
                startActivity(intent)
 }

活动2

val intent = getIntent()
val message = intent.getStringExtra("user_Id")

val bundle = Bundle()
bundle.putString("user_Id", message) //the part i updated
val fragmet= fragment_B()   
fragmet.setArguments(bundle)

片段B

override fun onCreateView(
    inflater: LayoutInflater, container: ViewGroup?,
    savedInstanceState: Bundle?
): View? {
    return inflater.inflate(R.layout.fragment_B, container, false)
    val bundle = this.arguments //the part i updated
    val myString = bundle!!.getString("user_Id", "defaultValue") //the part i updated
}

1-阅读单例设计模式 在所有应用程序中使用户 ID 全局化

  1. read it to understand

2-Shared Preference : 保存用户 ID 以供所有应用程序使用

  1. Shared Preferences

3-您可以尝试 Navigation Args :更好地选择性能 并可以使用它在您的应用程序中移动

  1. Navigation