app>src>main>java>com.example.tabapplication>MainActivity.kt
MainActivityのonCreateにhttps://www.google.comをセットしたstartActivityを追加
app>src>main>java>com.example.tabapplication>ui>home>HomeFragment.kt
HomeFragmentのonCreateViewにhttps://www.google.comをセットしたstartActivityを追加
実行
開始時にWebを表示。
戻って、Notificationsを選択して、Homeを選択時にWebを表示。
MainActivityのonCreateにhttps://www.google.comをセットしたstartActivityを追加
class MainActivity : AppCompatActivity() { private lateinit var binding: ActivityMainBinding override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) binding = ActivityMainBinding.inflate(layoutInflater) setContentView(binding.root) val navView: BottomNavigationView = binding.navView val navController = findNavController(R.id.nav_host_fragment_activity_main) // Passing each menu ID as a set of Ids because each // menu should be considered as top level destinations. val appBarConfiguration = AppBarConfiguration( setOf( R.id.navigation_home,, R.id.navigation_notifications ) ) setupActionBarWithNavController(navController, appBarConfiguration) navView.setupWithNavController(navController) } }
→
class MainActivity : AppCompatActivity() { private lateinit var binding: ActivityMainBinding override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) binding = ActivityMainBinding.inflate(layoutInflater) setContentView(binding.root) val navView: BottomNavigationView = binding.navView val navController = findNavController(R.id.nav_host_fragment_activity_main) // Passing each menu ID as a set of Ids because each // menu should be considered as top level destinations. val appBarConfiguration = AppBarConfiguration( setOf( R.id.navigation_home, R.id.navigation_notifications ) ) setupActionBarWithNavController(navController, appBarConfiguration) navView.setupWithNavController(navController) val url = "https://www.google.com" val intent = Intent(Intent.ACTION_VIEW) intent.data = Uri.parse(url) startActivity(intent) } }
HomeFragmentのonCreateViewにhttps://www.google.comをセットしたstartActivityを追加
class HomeFragment : Fragment() { private lateinit var homeViewModel: HomeViewModel private var _binding: FragmentHomeBinding? = null // This property is only valid between onCreateView and // onDestroyView. private val binding get() = _binding!! override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle? ): View? { homeViewModel = ViewModelProvider(this).get(HomeViewModel::class.java) _binding = FragmentHomeBinding.inflate(inflater, container, false) val root: View = binding.root val textView: TextView = binding.textHome homeViewModel.text.observe(viewLifecycleOwner, Observer { textView.text = it }) return root } override fun onDestroyView() { super.onDestroyView() _binding = null } }
→
class HomeFragment : Fragment() { private lateinit var homeViewModel: HomeViewModel private var _binding: FragmentHomeBinding? = null // This property is only valid between onCreateView and // onDestroyView. private val binding get() = _binding!! override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle? ): View? { homeViewModel = ViewModelProvider(this).get(HomeViewModel::class.java) _binding = FragmentHomeBinding.inflate(inflater, container, false) val root: View = binding.root val textView: TextView = binding.textHome homeViewModel.text.observe(viewLifecycleOwner, Observer { textView.text = it }) val url = "https://www.google.com" val intent = Intent(Intent.ACTION_VIEW) intent.data = Uri.parse(url) startActivity(intent) return root } override fun onDestroyView() { super.onDestroyView() _binding = null } }
開始時にWebを表示。
戻って、Notificationsを選択して、Homeを選択時にWebを表示。