Skip to content

Instantly share code, notes, and snippets.

@mikkipastel
Created October 6, 2022 04:11
Show Gist options
  • Save mikkipastel/5d672400883f39dfa8527b175fe11b98 to your computer and use it in GitHub Desktop.
Save mikkipastel/5d672400883f39dfa8527b175fe11b98 to your computer and use it in GitHub Desktop.
add SecondFragment
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white">
<TextView
android:id="@+id/textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Second Activity"
android:textSize="24sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
package com.ascedncorp.androidbasic
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import com.ascedncorp.androidbasic.databinding.FragmentMainBinding
import com.ascedncorp.androidbasic.databinding.FragmentSecondBinding
class SecondFragment: Fragment() {
private lateinit var binding: FragmentSecondBinding
companion object {
const val EXTRA_TEXT = "SecondFragment:EXTRA_TEXT"
fun newInstance(text: String) = SecondFragment().apply {
arguments = Bundle().apply {
putString(EXTRA_TEXT, text)
}
}
}
private val text by lazy {
arguments?.getString(EXTRA_TEXT)
}
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?,
): View {
binding = FragmentSecondBinding.inflate(inflater, container, false)
return binding.root
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
binding.textview.text = text
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment