skip to Main Content

I have a Fragment_A on Activity_1 and whenever a Button is clicked in the Fragment_A I have to start Activity_2 witch have a Fragment_B

So i need to send a user_Id from the Fragment_A in Activity 1 to the Fragment_B in activity_2.
Is there any way to send the data directly from Frament_A in Activity_1 to Fragment_B in Activity_2?

I am a student , so i am a beginner and i did found this question here but i didn’t understand what to do to solve this
I did find some solution about interface but i didn’t know how to do it

i keep getting class_id null

Language : 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 : Acttivity2 who has the fragment iwant to send it the 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 : fragment 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()
                    }
                }
            }

2

Answers


  1. fragmentA

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

    activity2

    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)
    

    fragmentB

    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
    }
    
    Login or Signup to reply.
  2. 1-Read About Singleton Design Pattern
    for make user id global in all app

    1. read it to understand

    2-Shared Preference :save user id to can using for all app

    1. Shared Preferences

    3-you can try Navigation Args : better choose for performance
    and can use it for movement through you app

    1. Navigation
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search