Home > Mobile >  How to disable recreate fragment when rotate screen in Android
How to disable recreate fragment when rotate screen in Android

Time:03-12

In my application I used NavigationComponent for show Fragments and I used ViewModel !
But rotate the screen recreate fragment and remove all of position, recall again api and more...

My Fragment codes:

    @AndroidEntryPoint
class HomeFragment : Fragment() {
    //Binding
    private lateinit var binding: FragmentHomeBinding

    @Inject
    lateinit var genreAdapter: GenreAdapter

    @Inject
    lateinit var topMoviesAdapter: TopMoviesAdapter

    private val viewModel: HomeViewModel by viewModels()

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
        binding = FragmentHomeBinding.inflate(inflater, container, false)
        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        //InitViews
        binding.apply {
            //Genres
            viewModel.genresList.observe(viewLifecycleOwner) {
                genreAdapter.differ.submitList(it)
                genresRecycler.initRecycler(
                    LinearLayoutManager(requireContext(), LinearLayoutManager.HORIZONTAL, false),
                    genreAdapter
                )
            }
            viewModel.loadGenresList()
            //Top movies
            viewModel.topMoviesList.observe(viewLifecycleOwner) {
                topMoviesAdapter.differ.submitList(it.data)
                topMoviesRecycler.initRecycler(
                    LinearLayoutManager(requireContext(), LinearLayoutManager.HORIZONTAL, false),
                    topMoviesAdapter
                )
                topMoviesRecycler.smoothScrollToPosition(4)
            }
            viewModel.loadTopMoviesList(1)
            //Loading
            viewModel.loading.observe(viewLifecycleOwner) {
                if (it) {
                    homeLoading.showInvisible(true)
                } else {
                    homeLoading.showInvisible(false)
                }
            }
        }
    }
}

ViewModel codes:

    @HiltViewModel
class HomeViewModel @Inject constructor(private val repository: HomeRepository) : ViewModel() {

    val genresList = MutableLiveData<List<ResponseGenres.ResponseGenresItem>>()
    val topMoviesList = MutableLiveData<ResponseMovies>()
    val loading = MutableLiveData<Boolean>()

    fun loadGenresList() = viewModelScope.launch {
        val response = repository.genresList()
        if (response.isSuccessful) {
            genresList.postValue(response.body())
        }
    }

    fun loadTopMoviesList(id: Int) = viewModelScope.launch {
        loading.postValue(true)
        val response = repository.topMoviesList(id)
        if (response.isSuccessful) {
            topMoviesList.postValue(response.body())
        }
        loading.postValue(false)
    }
}

How can I fix it and disable recreate Fragment?

CodePudding user response:

I've a suggestion to avoid re-call of the API, but never though about disable recreating the fragment in fragments as this an Android Architecture.

Simply, override the onCreate() method in the Fragment, and move calling API logic to it.

viewModel.loadGenresList()

This will call it once, as onCreate() is only created once even after screen rotation

  • Related