Soundlicious
10/29/2018, 3:38 AMSam
10/29/2018, 4:14 AMokarm
10/29/2018, 3:52 PMSoundlicious
10/30/2018, 2:08 AMokarm
10/30/2018, 11:57 AMgetActivity()
to your concrete Activity type.
// somewhere inside inside onCreateView, assuming the fragment's containing activity is an instance of ProfileDetailActivity
val userId = (activity as ProfileDetailActivity?)?.userId ?: -1
myViewModel.getUserDetail(userId)
//viewModel handles the case when userId == -1 and sets the state accordingly, otherwise fetches profile detail
// OR IF this fragment could be used inside multiple known Activities, use something like this:
val userId = activity?.let { activity ->
when(activity) {
is Activity1 -> activity.getIdFromActivity1() // will be smart cast to type Activity1 for you
is Activity2 -> activity.getIdFromActivity2() //will be smart cast to type Activity2 for you
else -> throw IllegalStateException("Containing activity is not of valid type! Actual type: ${activity::class.simpleName}")
}
} ?: -1