How to keep user loged in using SharedPreferences in Kotlin? - kotlin

i have a problem here, i want to make login and register using SharedPreferences. The pass and getting data is already done and no error, but idk how to keep user loged in and everytime user close the app it will automatically go to my HomeFragment. And user can go to login again if they click logout button in my HomeFragment
this is my LoginFragment
override fun onCreateView(
...
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
binding.btnSignIn.setOnClickListener {
getPrefData()
}
binding.tvRegister.setOnClickListener{
navControllerRegister()
}
}
private fun getPrefData(){
...
}
private fun navControllerSignIn(){
...
}
private fun navControllerRegister(){
...
}
this is my HomeFragment
override fun onCreateView(
...
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val pref = requireActivity().getSharedPreferences(Data.Preferences.PREF_ID, Context.MODE_PRIVATE)
binding.btnLogOut.setOnClickListener {
val editor = pref.edit()
editor.remove(Data.Preferences.IS_LOGIN)
editor.clear()
val intent = Intent(requireActivity(), LoginFragment::class.java)
startActivity(intent)
}
}
and this is my constant data
object Data{
object Preferences{
const val PREF_ID = "CHALLENGE_CHAPTER_4"
const val IS_LOGIN = "IS_LOGIN"
const val PREF_NAME = "NAME"
const val PREF_EMAIL = "EMAIL"
const val PREF_PASS = "PASS"
const val PREF_CONFIRM_PASS = "CONFIRM_PASS"
}
}

Related

Start get request in Fragment A and show the data from response in Fragment B

trying to add a recyclerview to a fragment is driving me crazy...
I have Fragment A where I use a button to start 2 retrofit request, the first request fetches a token, that is used for the second request. When debugging I can see that the requests work and the needed data is fetched.
Then I have Fragment B, there I want the data from the second request to been shown in a recyclerview.
So first, is this easily handable or should I try to move the call of the second request in the Fragment B? (In that case I would have to pass the values entered in 2 edittext-fields in Fragment A to Fragment B).
To begin slowly I tried to create the recyclerview in Fragment A, having the request and the recyclerview in the same Fragment.
It worked:
class AddStalkerFragment : Fragment(R.layout.fragment_add_stalker) {
private lateinit var binding: FragmentAddStalkerBinding
private lateinit var sessionManager: SessionManager
private lateinit var tvCategoryAdapter: TvCategoryAdapter
val viewModel: StalkerViewModel by lazy {
ViewModelProvider(this)[StalkerViewModel::class.java]
}
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
binding = FragmentAddStalkerBinding.inflate(inflater, container, false)
val view = binding.root
return view
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
prepareRecyclerView()
sessionManager = SessionManager(this#AddStalkerFragment.requireActivity())
val stalkerUrl = binding.etServerUrl.text
val macAddress = binding.etMacAddress.text
val buttonSave = binding.btnSaveStalker
buttonSave.setOnClickListener {
viewModel.getToken(
url = stalkerUrl.toString(),
cookie = "mac=${macAddress};stb_lang=en;timezone=Europe/Amsterdam;",
)
viewModel.getTvCategory(
url = stalkerUrl.toString(),
cookie = "mac=${macAddress};stb_lang=en;timezone=Europe/Amsterdam;",
token = "Bearer ${sessionManager.fetchAuthToken()}"
)
}
viewModel.getTokenLiveData.observe(viewLifecycleOwner) { response ->
if (response == null) {
Toast.makeText(
this#AddStalkerFragment.requireActivity(),
"Token Error",
Toast.LENGTH_SHORT
).show()
return#observe
}
val loginResponse = response.js.token
Toast.makeText(
this#AddStalkerFragment.requireActivity(),
"Token Ok",
Toast.LENGTH_SHORT
).show()
sessionManager.saveAuthToken(loginResponse)
}
viewModel.getTvCategoryLiveData.observe(viewLifecycleOwner) { response ->
if (response == null) {
Toast.makeText(
this#AddStalkerFragment.requireActivity(),
"Categories Error",
Toast.LENGTH_SHORT
).show()
return#observe
}
Toast.makeText(
this#AddStalkerFragment.requireActivity(),
"Categories Ok",
Toast.LENGTH_SHORT
).show()
tvCategoryAdapter.setTvGenresList(response.js)
}
}
private fun prepareRecyclerView() {
tvCategoryAdapter = TvCategoryAdapter()
binding.rvTest.apply {
layoutManager = LinearLayoutManager(context)
adapter = tvCategoryAdapter
}
}
}
And here is what I try in Fragment B to show the recyclerview with the data from Fragment A
Debugging"tvCategoryAdapter.setTvGenresList(response)" doesn't work in Fragment B, so it isn't even getting there :-)
I assume that I have to connect the two Fragments somehow (I did in Navigation Graph, is this enough?)
class TvCategoryFragment : Fragment(R.layout.fragment_tv_category) {
private lateinit var binding: FragmentTvCategoryBinding
private lateinit var tvCategoryAdapter: TvCategoryAdapter
val viewModel: StalkerViewModel by lazy {
ViewModelProvider(this)[StalkerViewModel::class.java]
}
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
binding = FragmentTvCategoryBinding.inflate(inflater, container, false)
val view = binding.root
return view
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
prepareRecyclerView()
viewModel.getTvCategoryLiveData.observe(viewLifecycleOwner) { response ->
if (response == null) {
Toast.makeText(
this#TvCategoryFragment.requireActivity(),
"Categories Error",
Toast.LENGTH_SHORT
).show()
return#observe
}
Toast.makeText(
this#TvCategoryFragment.requireActivity(),
"Categories Ok",
Toast.LENGTH_LONG
).show()
tvCategoryAdapter.setTvGenresList(response.js)
}
}
private fun prepareRecyclerView() {
tvCategoryAdapter = TvCategoryAdapter()
binding.rvLayoutTvCategory.apply {
layoutManager = LinearLayoutManager(context)
adapter = tvCategoryAdapter
}
}
}
My adapter looks like this:
class TvCategoryAdapter : RecyclerView.Adapter<TvCategoryAdapter.ViewHolder>() {
private var genreList = ArrayList<J>()
fun setTvGenresList(genreList: List<J>) {
this.genreList = genreList as ArrayList<J>
notifyDataSetChanged()
}
class ViewHolder(val binding: RvItemTvcategoryBinding) : RecyclerView.ViewHolder(binding.root) {}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
return ViewHolder(
RvItemTvcategoryBinding.inflate(
LayoutInflater.from(
parent.context
)
)
)
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
holder.binding.rvItemTvcategory.text = genreList[position].title
}
override fun getItemCount(): Int {
return genreList.size
}
}
Any hint that could help me to solve this? :-)
thanks in advance

How to use the searchView widget in a Fragment (Kotlin)

I'm trying to use the searchView widget in a fragment. I can't find a way to do it. It's easier to implement a searchView using an Activity but I have no clue how to do implement it in a fragment in Kotlin. I tried this way but doesn't seem to work ( only works in an activity). I'm a beginner in Android and would really appreciate some help here, i've been struggling for a couple of days rn.
class Homepage : Fragment() {
private var layoutManager : RecyclerView.LayoutManager? = null
private var _binding : FragmentHomepageBinding? = null
private val binding get() = _binding!!
private val shoe = DataSource.shoes
val arrayList = ArrayList<Shoe>()
val displayList = ArrayList<Shoe>()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
_binding = FragmentHomepageBinding.inflate(inflater, container, false)
return binding.root
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val myAdapter = ShoeAdapter(displayList)
layoutManager = GridLayoutManager(this.context,2)
recyclerView.layoutManager = layoutManager
recyclerView.adapter = myAdapter
setHasOptionsMenu(true)
}
override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
inflater.inflate(R.menu.main_menu,menu)
val menuItem = menu.findItem(R.id.search)
val searchView = menuItem.actionView as SearchView
searchView.setOnQueryTextListener(object : SearchView.OnQueryTextListener{
override fun onQueryTextSubmit(query: String?): Boolean {
return true
}
override fun onQueryTextChange(newText: String?): Boolean {
if(newText!!.isNotEmpty()){
displayList.clear()
val search = newText.lowercase(Locale.getDefault())
arrayList.forEach {
if(it.name.lowercase(Locale.getDefault()).contains(search)){
displayList.add(it)
}
}
recyclerView.adapter!!.notifyDataSetChanged()
}
else{
displayList.clear()
displayList.addAll(arrayList)
recyclerView.adapter!!.notifyDataSetChanged()
}
return true
}
})
super.onCreateOptionsMenu(menu, inflater)
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
return super.onOptionsItemSelected(item)
}
}```
I used it as below before and it was working properly.
class BooksFragment : Fragment(), SearchView.OnQueryTextListener {
private lateinit var binding: FragmentBooksBinding
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View {
binding = FragmentBooksBinding.inflate(inflater, container, false)
binding = DataBindingUtil.inflate(inflater, R.layout.fragment_books, container, false)
return binding.root
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
setHasOptionsMenu(true)
}
override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
inflater.inflate(R.menu.toolbar_menu,menu)
val item = menu.findItem(R.id.search_book)
val searchView = item.actionView as SearchView
searchView.setOnQueryTextListener(this)
super.onCreateOptionsMenu(menu, inflater)
}
override fun onQueryTextSubmit(query: String): Boolean {
// search button submit
return true
}
override fun onQueryTextChange(newText: String): Boolean {
// search text changed
return true
}

How to pass data from a BottomSheetFragment to another fragment?

I have an Activity, called 'HomeActivity', where I have a bottom navigation menu and four fragments. The activity has a FloatingActionButton and when I hit this, it opens a bottomSheetFragment which contain a RecyclerView and when I hit an item from it, I want some values to send to the fragment ('HomeFragment') which is part of the 'HomeActivity'
After doing a lot of searches online, I have done the following but I am not able to get the data in the fragment.
Following is the fragment from which I want to send the data.
'HomeBottomSheetFragment.kt'
class HomeBottomSheetFragment: BottomSheetDialogFragment() {
private lateinit var binding: FragmentHomeBottomSheetBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
FirestoreClass().getCategoryList(this)
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?
): View {
binding = FragmentHomeBottomSheetBinding.inflate(inflater, container, false)
return binding.root
}
fun successCategoryList(categoryList: ArrayList<ProdCategories>) {
binding.rvHomeCategories.visibility = View.VISIBLE
binding.rvHomeCategories.layoutManager = GridLayoutManager(context, 4)
binding.rvHomeCategories.setHasFixedSize(true)
val categoryAdapter = CategoryListAdapter(requireContext(), categoryList)
binding.rvHomeCategories.adapter = categoryAdapter
categoryAdapter.setOnClickListener(object :CategoryListAdapter.OnClickListener{
override fun onClick(position: Int, category: ProdCategories) {
val myFragment = HomeFragment()
val bundle = Bundle()
bundle.putString("category", category.category_name)
myFragment.arguments = bundle
fragmentManager?.beginTransaction()?.replace(R.id.nav_host_fragment,HomeFragment())?.commit()
}
})
}
}
Following is the adapter of the above fragment ('HomeBottomSheetFragment.kt'), 'CategoryListAdapter.kt'.
open class CategoryListAdapter(private val context: Context, private var list: ArrayList<ProdCategories>)
: RecyclerView.Adapter<RecyclerView.ViewHolder>() {
private var onClickListener: OnClickListener? = null
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
return MyViewHolder(
HomeBottomsheetCategoryListLayoutBinding.inflate(
LayoutInflater.from(
parent.context
), parent, false
)
)
}
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
val model = list[position]
if (holder is MyViewHolder) {
GlideLoader(context).loadProductPicture(model.category_image, holder.binding.ivCategoryImage)
holder.binding.tvCategoryName.text=model.category_name
}
holder.itemView.setOnClickListener {
if (onClickListener != null) {
onClickListener!!.onClick(position, model)
}
}
}
override fun getItemCount(): Int {
return list.size
}
fun setOnClickListener(onClickListener: OnClickListener) {
this.onClickListener = onClickListener
}
interface OnClickListener{
fun onClick(position: Int, category: ProdCategories)
}
private class MyViewHolder(val binding: HomeBottomsheetCategoryListLayoutBinding) : RecyclerView.ViewHolder(binding.root)
}
Following is the fragment where I want to receive the data.
class HomeFragment : BaseFragment() {
private var filterCategory: String?= null
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?
): View? {
binding = FragmentHomeBinding.inflate(inflater, container, false)
val bundle = this.arguments
if (bundle!=null) {
filterCategory = bundle.getString("category")
}
return binding.root
}
}
All I know is that I am not doing things right, please help me fix it.
In this line,
fragmentManager?.beginTransaction()?.replace(R.id.nav_host_fragment,HomeFragment())?.commit()
Replace HomeFragment() with myFragment because that's where you are attaching the bundle.
See if this solves your problem.

I am really stuck right now on moving to a different activity from a fragment

This is my Code for CallFragment.kt , when i click on btnSetupVideoCall nothing happens whereas i should be going to different activity.
class CallFragment : Fragment() {
private var param1: String? = null
private var param2: String? = null
private lateinit var binding: FragmentCallBinding
companion object {
#JvmStatic
fun newInstance(param1: String, param2: String) =
CallFragment().apply {
arguments = Bundle().apply {
putString(ARG_PARAM1, param1)
putString(ARG_PARAM2, param2)
}
}
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_call, container, false)
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
arguments?.let {
param1 = it.getString(ARG_PARAM1)
param2 = it.getString(ARG_PARAM2)
}
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
binding = FragmentCallBinding.inflate(layoutInflater)
binding.btnSetupVideoCall.setOnClickListener{
(activity as MainActivity?)?.let {
val intent = Intent(it, One_to_One_VC::class.java)
it.startActivity(intent)
}
}
}
}
And this is how i made fragments in mainactivity.kt
class MainActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
val view = binding.root
setContentView(view)
val callFragment = CallFragment()
val chatFragment = ChatFragment()
makeCurrentFragment(callFragment)
binding.btmNvg.setOnNavigationItemSelectedListener {
when (it.itemId) {
R.id.ic_call -> makeCurrentFragment(callFragment)
R.id.ic_chat -> makeCurrentFragment(chatFragment)
else -> makeCurrentFragment(callFragment)
}
true
}
}
override fun onBackPressed() {
if(binding.btmNvg.selectedItemId == R.id.ic_call){
super.onBackPressed()
finish()
}else{
makeCurrentFragment(CallFragment())
}
}
private fun makeCurrentFragment(fragment: Fragment) =
supportFragmentManager.beginTransaction().apply {
replace(R.id.fl_wrapper , fragment)
commit()
}
}
Logcat when i click the button
The only option left for me is to make another UI since it is starting of the project only but help would be really appreciated
onCreateView should use FragmentCallBinding.inflate
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
binding = FragmentCallBinding.inflate(inflater)
return binding.root
}
onViewCreated should not assign the binding variable
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
binding.btnSetupVideoCall.setOnClickListener{
(activity as MainActivity?)?.let {
val intent = Intent(it, One_to_One_VC::class.java)
it.startActivity(intent)
}
}
}

How to add ViewModel to a project which uses ViewPager and Tablayout?

Hello i am new in Android Development so maybe this question can be weird for you.
I have a class named IdentityCardInfo which has variables. And I am getting this variables in InfoFragment.
From InfoFragment I want to show these variables in a ResultsFragment wihch has 3 tabs with tablayout.
I want to use ViewModel to pass data to tablayouts but I don't know how to use I've searched but get stuck.
Here is code for IdentityCardInfo class:
class IdentityCardInfo {
companion object AppConstant{
const val serieNo ="67G74444"
const val validDate = "01/01/2022"
const val dateOfBirth = "10/08/1998"
const val fullName = "Muhittin KAYA"
const val identityNo: Long = 11111111111
const val gender = "MALE"
const val nationality = "TUR"
}
}
Here is code for InfoFragment:
class InfoFragment : BaseFragment() {
lateinit var navController: NavController
companion object {
private const val TAG = "Info Fragment"
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_info, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
navController = Navigation.findNavController(view)
button_info_idcard.setOnClickListener {
//start OcrActivity
val intent = Intent(activity, OcrActivity::class.java)
startActivityForResult(intent, 101)
}
imagebutton_info_settings.setOnClickListener {
navController.navigate(R.id.action_infoFragment_to_settingsFragment)
}
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
Log.i("Muhittin", "onActivityResult()")
if (requestCode == 101) {
val message = data?.getStringExtra("TEST_TEXT")
Toast.makeText(context, message, Toast.LENGTH_SHORT).show()
val serieNo = IdentityCardInfo.serieNo
val validDate = IdentityCardInfo.validDate
val dateOfBirth = IdentityCardInfo.dateOfBirth
val fullname = IdentityCardInfo.fullName
val gender = IdentityCardInfo.gender
val identityNo = IdentityCardInfo.identityNo
val nationality = IdentityCardInfo.nationality
val bundle = bundleOf(
"TEST_TEXT" to message,
"SERIE_NO" to serieNo,
"VALID_DATE" to validDate,
"DOB" to dateOfBirth,
"FULL_NAME" to fullname,
"GENDER" to gender,
"IDENTITY_NO" to identityNo,
"NATIONALITY" to nationality
)
navController.navigate(
R.id.action_infoFragment_to_detailFragment,
bundle
)
}
}
}
This is for ResultsFragment: (I don't add tablayout code and viewpager code)
class ResultsFragment : Fragment() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
retainInstance = true
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_results, container, false)
}
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
val tabLayout: TabLayout = requireView().findViewById(R.id.tabLayout_results)
tabLayout.tabGravity = TabLayout.GRAVITY_FILL
val tabTitles = arrayOf(
resources.getString(R.string.result_tab1_name),
resources.getString(R.string.result_tab2_name),
resources.getString(R.string.result_tab3_name)
)
val viewPager: ViewPager = requireView().findViewById(R.id.viewpager_results)
viewPager.offscreenPageLimit = 3
val adapter = PagerAdapter(fragmentManager, tabTitles)
viewPager.adapter = adapter
tabLayout.setupWithViewPager(viewPager)
}
}
If you want to use ViewModels to pass data between fragments in the same activity, here's the Android documentation about it. It lets the fragments access a shared object without needing to know about each other, or communicate through the activity (with a bunch of code just to enable that communication)
You'd basically do something like this:
// create a ViewModel that holds the data you want to share
class IdentityCardViewModel : ViewModel() {
val identityInfo = MutableLiveData<IdentityCardInfo>()
fun setInfo(info: IdentityCardInfo) {
identityInfo.value = info
}
}
Then each fragment would grab a copy of the same ViewModel instance by doing:
// Get a reference to the IdentityCardViewModel created and held by the activity
private val model: IdentityCardViewModel by activityViewModels()
and then InfoFragment can set the data with model.setInfo(yourIdentityCardInfo). ResultsFragmentcan observe this data and do something whenever it sees a new value:
model.identityInfo.observe(viewLifecycleOwner, Observer<IdentityCardInfo> { info ->
// Do whatever you need with the updated info, like displaying it
})
#muhittin kaya Refer below code to resolve your issue
InfoFragment
class InfoFragment : BaseFragment() {
lateinit var navController: NavController
// Initialize your variable
lateinit var infoFragmentInterface: InfoFragmentDataInterface
// If you get initialization exception then uncomment below line and comment above line
//private var infoFragmentInterface: InfoFragmentDataInterface? = null
companion object {
private const val TAG = "Info Fragment"
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_info, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
navController = Navigation.findNavController(view)
button_info_idcard.setOnClickListener {
//start OcrActivity
val intent = Intent(activity, OcrActivity::class.java)
startActivityForResult(intent, 101)
}
imagebutton_info_settings.setOnClickListener {
navController.navigate(R.id.action_infoFragment_to_settingsFragment)
}
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
Log.i("Muhittin", "onActivityResult()")
if (requestCode == 101) {
val message = data?.getStringExtra("TEST_TEXT")
Toast.makeText(context, message, Toast.LENGTH_SHORT).show()
val serieNo = IdentityCardInfo.serieNo
val validDate = IdentityCardInfo.validDate
val dateOfBirth = IdentityCardInfo.dateOfBirth
val fullname = IdentityCardInfo.fullName
val gender = IdentityCardInfo.gender
val identityNo = IdentityCardInfo.identityNo
val nationality = IdentityCardInfo.nationality
val bundle = bundleOf(
"TEST_TEXT" to message,
"SERIE_NO" to serieNo,
"VALID_DATE" to validDate,
"DOB" to dateOfBirth,
"FULL_NAME" to fullname,
"GENDER" to gender,
"IDENTITY_NO" to identityNo,
"NATIONALITY" to nationality
)
//Add This line for passing your data
//You can also pass string data here
infoFragmentInterface.getInfoFragmentData(bundle)
navController.navigate(
R.id.action_infoFragment_to_detailFragment,
bundle
)
}
}
}
// Create this interface to pass your data
interface InfoFragmentDataInterface {
fun getInfoFragmentData(bundle: Any)
}
ResultsFragment
class ResultsFragment : Fragment(), InfoFragmentDataInterface {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
retainInstance = true
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_results, container, false)
}
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
val tabLayout: TabLayout = requireView().findViewById(R.id.tabLayout_results)
tabLayout.tabGravity = TabLayout.GRAVITY_FILL
val tabTitles = arrayOf(
resources.getString(R.string.result_tab1_name),
resources.getString(R.string.result_tab2_name),
resources.getString(R.string.result_tab3_name)
)
val viewPager: ViewPager = requireView().findViewById(R.id.viewpager_results)
viewPager.offscreenPageLimit = 3
val adapter = PagerAdapter(fragmentManager, tabTitles)
viewPager.adapter = adapter
tabLayout.setupWithViewPager(viewPager)
}
override fun getInfoFragmentData(bundle: Any) {
// You'll get your Bundle data here.
}
}