Home > Mobile >  Get access to a component from other activity (Kotlin with Android Studio)
Get access to a component from other activity (Kotlin with Android Studio)

Time:12-30

I have the following problem: I would like to access the component (linearLayout4) of another activity from one activity.

As you can see in the picture, the LinearLayout4 is not found because it is in another activity.

How can I access LinearLayout4? Thanks in advance! Steve

CodePudding user response:

You can't actually access that same instance of the view from another activity, however you can get that view from another activity's layout using layout inflater:

val firstActivityView = LayoutInflater.from(requireContext()).inflate(R.layout.activity_1,binding.cont,false)

Here you'll get the whole layout of first activity and then you can call

firstActivityView.findViewById(R.id.linearlayout4)

  • Related