RecyclerView scrolls to the top when I open another activity - android

I have a RecyclerView in my Fragment. When I open another activity, then hit back and go to the initial activity/fragment, the RecyclerView is now scrolled to the top of the list.
I'm initiating my RecyclerView inside of onCreate method, and when I navigate to another activity and come back, only onStop (and the once after obviously) get called.
I even tried saving the state of the LinearLayout manager
override fun onStop() {
super.onStop()
rcerecylerViewState = myRecyclerView.layoutManager?.onSaveInstanceState()
}
override fun onStart() {
super.onStart()
if(rcerecylerViewState != null){
myRecyclerView.layoutManager?.onRestoreInstanceState(newsFeedViewModel.rcerecylerViewState)
}
}
I dont want to clutter this post by including my whole layout and fragment file. Please let me know which part of the code I should include to further clarify.

No need to do anything in onStart.
Move your onStart code into your onCreate.
Pseudocode looks like this.
if (recyclerview != null)
// restoredata
else
// initial set up.

Related

PagingDataAdapter.refresh() not working after fragment navigation

I am using PagingDataAdapter in one fragment to show user activity.
in fragment class level,
private var activityAdapter: ActivityFeedAdapter? = null
in onCreate() I am initializing before use as,
activityAdapter = initAdapter()
also in onCreate(),
this.lifecycleScope.launchWhenResumed {
viewLifecycleOwner.repeatOnLifecycle(Lifecycle.State.STARTED) {
viewModel.getActivityFeed().observe(viewLifecycleOwner) {
it?.let {
activityAdapter?.submitData(lifecycle, it)
}
}
}
}
and after onStart(), I am setting a click Listener on a view to refresh pagingdata from the UI as,
binding?.refresh?.setOnClickListener {
activityAdapter?.refresh()
}
Everything works fine when I use it for the first load. But after I navigate to some fragment and get back to the same screen, clicking on refresh only handles click event but does not refresh the adapter.
BTW, I have initialized the adapter in onCreate() because I need the adapter to maintain loaded data across screen transitions. Anyone help me...
I got the bug... :))
In onCreate() I was setting observer with lifecycleOwner as viewLifecycleOwner.
But viewLifecycleOwner is only active from onCreateView() till onDestroyView(). So after navigation to a different fragment and getting back from there, the new observer was not set. The old observer is canceled due to lifecycleOwner is destroyed. So I could refresh more data in PagingDataAdapter.
When setting the observer please rethink which lifecycleOwner is to be used. Hope this might help someone. :)

Fragment Transaction Issue in Android

I have a Fragment A in My app.
From the Fragment A, I am moving to Fragment B.
Note that I am adding the Fragment B on Fragment A (not replacing the Fragment.)
Now, When I coming back to Fragment A from Fragment B, I have to call a method of Fragment A and that's Why I am calling that method in life cycle method of Fragment A : in onResume(), onViewCreated()
But What I noticed by putting log in both the method that these methods are not calling means I am not getting Log in logcat and the method which I have called in these two methods is not executing.
What might be the issue? Thanks.
I am doing as below for your reference:
override fun onResume() {
super.onResume()
Log.e("onResume 1","onResume 1")
(activity as HomeActivity).updateToolbar(false)
setLanguageSpecificData()
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
Log.e("onViewCreated","onViewCreated")
setLanguageSpecificData()
}
Problem:
The way you mentioned in your question that:
I am adding the Fragment B on Fragment A (not replacing the Fragment.)
So, There is a difference between what lifecycle methods gets called based on replace & add.
If you take a look at this answer :https://stackoverflow.com/a/21684520/9715339
In terms of fragment's life cycle events onPause, onResume,
onCreateView and other life cycle events will be invoked in case of
replace but they wont be invoked in case of add
Also, It's tied to activity lifecycle as well.
Solution:
Looking at your code you want to update something when current visible fragment changes or in other words backstack changes.
For that you can do this in your activity:
supportFragmentManager.addOnBackStackChangedListener {
//when backstack changes get the current fragment & do something with that info
val frg = supportFragmentManager.findFragmentById(R.id.root_container)
if (frg is AFragment){
// do something like updateToolbar(false)
} else if (frg is BFragment){
//something else
}
Log.d("FrgTest-",frg.toString())
}
In fragment you can do requireActivity().supportFragmentManager & rest will be fine.
Read:
This is just an example for backstack change. If you want to communicate between fragments you can use other ways like setFragmentResultListener as well.
This may help: https://developer.android.com/guide/fragments/communicate#kotlin

Managing Fragments on Android Foldable

I am creating a new Android Project and soon android foldable devices will be launched. I have an Activity which has fragment called first fragment.
First Fragment has a button called first button which open second fragment which has a button called second and on click of second, third fragment opens.
Suppose user is in third fragment and user decides to unfold his device, will the user go back to fragment one or will he stay in fragment three. As far as I have understood from the Developer Summit, the activity will be destroyed and recreated when user unfolds his device so technically user goes backs to first fragment leading to poor user experience.
So my question is should I consider even using fragments?, If yes how to manage state so that user goes to the same fragment he was when he folds or unfolds his device.
Following is my code if I am changing fragments
private fun displayView(fragment: Fragment?, title: String) {
if (fragment != null) {
supportFragmentManager.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE)
supportFragmentManager.beginTransaction()
.replace(R.id.framelayout_activity_main, fragment, title).commit()
}
}
In onCreate(), you only want to execute a FragmentTransaction if this activity is being
newly created, instead of being recreated from a configuration change. Or, more accurately,
you only want to execute a FragmentTransaction if you do not already have fragments in the state that you want them.
So, a typical approach is to see if you already have a fragment in your container:
override fun onCreate(state: Bundle) {
super.onCreate(state)
if (supportFragmentManager.findFragmentById(R.id.framelayout_activity_main) == null) {
// do something to show your fragment
}
// other good stuff goes here
}
On the first onCreate() invocation, findFragmentById() will return null, so you execute your code to display your first fragment. On a subsequent onCreate() invocation after a configuration change, Android will have already set up your fragment(s) for you by the time onCreate() is called. So, in that case, findFragmentById() will return something other than null, so you know that you already have a fragment in your container and do not need to do anything more.

ViewPager Current Fragment Visibility

What I Have
I have a ViewPager with 5 fragments. I want to animate some TextViews inside the fragments whenever they become visible to the user.
I can't use onResume() as the fragments to the left and right are already created. I can't use setUserVisibilityHint() as it is called before onCreateView() so the views are not ready yet.
So what should be the way to animate the views whenever a particular fragment becomes visible to the user?
I'm not sure, but if you say that setUserVisibilityHint calls before onCreateView, than check view on null here (make reference on view - field), and if it not null - animate it. Also animate it always in onCreateView.
(1) I can't use onResume() as the fragments to the left and right are already created.
(2) I can't use setUserVisibilityHint() as it is called before onCreateView() so the views are not ready yet.
So what should be the way to animate the views whenever a particular fragment becomes visible to the user?
You're right on (1) and (2). However, setUserVisibilityHint() gets called Once Again with a True value after the Fragment comes to Front on Display. But on First Run the Fragment to be shown gets its setUserVisibilityHint() called before onCreateView().
SOL: You should use the above said behaviour of setUserVisibilityHint() along with onResume() to animate the views whenever a particular fragment becomes visible to the user.
Scenario 1: On First Run: Displayed Fragment's setUserVisibilityHint(boolean isVisibleToUser) gets called with
True param value. But as the Fragment's State is not Resumed we postpone and let the onResume() handle animation.
Scenario 2: For Other Fragments that are already in Resume State, setUserVisibilityHint(boolean isVisibleToUser) will get called with
True param it they come on to Display. Here you check for the
Fragment Animated or not and Do animation.
CODE
a) Declare two Global Boolean Fields: isAnimated and isOnDisplay
a.1) Set isAnimated boolean to True;
b) Override setUserVisibilityHint(boolean isVisibleToUser):
Here you set isOnDisplay boolean to isVisibleToUser and check is the Fragment Not Already Animated and is in Resumed State and is Visible to User.
{ if(!isAnimated && isResumed() && isVisibleToUser) // DO Animation }
c) Override onResume()
Check if the Fragment Not Already Animated and is Visible to User.
{ if(!isAnimated && isVisibleToUser) // DO Animation }
I know this answer might be a bit late, but I hope it can help others in a similar situation.
You could use FragmentViewPager library (I am the author), which deals with the issue you are facing for you. Its features are:
allows its Fragment pages to get notified when they are actually
visible/invisible to the user
supports multiple levels of FragmentViewPagers (nesting)
provides methods to control its paging
A basic usage would be:
Attach FragmentViewPager programmatically or via XML to an Activity
or Fragment, as you would with native ViewPager
Set FragmentViewPager's adapter. Your adapter should inherit
com.sbrukhanda.fragmentviewpager.adapters.FragmentPagerAdapter or
com.sbrukhanda.fragmentviewpager.adapters.FragmentStatePagerAdapter
Override onResumeFragments() of the hosting Activity and call
FragmentViewPager.notifyPagerVisible():
private FragmentViewPager mFragmentsPager;
#Override
public void onResumeFragments() {
super.onResumeFragments();
mFragmentsPager.notifyPagerVisible();
...
}
or onResume() of the hosting Fragment and call
FragmentViewPager.notifyPagerVisible():
private FragmentViewPager mFragmentsPager;
#Override
public void onResume() {
super.onResume();
mFragmentsPager.notifyPagerVisible();
...
}
Override onPause() of the hosting Activity or Fragment and call
FragmentViewPager.notifyPagerInvisible():
private FragmentViewPager mFragmentsPager;
#Override
public void onPause() {
super.onPause();
mFragmentsPager.notifyPagerInvisible();
...
}
Implement FragmentVisibilityListener on all Fragment pages that you
wish to receive callbacks for their visibility state
You are ready to go!
If you wish to see a more complete sample code, then check project's sample project.
If you want to do it in individual fragments, then you can use isVisible()
for each fragment in your fragment transition and create a listener. Whenever a fragment will become visible , listener will be invoked and each fragment will implement that listener and do your intended task in the overridden method.

Detecting the visible fragment in android

I want to implement the onbackpressed() in android and my code is as follows
public void backpressed(){
NDListeningFragment fragment1=(NDListeningFragment)getSupportFragmentManager().findFragmentByTag(ConnectedDevicesFragment.TAG);
if(fragment1!=null && fragment1.isVisible())
{
super.onBackPressed();
}
else
{
fragment1=(NDListeningFragment)SimpleFragmentFactory.createFragment(ConnectedDevicesFragment.TAG);
getSupportFragmentManager().beginTransaction().replace(R.id.content,fragment1).commit();
fragment1.setUserVisibleHint(true);
}
}
The above code checks if the visible fragment is ConnectedDevicesFragment. If yes then super() is called and if not then I create ConnectedDevicesFragment and replace it in the framelayout.
But I am not able to implement in this way. When I press back button it reloads the Connected DevicesFragment again and again.
can you help with some workaround.
Cheers!
You creates fragment1 object every time in the onBackPressed function it means it will not null and it is on invisible state. You need to add NDListeningFragment in backstack when you open NDListeningFragment first and check Is the fragment available in back stack. If yes then call super.onBackpressed.

Categories

Resources