Skip to content

Instantly share code, notes, and snippets.

@dntf-studio
Created May 4, 2021 02:45
Show Gist options
  • Save dntf-studio/15eac00a01ae7aa466620ef8c23747dd to your computer and use it in GitHub Desktop.
Save dntf-studio/15eac00a01ae7aa466620ef8c23747dd to your computer and use it in GitHub Desktop.
フラグメントからアクティビティ移動
package com.dntf.class_omitter.ui.home
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.TextView
import androidx.fragment.app.Fragment
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import com.dntf.class_omitter.R
import com.dntf.class_omitter.ui.chemis1
class HomeFragment : Fragment() {
private lateinit var homeViewModel: HomeViewModel
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
homeViewModel =
ViewModelProvider(this).get(HomeViewModel::class.java)
val root = inflater.inflate(R.layout.fragment_home, container, false)
val textView: TextView = root.findViewById(R.id.text_home)
homeViewModel.text.observe(viewLifecycleOwner, Observer {
textView.text = it
})
return root
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val btn1 : Button = view.findViewById(R.id.button)
val c = context
btn1.setOnClickListener {
if (c != null) {
chemis1(c)
}
}
}
private fun chemis1(context: Context){
val intent = Intent(context,chemis1::class.java)
startActivity(intent)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment