Помогите разобраться почему происходит дублирование данных при нажатии кнопки "назад" телефона, тоесть имеем список из 12 полей, при переходе с главного фрагмента в дочерний и затем обратно список уже имеет 24 поле, повторяем те же действия и список уже 36 полей и так можно пока наверное память не закончится.
HeroesFragment
class HeroesFragment : Fragment() {
private var mBinding: FragmentHeroesBinding? = null
private lateinit var mAllInfoApex: HeroesViewModel
private lateinit var mHeroesAdapter: HeroesAdapter
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setHasOptionsMenu(true)
}
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View {
mBinding = FragmentHeroesBinding.inflate(inflater, container, false)
return mBinding!!.root
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
mAllInfoApex =
ViewModelProvider(this).get(HeroesViewModel::class.java)
mAllInfoApex.getAllInfoApexFromAPI(Constants.API_PLAYER_VALUE)
mBinding?.rvHeroList?.layoutManager = GridLayoutManager(requireActivity(), 2)
mHeroesAdapter = HeroesAdapter(this@HeroesFragment)
mBinding?.rvHeroList?.adapter = mHeroesAdapter
InfoApexViewModelObserver()
}
fun heroesDetails(legends: Heroes){
findNavController().navigate(HeroesFragmentDirections.actionAllHeroesToHeroesDetails(legends))
}
override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
inflater.inflate(R.menu.menu_profile, menu)
super.onCreateOptionsMenu(menu, inflater)
}
override fun onDestroy() {
super.onDestroy()
mBinding = null
}
}
DetailsHeroesFragment
class DetailsHeroesFragment : Fragment() {
private var mBinding: FragmentDetailsHeroesBinding? = null
//private val hero : ArrayList<AllHeroes.Data>? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
mBinding = FragmentDetailsHeroesBinding.inflate(inflater, container, false)
// Inflate the layout for this fragment
return mBinding!!.root
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
val args: DetailsHeroesFragmentArgs by navArgs()
Glide.with(this)
.load(args.heroesDetails.data.ImgAssets.banner)
.into(mBinding!!.ivHeroesDetails)
mBinding!!.tvHeroesName.text = args.heroesDetails.name
args.heroesDetails.data.let { values ->
when {
values.data.isNullOrEmpty() -> {
mBinding!!.tvDataName.text = "NOT DATA"
mBinding!!.tvDataValue.visibility = View.INVISIBLE
mBinding!!.tvDataName2.visibility = View.INVISIBLE
mBinding!!.tvDataValue2.visibility = View.INVISIBLE
mBinding!!.tvDataName3.visibility = View.INVISIBLE
mBinding!!.tvDataValue3.visibility = View.INVISIBLE
mBinding!!.tvDataName4.visibility = View.INVISIBLE
mBinding!!.tvDataValue4.visibility = View.INVISIBLE
Log.i("Heroes test", "test")
}
else -> {
mBinding!!.tvDataName.visibility = View.INVISIBLE
mBinding!!.tvDataValue.visibility = View.INVISIBLE
mBinding!!.tvDataName2.visibility = View.INVISIBLE
mBinding!!.tvDataValue2.visibility = View.INVISIBLE
mBinding!!.tvDataName3.visibility = View.INVISIBLE
mBinding!!.tvDataValue3.visibility = View.INVISIBLE
mBinding!!.tvDataName4.visibility = View.INVISIBLE
mBinding!!.tvDataValue4.visibility = View.INVISIBLE
}
}
}
}
override fun onDestroy() {
super.onDestroy()
mBinding = null
}
}
MainActivity
class MainActivity : AppCompatActivity() {
private lateinit var mBinding: ActivityMainBinding
private lateinit var mNavController: NavController
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
mBinding = ActivityMainBinding.inflate(layoutInflater)
setContentView(mBinding.root)
mNavController = findNavController(R.id.nav_host_fragment_activity_main)
// Passing each menu ID as a set of Ids because each
// menu should be considered as top level destinations.
val appBarConfiguration = AppBarConfiguration(
setOf(
R.id.navigation_all_heroes,
R.id.navigation_match_history,
R.id.navigation_funny_videos
)
)
setupActionBarWithNavController(mNavController, appBarConfiguration)
mBinding.navView.setupWithNavController(mNavController)
}
override fun onSupportNavigateUp(): Boolean {
return NavigationUI.navigateUp(mNavController, null)
}
}
Тоесть получается когда возвращаюсь из DetailsHeroesFragment обратно к HeroesFragment то к существующим данным добавляется список еще точно таких же данных, как это пофиксить ? Либо где я что-то упустил ? В логах вижу как данные создаются 2 раза затем при переходе еще +2 и т.д
ViewModel
class HeroesViewModel: ViewModel() {
private val allInfoApexAPIService = AllInfoApexAPIService()
private val compositeDisposable = CompositeDisposable()
val loadAllInfoApex = MutableLiveData<Boolean>()
val allInfoApexResponse = MutableLiveData<AllHeroes.Heroes>()
val allInfoApexLoadingError = MutableLiveData<Boolean>()
fun getAllInfoApexFromAPI(username: String){
loadAllInfoApex.value = true
compositeDisposable.add(
allInfoApexAPIService.getAllInfoApexTracker(player = username)
.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread())
.subscribeWith(object: DisposableSingleObserver<AllHeroes.Heroes>(){
override fun onSuccess(value: AllHeroes.Heroes) {
loadAllInfoApex.value = false
allInfoApexResponse.value = value
allInfoApexLoadingError.value = false
}
override fun onError(e: Throwable) {
loadAllInfoApex.value = false
allInfoApexLoadingError.value = true
e.printStackTrace()
}
})
)
}
}