How can the position of a RecyclerView be retained when Activity has restarted? I've considered using something like savedInstanceState but there doesn't seem to be a simple away of doing this without using dozens of lines of code.
Activity
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.md)
if (savedInstanceState == null) supportFragmentManager.beginTransaction()
.replace(R.id.master_container, MyFragment())
.commit()
}
}
Fragment
class MyFragment : androidx.fragment.app.Fragment() {
private lateinit var mRecyclerView: RecyclerView
private var myAdapter: AdapterMain? = null
private val myList = ArrayList<RVItem>()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setHasOptionsMenu(true)
}
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val view = inflater.inflate(R.layout.activity_main, container, false)
myRecyclerView = view.myRecyclerView
myList.add(RVItem("Item A"))
myList.add(RVItem("Item B"))
myList.add(RVItem("Item C"))
...
myList.add(RVItem("Item Y"))
myList.add(RVItem("Item Z"))
myAdapter = AdapterMain(activity!!, myList)
myRecyclerView.adapter = mAdapter
return view
}
override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
val mInflater = Objects.requireNonNull<androidx.fragment.app.FragmentActivity>(activity).menuInflater
mInflater.inflate(R.menu.main, menu)
super.onCreateOptionsMenu(menu, inflater)
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
return when (item.itemId) {
R.id.action_restart -> {
restartActivity()
true
}
else -> super.onOptionsItemSelected(item)
}
}
private fun restartActivity() {
startActivity(
Intent(view!!.context, MainActivity::class.java)
)
activity!!.overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out)
activity!!.finish()
}
}
RecyclerView restores scroll position natively by implementing view save state and requires no extra code on your part.
What you're doing incorrectly is "restarting" your activity. By calling start and finish you're destroying current activity and creating new instance. It will never receive savedInstanceState because it's not the same activity.
Instead you should use activity.recreate() to properly recreate the activity instance. It will receive savedInstanceState and implicitly restore view state including RecyclerView scroll.
Related
I am training with a simple app to show movies, I use an MVVM pattern and Flow.
Problem
This is my home, filterable through chips
I click on a movie , the details screen comes up then I go back to the home and this is the result:
Using logcat the home screen gets the list of movies to show but is not shown in the recyclerview (which uses diffUtil).
Below is the code for my fragment:
#AndroidEntryPoint
class Home2Fragment : Fragment() {
private val TAG = Home2Fragment::class.simpleName
private var _binding: FragmentHome2Binding? = null
private val binding: FragmentHome2Binding
get() = _binding!!
private val viewModel: HomeViewModel by viewModels()
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View {
// Inflate the layout for this fragment
_binding = FragmentHome2Binding.inflate(inflater, container, false)
return binding.root
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
binding.apply {
initChipGroupSpecificMovieList()
val adapter = MovieAdapter()
sectionRv.setHasFixedSize(true)
sectionRv.adapter = adapter
viewLifecycleOwner.lifecycleScope.launch {
viewLifecycleOwner.lifecycle.repeatOnLifecycle(Lifecycle.State.STARTED) {
viewModel.movieListBySpecification.collectLatest {
Log.d(TAG, "onViewCreated: received list")
adapter.addItems(it)
}
}
}
}
}
private fun FragmentHome2Binding.initChipGroupSpecificMovieList() {
val sortByMap = HomeViewModel.Companion.MovieListSpecification.values()
chipGroup.removeAllViews()
for (specification in sortByMap) {
val chip = Chip(context)
chip.isCheckable = true
chip.id = specification.ordinal
chip.text = getString(specification.nameResource)
chip.setOnCheckedChangeListener { _, isChecked ->
if (isChecked)
viewModel.setMovieListSpecification(specification)
}
chipGroup.addView(chip)
}
chipGroup.check(sortByMap.lastIndex - sortByMap.size + 1)//check first element
}
override fun onDestroyView() {
super.onDestroyView()
_binding = null
}
}
it seems at the line of code where I try to insert the list of movies in the adapter this doesn't add them because maybe via diffUtil it finds that it is the previous list and so it doesn't load it. However it doesn't show the previous one either, possible solutions?
as java code you can use this
#Override
public void onResume() {
super.onResume();
if(it.size() > 0) {
adapter.addItems(it)
}
}
I have an app with 3 main destinations which can be accessed by a bottom nav view. Each destination has its own navigation graph.
The problem is that when I minimize and reopen my app, the navigation components reset to the default destination. Why does this happen?
My main activity: (Irrelevant code omitted)
class MainActivity : AppCompatActivity() {
// List of base host containers
val fragments = listOf(
HomeHostFragment(),
CoursesHostFragment(),
SearchHostFragment()
)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val viewPager: ViewPager = findViewById(R.id.main_activity_pager)
viewPager.adapter = ViewPagerAdapter()
}
inner class ViewPagerAdapter : FragmentPagerAdapter(supportFragmentManager) {
override fun getItem(position: Int): Fragment =
fragments[position]
override fun getCount(): Int =
fragments.size
}
}
HomeHostFragment.kt:
class HomeHostFragment : Fragment() {
private lateinit var navController: NavController
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? =
inflater.inflate(R.layout.fragment_home_host, container, false)
override fun onStart() {
super.onStart()
navController = requireActivity().findNavController(R.id.nav_host_home)
navController.setGraph(R.navigation.nav_graph_home)
NavigationUI.setupWithNavController(toolbar_home, navController)
}
fun onBackPressed(): Boolean {
return navController.navigateUp()
}
}
Whenever onStart() is called, NavigationUI.setupWithNavController() is called again which resets the navigation. Move this call to onViewCreated() so that the navigation setup is not done every time the fragment pauses and restarts.
I have a Fragment that contains a ViewPager. When I navigate to this fragment I see the first page as expected. However, if I hit back, and then navigate to that fragment again, I am then presented with a blank white screen...
What I'm noticing in the Layout Inspector is that when I navigate to the ViewPager Fragment the second time, the ViewPager is present, but none of the page fragments are in the hierarchy.
Here is how I set up my viewPager in my ViewPagerFragment:
/**
* Variables
*/
var pageOneFragment = ForgotPasswordInitiateFragment()
var pageTwoFragment = ForgotPasswordSubmitCodeFragment()
var pageThreeFragment = ForgotPasswordSubmitPasswordFragment()
lateinit var mViewPager: ForgotPasswordViewPager
lateinit var mPagerAdapter: ForgotPasswordPagerAdapter
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
}
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.forgot_password_pager_fragment, container, false)
}
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
forgotPasswordViewModel = ViewModelProviders.of(this, viewModelFactory)
.get(ForgotPasswordViewModel::class.java)
pageOneFragment.viewPager = this
pageTwoFragment.viewPager = this
pageThreeFragment.viewPager = this
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
activity?.let {
mPagerAdapter = ForgotPasswordPagerAdapter(it.supportFragmentManager)
}
mViewPager = viewPager
mViewPager.adapter = mPagerAdapter
mViewPager.setListeners()
}
override fun onResume() {
super.onResume()
}
inner class ForgotPasswordPagerAdapter(fm: FragmentManager): FragmentPagerAdapter(fm) {
override fun getItem(position: Int): Fragment {
when (position) {
0 -> return pageOneFragment
1 -> return pageTwoFragment
2 -> return pageThreeFragment
else -> throw IllegalStateException("Pager position $position is out of bounds.")
}
}
override fun getCount(): Int {
return 3
}
}
I attempted to set all my mViewPager configurations into the onResume function but the same thing happened.
When does the viewPager actually populate its pages? Because what it seems like to me is they are populated the first time, but not the second....
EDIT:
I debugged the Activity's fragments on the back pressed and noticed that the pages were in that fragment stack, but not the actual viewPager fragment.
You're using
activity?.let {
mPagerAdapter = ForgotPasswordPagerAdapter(it.supportFragmentManager)
}
I.e., using the activity's FragmentManager. That's always the wrong thing to do and the source of your issue. Instead, you need to use the Fragment's childFragmentManager:
mPagerAdapter = ForgotPasswordPagerAdapter(childFragmentManager)
I have an activity that hosts both fragments called SourceFragment and DestinationFragment. The SourceFragment contains a RecyclerView and the DestinationFragment contains a ViewPager. I've been using the fragment manager to swap back and forth between the Source and Destination fragment.
Issue:
The return transition works normally as long as I don't swipe to a different view on the ViewPager. To resolve this I overrode the onMapSharedElements in the DestinationFragment and the SourceFragment to make sure the views match each other when the view pager is swiped.
For some reason swiping and returning to the SourceFragment doesn't work and no transition animation happens. I even debugged the onMapSharedElements functions to make sure that the views mapped correctly.
This is what I'm trying to implement.
My implementation
I've provided the code below for the fragments in question but here is the repo to my implementation. Been itching my head for a week now trying to figure this out hopefully I can get some insight as to why this is happening.
SourceFragment:
class SourceFragment : Fragment() {
private lateinit var sharedViewModel: SharedViewModel
private lateinit var itemRecyclerView: RecyclerView
private lateinit var adapter: SourceAdapter
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setExitSharedElementCallback(object : SharedElementCallback() {
override fun onMapSharedElements(names: MutableList<String>?, sharedElements: MutableMap<String, View>?) {
val view = itemRecyclerView.findViewHolderForAdapterPosition(sharedViewModel.currentPos)
?.itemView?.findViewById<TextView>(R.id.source_text)
val item = sharedViewModel.itemList[sharedViewModel.currentPos]
if (view == null) return
names?.clear()
sharedElements?.clear()
names?.add(item)
sharedElements?.put(item, view)
}
})
sharedViewModel = ViewModelProviders.of(requireActivity()).get(SharedViewModel::class.java)
adapter = SourceAdapter(object : onClickItem {
override fun onClick(position: Int, view: View) {
// Save the position of the item that was clicked.
sharedViewModel.currentPos = position
// Setup shared element transition
val transitionName = ViewCompat.getTransitionName(view) ?: ""
// Start fragment transaction along with shared element transition.
fragmentManager?.apply {
beginTransaction()
.setReorderingAllowed(true)
.addSharedElement(view, transitionName)
.replace(
R.id.fragment_container,
DestinationFragment(),
DestinationFragment::class.java.simpleName
)
.addToBackStack(null)
.commit()
}
}
})
val list = sharedViewModel.itemList
adapter.submitList(list)
}
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val view = inflater.inflate(R.layout.source_layout, container, false)
postponeEnterTransition()
return view
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
// Setup recycler view here
itemRecyclerView = item_recycler_view
itemRecyclerView.layoutManager = GridLayoutManager(requireContext(), 2)
itemRecyclerView.adapter = adapter
// Start enter transition on pre draw.
itemRecyclerView.viewTreeObserver.addOnPreDrawListener(object : ViewTreeObserver.OnPreDrawListener {
override fun onPreDraw(): Boolean {
itemRecyclerView.viewTreeObserver.removeOnPreDrawListener(this)
startPostponedEnterTransition()
return true
}
})
// Scroll to the position of the item that was selected in the Destination Fragment.
itemRecyclerView.addOnLayoutChangeListener { p0, p1, p2, p3, p4, p5, p6, p7, p8 ->
val layoutMan = itemRecyclerView.layoutManager
val viewAtPos = layoutMan?.findViewByPosition(sharedViewModel.currentPos)
if (viewAtPos == null ||
layoutMan.isViewPartiallyVisible(viewAtPos, false, true)
) {
itemRecyclerView.post {
layoutMan?.scrollToPosition(sharedViewModel.currentPos)
}
}
}
}
DestinationFragment
class DestinationFragment : Fragment(), MainActivityListener {
lateinit var sharedViewModel: SharedViewModel
lateinit var destPager : ViewPager
lateinit var itemAdapter : ItemPagerAdapter
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
postponeEnterTransition()
setEnterSharedElementCallback(object : androidx.core.app.SharedElementCallback(){
override fun onMapSharedElements(names: MutableList<String>?, sharedElements: MutableMap<String, View>?) {
val item = sharedViewModel.itemList[destPager.currentItem]
val itemView = (itemAdapter.instantiateItem(destPager, destPager.currentItem) as Fragment).view?.findViewById<TextView>(R.id.item_fragment_textview) as View
names?.clear()
names?.add(item)
sharedElements?.clear()
sharedElements?.put(item, itemView)
}
})
sharedElementEnterTransition = TransitionInflater.from(requireContext()).inflateTransition(android.R.transition.move)
sharedViewModel = ViewModelProviders.of(requireActivity()).get( SharedViewModel::class.java)
}
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.destination_layout, container ,false)
}
override fun onBackPressed(): Boolean {
sharedViewModel.currentPos = destPager.currentItem
return false
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
destPager = destination_pager
itemAdapter = ItemPagerAdapter(sharedViewModel.itemList, childFragmentManager)
destination_pager.adapter = itemAdapter
destination_pager.currentItem = sharedViewModel.currentPos
}
I have the following activity with two integers
class ComplexActivity : AppCompatActivity() {
var clubs : Int = 0
var diamonds : Int = 0
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_complex)
val fragment = ClubsFragment()
val transaction = supportFragmentManager.beginTransaction()
transaction.replace(R.id.main_frame, fragment)
transaction.commit()
}
}
I want to change the value of the integer clubs from the fragment ClubsFragment when isScored is true
class ClubsFragment : Fragment(), SeekBar.OnSeekBarChangeListener{
private var isScored = false
override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
// Inflate the layout for this fragment
val v = inflater!!.inflate(R.layout.fragment_clubs, container, false)
v.image_clubs.setOnClickListener {
if(isScored){
activity.clubs = 4
}
}
}
}
I tried to use activity.clubs but It's not working. How can I access the activity constants from a fragment.
You would create an interface, let's say FragmentListener for your Activity that contains a function like fun updateClubs(count: Int). Your Activity should implement this interface.
Then, in your Fragment, add a fragmentListener property and override onAttach(context: Context):
private var fragmentListener: FragmentListener? = null
override fun onAttach(context: Context) {
this.listener = context as? FragmentListener
}
Then, in your OnClickListener, you can simply call fragmentListener?.updateClubs(4).