How can I maintain UI state of my previous state in android? - android

Explanation:
I have two fragments and one activity. I have gone from one fragment to another fragment and my first fragment is visible from the activity.
In the first fragment onCreateView() method I called my API call. After call my data I set my data into the ListView. When I click on one of the rows of the ListView My second fragment becomes visible. Everything is working fine till now.
Problem
When I click on the back button of the device from the second fragment my previous fragment is visible but API call has been started.
How can I stop to refresh UI of the first fragment when I press back button?
For more detail See the Instagram Android Application.

If you are using addtobackstack() to maintain fragment state and call replace() method to call second fragment from first fragment in that case every time when you pressed back button from second fragment the onCreateView() method of first fragment will be called.
So the solution is call your API in onAttach() or onCreate() method.
Hope this will help you.

Related

What will be fragment behavior when came from background. Considering fragment is in stack

I have a Navigation panel activity. With 5 fragments (Will name it as Fragment1, Fragment2, ...) in menu sections.
Now by Default, activity will display Fragment1.
If user navigate to Fragmentxtz from Fragment1. We will add the fragment on top of Fragment1.
Now user goes to background by pressing home button and open the app from tasks.
Now i know Fragmentxtz onStart will be called. But i see that Fragment1 onStart is also called.
Is this expected behavior ?
As you can see on Android Developers, your fragment will be called at onViewCreated().
https://developer.android.com/guide/components/fragments#Creating
The View has to be updated in case you changed the system language or something like that.
Providing a bit more context to what might be happening here.
If both of your fragments are added e.g. via FragmentTransaction.add() both of them will have onCreateView() called when the layout is restored for the user as Robert pointed out. From the system's point of view all of those fragments are relevant to the user and would be shown simultaneously.
If on the other hand you add fragments via FragmentTransaction.replace() only the topmost fragment on the back stack will receive the onCreateView() call. This can also be achieved by doing an add and remove of the old fragment. If you make this transaction reversible by the back stack after pressing the back button your previous fragment would receive the appropriate lifecycle callbacks.

What methods fire when coming back from a fragment where using Navigation Components?

I have a single activity named MainActivity which holds the FirstFragment. Using Navigation Components I already set that the single fragment that can be opened from FirstFragment is SecondFragment. I have also enabled back button to be able to navigate back from SecondFragment to the FirstFragment. The problem is when I get back to the FirstFragment, onStart() method is not triggered. What methods fire in this case? How to know when I get back from SecondFragment to FirstFragment?
onCreateView() method of the first fragment is called whenever you press the back button from the second fragment if you are doing this right. The layout of this fragment is inflated in onCreateView() method. So that you will see first fragment screen.

Fragments onOptionsItemSelected

How can i make the fragments on the backstack not to trigger the onOptionsItemSelected(). Because every time i create a new fragment it always trigger the onOptionsItemSelected() if i select an item in menu. Also this fragment will be reused throughout the activity so "return true" on onOptionsItemSelected() is not an option because the fragment that was created still the same with the backstack fragment.
It appears that onOptionsItemSelected will always be called in every new fragment that is added in activity Menu documentation , so to solve my problem is that I added a validation which appears this one
<CurrentFragment>this.equals(fragmentManager.findFragmentById(R.id.frame_container) to validate if the fragment displayed/ attached in activity is the fragment that trigger the onOptionsItemSelected.
Hope this will help to anyone who will have this kind of behaviour on fragments.

I am not able to find callback in first two fragments of viewpager in activity

Scenario: I have three fragments in ViewPager in activity.
Fragment One
FragmentTwo
FragmentThree
I noticed that switching from fragment one to three onPause() method is called
but when I switched from fragment one to two onPause is not called
Problem: I want to show some data in FragmentOne's TextView with a webservice on button click.I clicked that button and data is shown But I noticed when I switch from one to two and then come back to fragment one data is shown. I want to hide that data when I come back to one. Is there any callback . ?
You need to understand Viewpager first. viewpager creates fragment in advance before and keeps so that there is smooth flow when you switch, like if you are on Fragment 2. viewpager creates 1 and 3 and keeps.
and when you are on Fragment 3 it retains Fragment 1.
Anyways you can increase the limit of viewpager with this
viewPager.setOffscreenPageLimit(int num)
you can understand all the lifecylce of fragment in Viewpager by putting Logs in onPause and onResume in viewpager fragments.
Anyways coming back to your prob. you need to have custom interface like this . answer helped me too
Its happening because Fragment One is in backstack. Dont keep FragmentOne in backstack while switching to FragmentTwo and on Back press of FrgamentTwo call switchContent for FragmentOne again .

How to scroll to the top of a ListFragment

My situation: I have two ListFragments (call them A and B) managed by one Activity which keeps persistent references to both of these Fragments. When I click a button in Fragment A, I replace that with Fragment B. The problem starts when I do the following flow.
A -> B -> (scroll) -> (back button) -> B
In that case, when I go back to Fragment B the second time, the previous scroll position is maintained, which I don't want. Instead, I would like for Fragment B to start with its ListView at the top of its content.
Things I have tried which do nothing:
Calling setSelection(0) in onActivityCreated
Calling setSelectionAfterHeaderViews() in onActivityCreated
Calling smoothScrollToPosition(0) in onActivityCreated
Interestingly, all of these work if I post them on a Runnable. However, when I do that there is a weird flickering the second time I open Fragment B.
So, how do I get Fragment B to automatically scroll to the top each time it is attached to its parent Activity? I feel like there must be something blindingly obvious that I'm missing, but I'm really stumped right now.
You're calling the right methods, but you're calling them in the wrong place.
I assume you have code that switches between the fragments and you call it when an item is clicked in A. So whenever you do the switch set the scroll to the top, something along these lines:
protected void switchList() {
ListFragment a = (ListFragment) getFragmentManager().findFragmentByTag("a");
ListFragment b = (ListFragment) getFragmentManager().findFragmentByTag("b");
b.getListView().setSelectionAfterHeaderView();
getFragmentManager().beginTransaction().hide(a).show(b).addToBackStack(null).commit();
}
And one important note: never keep persistent references to fragments in your activities. Whenever you need a fragment get it from the FragmentManager. This is crucial since on configuration change (like a device rotation, or when your app is suspended and restored) the fragments are recreated, and the reference you kept leads to a 'dead' fragment. Not only is it a major leak, it will also prevent your code from functioning. any change you make to the saved fragment is not reflected on the screen because the screen holds the newly created fragment.

Categories

Resources