Posts

Showing posts from March, 2022

Delay using coroutine, splash activity delay, kotlin global scope

  override fun onViewCreated ( view : View , savedInstanceState : Bundle ?) { super . onViewCreated ( view , savedInstanceState ) ( activity as MainActivity ). hideBottomNavigationView () lifecycleScope . launch { delay ( 1000 ) //check if user is already logged in if ( FirebaseAuth . getInstance (). currentUser != null ) { try { view . findNavController (). navigate ( R . id . action_fragment_splash_screen_to_homeFragment ) } catch ( e : Exception ){ e . printStackTrace () } } else { if ( findNavController (). currentDestination ?. id == R . id . fragment_splash_screen ) { try { view . findNavController (). navigate ( R . id . splash_screen_to_login_fragment ) } catch ( e : Exception ){ e . printStackTrace () } } } }

Handler and looper in splash

  if ( activity ?. intent ?. extras == null ) { Handler ( Looper . getMainLooper ()). postDelayed ( { //check if user is already logged in if ( FirebaseAuth . getInstance (). currentUser != null ) { try { findNavController (). navigate ( R . id . action_splashScreenFragment_to_inStoreTransactionsFragment ) } catch ( e : Exception ){ e . printStackTrace () } } else { try { view . findNavController (). navigate ( R . id . splash_screen_to_login_fragment ) } catch ( e : Exception ) { e . printStackTrace () } } } , 1000 ) }

RecyclerView on a dialog box iin Android

  dialog = Dialog ( requireActivity ()) binding . btnMenu . setOnClickListener { dialog = Dialog ( requireActivity ()) dialog . requestWindowFeature ( Window . FEATURE_NO_TITLE ) dialog . setCancelable ( true ) val dialogBinding = MenuDialogLayoutBinding . inflate ( LayoutInflater . from ( requireContext ())) dialog . setContentView ( dialogBinding . root ) dialogBinding . menuRv . layoutManager = GridLayoutManager ( requireContext (), 3 ) dialogBinding . menuRv . adapter = MenuItemAdapter ( requireContext (), menuItems , this ) dialog . show () } Dialog box xml:- <? xml version ="1.0" encoding ="utf-8" ?> < androidx.constraintlayout.widget.ConstraintLayout xmlns: android ="http://schemas.android.com/apk/res/android" android :layout_width ="match_parent" android :layout_height ="match_parent" android :layout_gravity ="center" xmlns: app ="http://schemas.andr...

Double back press to exit the application best method (android) in fragment

  private var backPressedTime : Long = 0 if ( backPressedTime + 2000 > System . currentTimeMillis ()) { requireActivity (). finishAffinity () } else { showToast ( MyApplication . context !!, "Press Back again to exit " , IndiaHaatConstants . ToastType . CUSTOM ) } backPressedTime = System . currentTimeMillis ()