how to delay splash fragment time to complete lottie animation - android

This is my splashfragment. I have created the lottie animation which is around 3sec long and it work fine but screen changes before the animation is completed.
class SplashFragment : Fragment() {
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?) =
FragmentSplashBinding.inflate(inflater, container, false).root
}

For Delay Use Handler
Handler(Looper.getMainLooper()).postDelayed({
//Do something after 3sc
}, 3000)

lottieAnimationView.addAnimatorListener(object : AnimatorListener{
override fun onAnimationStart(p0: Animator?) {
Log.d("Lottie","Animation start")
}
override fun onAnimationEnd(p0: Animator?) {
Log.d("Lottie","Animation end")
}
override fun onAnimationCancel(p0: Animator?) {
Log.d("Lottie","Animation Cancelled")
}
override fun onAnimationRepeat(p0: Animator?) {
Log.d("Lottie","Animation repeat")
}
})
Go to next screen when onAnimationEnd triggered.

Related

Is it okay to delete a CompanionObject on a fragment?

So I'm working with Android Studio, and I found a video on youtube on how to set tab layout. I'm trying to create a fragment with new>Fragment>Fragment(Blank) and I'm following youtube instructions to remove everything else except onCreate and onCreateView in Fragment class. But when i try to put everything together, i get this error:
Classifier 'PemasukanFragment' does not have a companion object, and thus must be initialized here
I use AddActivity class to hold my fragment, the code look like this:
class AddActicity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_add_acticity)
setUpTabs()
}
private fun setUpTabs(){
val adapter= Adapter_AddActivity(supportFragmentManager)
adapter.addFragment(PemasukanFragment,"Pemasukan")
}
}
I have 2 fragment pages. The code look like this:
class PemasukanFragment : Fragment() {
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
arguments?.let {
}
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_pemasukan, container, false)
}
}
The other fragment page have a similar code to this. I also have adapter that look like this:
class Adapter_AddActivity(supportFragmentManager: FragmentManager) :
FragmentPagerAdapter(supportFragmentManager, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT) {
private val mFragmentList = ArrayList<Fragment>()
private val mFragmentListTitle = ArrayList<String>()
override fun getCount(): Int {
return mFragmentList.size
}
override fun getItem(position: Int): Fragment {
return mFragmentList[position]
}
override fun getPageTitle(position: Int): CharSequence? {
return mFragmentListTitle[position]
}
fun addFragment(fragment: Fragment, title: String){
mFragmentList.add(fragment)
mFragmentListTitle.add(title)
}
}
I don't know if companion objects can be removed. But if it can, how to solve this error? Any help is appreciated

Show products from server with diffrent sorts

I'm working on an ecommerce Android application using MVVM & RXJAVA & KOIN with kotlin and the product request URL has query type parameters which will show latest products, most popular product, most expensive to cheap and most cheap to expensive and I want to show sort in main page with RecyclerView but all of my RecyclerViews showing the same thing. How do I fix this?
interface ApiService{
#GET("product/list")
fun getLatestProducts(#Query("sort")sort:String):Single<List<Product>>
}
class MainViewModel(productRepository: ProductRepository,bannerRepository: BannerRepository):NikeViewModel() {
val productLiveData = MutableLiveData<List<Product>>()
val productLiveData1 = MutableLiveData<List<Product>>()
val progressBarLiveData = MutableLiveData<Boolean>()
val bannerLiveData=MutableLiveData<List<Banner>>()
init {
progressBarLiveData.value = true
productRepository.getLatestProducts(SORT_LATEST)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.doFinally { progressBarLiveData.value = false }
.subscribe(object : SingleObserver<List<Product>> {
override fun onSubscribe(d: Disposable) {
compositeDisposable.add(d)
}
override fun onSuccess(t: List<Product>) {
productLiveData.value = t
}
override fun onError(e: Throwable) {
TODO("Not yet implemented")
}
})
productRepository.getLatestProducts(SORT_POPULAR)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.doFinally { progressBarLiveData.value = false }
.subscribe(object : SingleObserver<List<Product>> {
override fun onSubscribe(d: Disposable) {
compositeDisposable.add(d)
}
override fun onSuccess(t: List<Product>) {
productLiveData1.value = t
}
override fun onError(e: Throwable) {
TODO("Not yet implemented")
}
})
class MainFragment:NikeFragment(),ProductListAdapter.ProductOnClickedListener {
val mainViewModel:MainViewModel by viewModel()
val productListAdapter:ProductListAdapter by inject()
val productListAdapter2:ProductListAdapter by inject()
private val TAG = "MainFragment"
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return inflater.inflate(R.layout.home_fragment,container,false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
latest_product_rv.layoutManager=
LinearLayoutManager(requireContext(),RecyclerView.HORIZONTAL,false)
productListAdapter.productOnClickedListener=this
popular_product_rv.layoutManager=
LinearLayoutManager(requireContext(),RecyclerView.HORIZONTAL,false)
popular_product_rv.adapter=productListAdapter
val bannerSlider=view.findViewById<ViewPager2>(R.id.bannerSliderMain)
mainViewModel.productLiveData.observe(viewLifecycleOwner){
latest_product_rv.adapter=productListAdapter
productListAdapter.products=it as ArrayList<Product>
}
mainViewModel.productLiveData1.observe(viewLifecycleOwner){
popular_product_rv.adapter=productListAdapter
productListAdapter2.products=it as ArrayList<Product>
}
You're setting the same adapter on both.
This
mainViewModel.productLiveData1.observe(viewLifecycleOwner){
popular_product_rv.adapter=productListAdapter
productListAdapter2.products=it as ArrayList<Product>
}
should be
mainViewModel.productLiveData1.observe(viewLifecycleOwner){
popular_product_rv.adapter=productListAdapter2
productListAdapter2.products=it as ArrayList<Product>
}

When app is onPause my DialogFragment is shown multiple times

I have a custom dialog in my App and i actually open it from my fragment like this:
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
btnScarica = view.findViewById(R.id.btnScarica)
btnScarica.setOnClickListener {
ScaricoDialog(articoliViewModel.articoli.value?.size).show(parentFragmentManager, "DialogScarico")
}
}
While the ScaricoDialog.kt looks like this:
class ScaricoDialog(private val listSize: Int? = 0): DialogFragment() {
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
dialog!!.window?.setBackgroundDrawableResource(R.drawable.round_corner)
return inflater.inflate(R.layout.scarica_dialog, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
}
override fun onStart() {
super.onStart()
val width = (resources.displayMetrics.widthPixels * 0.85).toInt()
dialog!!.window?.setLayout(width, ViewGroup.LayoutParams.WRAP_CONTENT)
}
}
When i lock the device so the app goes onPause and i unblock it i find lot of that dialogs open even when i opened just one...
i solved it by setting this onPause in my DialogFragment:
override fun onPause() {
super.onPause()
dialog?.dismiss()
}
But is it normal? is my solution right to do so?
set Dialog show method in onActivityCreate inside fragment method like this and stop dismiss in on pause
override fun onCreate(savedInstanceState: Bundle?)
{
super.onCreate(savedInstanceState)
val width = (resources.displayMetrics.widthPixels * 0.85).toInt()
dialog!!.window?.setLayout(width, ViewGroup.LayoutParams.WRAP_CONTENT)
ScaricoDialog(articoliViewModel.articoli.value?.size).show(parentFragmentManager, "DialogScarico")
}

Which lifecycle state called after BottomSheet close?

I am using BottomSheet Dialog from a fragment. I want to reload fragment data after bottomsheet close.
I am trying by adding reload code in onResume method. But it is not working.
Here My BottomSheet Dialog code.
class LoopResponseDialogueFragment : BottomSheetDialogFragment() {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?) =
inflater.inflate(R.layout.fragment_loop_response_dialog, container, false)!!
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
var progressDialog = ProgressDialog(context)
acceptButton.setOnClickListener {
priyoLoopService.sendLoopResponse(LoopActionRequest(user.id, "accept"))
.enqueue(object : Callback<GenericLoopResponse> {
override fun onFailure(call: Call<GenericLoopResponse>, t: Throwable) {
}
override fun onResponse(
call: Call<GenericLoopResponse>,
response: Response<GenericLoopResponse>
) {
dismiss()
}
})
}
}
You can create an interface in your LoopResponseDialogueFragment class and use it inside your fragment. Override onDismiss() and onCancel() function and call the interface function.
LoopResponseDialogueFragment Code:
class LoopResponseDialogueFragment : BottomSheetDialogFragment() {
private lateinit var onBottomSheetCloseListener:OnBottomSheetCloseListener
interface OnBottomSheetCloseListener{
fun onBottomSheetClose()
}
fun setOnBottomSheetCloseListener(listener:OnBottomSheetCloseListener) {
onBottomSheetCloseListener = listener
}
// rest of the code
override fun onDismiss(dialog: DialogInterface)
{
super.dismiss(dialog)
listener.onBottomSheetClose()
}
}
Now in your fragment before calling show method, call setOnBottomSheetCloseListener()
In your Fragment:
dialog.setOnBottomSheetCloseListener(object: OnBottomSheetCloseListener{
override fun onBottomSheetClose()
{
//refresh your data here
}
})
You can add code to refresh the fragment data inside onBottomSheetClose method.

Timer is not cancelling

None of the other instances of this question are solving my problem. I have a Fragment that appears at the end of a transaction sequence. It is meant to close the app when a Timer contained within it completes:
var terminalTimer: Timer? = null
class TerminalFragment : Fragment() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_terminal, container, false)
}
override fun onStart() {
super.onStart()
initUi()
startCountDown()
}
override fun onStop() {
super.onStop()
AppLog.i(TAG, "onStop()")
stopCountDown()
}
private fun startCountDown() {
if (terminalTimer == null) {
terminalTimer = Timer()
terminalTimer!!.schedule(object : TimerTask() {
override fun run() {
AppLog.i(TAG, "Terminal countdown finished")
{
activity?.finish()
}
}, 5000)
}
}
private fun stopCountDown() {
AppLog.i(TAG, "stopCountDown()")
terminalTimer?.cancel()
terminalTimer?.purge()
terminalTimer = null
}
private fun returnToStart() {
AppLog.i(TAG, "returnToStart()")
(context as MainActivity).restartFlow() // calls popBackStackImmediate() for every fragment in the backstack, returning user to the beginning of their flow
}
companion object {
#JvmStatic
fun newInstance(terminalType: String, amountLoaded: Double) =
TerminalFragment().apply {
arguments = Bundle().apply {
}
}
}
}
stopCountDown() is being called whenever the fragment is navigated away from, but it somehow survives sometimes and closes the app from another Fragment. Using logs, I've also discovered that there appears to be 2 instances of this timer sometimes. How do I insure that this countdown is never active outside of this fragment and is cancelled/ destroyed in the Fragment's onStop()?

Categories

Resources