1

I have two tabs in my app with two RecyclerView that when I click on one item in the first tab it adds one item to the second tab (download link to download manager) but the user can't see the change until the fragment onDestroy and again onCreate (rotate, exit, going to the third tab) I think it happens because of the lifeCycle of fragment in viewPager that keeps the previous tab.

How can I force android to destroy fragment on tab change? I even tried setOnPageChangeListener with interface but it did not work for me, maybe because getItem only runs on fragment creation?

Jaap
  • 81,064
  • 34
  • 182
  • 193
behruz
  • 11
  • 2
  • There is NEVER and no need to destroy a fragment to view changes. Just update data source (your array) from fragment#1. Refresh recycler view on OnResume() call. – NightFury Sep 12 '16 at 20:17
  • sorry i am new in this and maybe i didn't get it right but i think android hold the previous tab for when if the user came back it have it already . and just when the user goes to the third tab it destroys tab i try it with log and onResume not running – behruz Sep 12 '16 at 20:29
  • See answer of Aenadon. This is what you should do. – NightFury Sep 12 '16 at 20:32
  • In case the current answer doesn't work for some reason, it is possible to do what you're trying to do using broadcast reveivers and case/switch logic on your fragments but it is NASTY! – Nerdy Bunz Sep 13 '16 at 03:57

2 Answers2

2

Instead of populating your list in onCreate(), you should do it in onResume(). Following the Android Activity lifecycle, as soon as your activity/fragment is inactive, onPause() is called - and as soon as you go back to it, onResume() is called. So if you are in another tab and switch to your second tab, onResume() will be called and you can create the list from there - no need to destroy the vieiw.

You can read more on the activity lifecycle here: https://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle

techfly
  • 1,826
  • 3
  • 25
  • 31
  • ok: i'm going and try but what about this:Fragment’s onResume() is not called when fragment is actually resumed and showed to the user on screen. So in standard ViewPager it is impossible to update fragment when it is displayed, since there is no lifecycle method that is called when fragment is displayed.https://looksok.wordpress.com/2013/11/02/viewpager-with-detailed-fragment-lifecycle-onresumefragment-including-source-code/ – behruz Sep 12 '16 at 20:46
  • If that's the case, maybe this will help you: http://stackoverflow.com/a/23647673/3673616 – techfly Sep 13 '16 at 05:41
0
override fun onPause() {
    list.clear()
    super.onPause()
}

add this code in your each fragment

JustSightseeing
  • 1,460
  • 3
  • 17
  • 37
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jan 07 '23 at 04:00