1

I have Fragment A which loads some data when it opens. It has one button when we click. It navigate to Fragment B. That's not the issue, the problem is when I press back Button in Fragment B it comes to Fragment A and reload that data in Fragment A. My problem is I don't want to reload that data in Fragment A When back press From Fragment B.

Thanks in advance

8
  • have one flag in companion object and check its value before reloading. Commented Jun 18, 2021 at 16:12
  • Can you eloborate or some code for that. Thanks Commented Jun 18, 2021 at 16:22
  • It shouldn't reload the data? Are you replacing the fragment A with B and adding it to backstack? Commented Jun 18, 2021 at 16:24
  • I am replacing the fragment not backstack it. Commented Jun 18, 2021 at 16:28
  • <action android:id="@+id/action_homeFragment_to_showPurchaseFragment" app:destination="@id/showPurchaseFragment" app:enterAnim="@anim/fade_in_animation" app:exitAnim="@anim/fade_out_animation" app:popEnterAnim="@anim/fade_in_animation" app:popExitAnim="@anim/fade_out_animation" /> Commented Jun 18, 2021 at 16:31

2 Answers 2

1

You should use viewModel and put your all data inside viewModel, In case of back press fragment will reload but your data will not refresh it will same as previous was on view.

Class TestViewModel:ViewModel(){

 init{
   apiReqest()
 }

fun apiReqest(){
 // writer here your api request code and observe it on view using observer
} 

}
Sign up to request clarification or add additional context in comments.

Comments

0

I would suggest Dharmender's approach but if you are not using viewmodel then you can try this:

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        val tv1 = requireActivity().findViewById<TextView>(R.id.tv1)
        tv1.setOnClickListener {
            if(!isUpdated) {
                isUpdated = true
                requireActivity().supportFragmentManager.beginTransaction()
                    .replace((requireView().parent as ViewGroup).id, F2.newInstance())
                    .addToBackStack("f2").commit()
            }else{
                Toast.makeText(requireActivity(),"Already updated!", Toast.LENGTH_LONG).show()
            }
        }
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        // Inflate the layout for this fragment
        Log.e(TAG,"On create view")
        return inflater.inflate(R.layout.fragment_f1, container, false)
    }

    companion object {
        @JvmStatic
        fun newInstance() =
            F1()
        val TAG = F1::class.java.simpleName
        var isUpdated = false
    }

Here I am using one flag isUpdated.

1 Comment

Thanks man, for this solution it works fine

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.