Android fragment event listeners - android

As i understand it, would it not be better to create a fragments event listeners in the fragments onCreate? If it is done in onCreateView it would have to be redone everytime the fragment comes back into view (onResume())? Would it make a difference where it is set?

1) yes it can be better
2) yes you are correct, as event listener is initialised there than the process will be repeated here
3) please have a close look at life-cycle method
4)it always makes difference where you are using and when you are initializing.

An event is set on a view and hence is restored when the view is restored. For example, lets say we set a listener on a button in a fragment that is created in onCreateView. If at some point of time, if the fragment is restored (without a call to onCreateView), the button is restored along with the listener that is set. The same goes for setting a text/background for a button - you don't have to reset the text/background each time the fragment is resumed.

Related

Force onDestroyView on a fragment's view

I'm writing an app that loads user profiles and user's ratings for different places. The app uses fragments pervasively, and it's relatively easy to jump from a profile to a rated place.
As a user clicks a profile and gets to a rated place, they can click another profile on a rated place and go on and on.
The problem I'm having is memory related, when I'm looking at a ranked place and I click a profile, I switch from one Activity to the next. In both of these activities, after setContentView I load a fragment dynamically into layout space.
Now, as I shuttle between these activities, onSaveInstance state is almost always called, however since the Fragment displaying whatever was in the foreground before the activity switch, onDestroyView is not called.
What I would like is when onSaveInstanceState is called in these dynamic fragments, it to force onDestroyView to be called as well.
The only time onDestroyView seems to be called is when I add a Fragment to the back stack. When another activity comes to the foreground and this fragment is stopped, I'd like on DestroyView to be called as well.
The current workaround I want to implement is have an empty fragment with no view, and every time I call startActivity(Intent i), load this dummy fragment to destroy views and start the next activity. Then, when I come back, pop it off the back stack and restore the actual last fragment.
This however seems excessive. So, for a stopped fragment in a stopped activity with a new activity in front of it, how do I force it to destroy it's View?
First, you should not force or satisfy onDestroyView to fix your code, that's the job of the FragmentManager and the Android lifecyle # Pausing and Resuming an Activity. If you want to work with your existing code, use the other override methods onPause() or onStop().
Without posted code, I assume you're using the replace() method to display one fragment over another. This more or less forces you to manage the fragments yourself, some developers actually succeed in doing so with some struggle (look at other SO questions).
So my suggestion for you is either:
Maintain your own states, and show the proper fragments based on the state.
Use the BackStack and let the Fragment management handle the stack/states.

OnCreateView() not called on click of "Up" button

I have a scenario where I have 2 fragments.
Clicking on a button in the first fragment takes you to the 2nd fragment.
By clicking the "UP" button in the 2nd fragment you'll get navigated back to the first fragment. Unfortunately the OnCreateView() method of the first fragment is not called.
Is there a way to call it? Which methods are called by clicking the "up" button?
It won't be called since 1st fragment is not yet detached from its activity and not destroyed yet. In your case, onResume() callback would be the best place to put your code.
OnCreateView doesnt get called because the fragment A is already created.
Read about the Fragmetn/Activity lifecycle and u will understand that.
OnResume will get called once pressed back from Fragment B, so u can put your logic in that method.

Fragment onStart and onCreateActivity called after back button press

Scenario:
I have a single Activity in which I have multiple fragments and I'm replacing one fragment with another using fragment transaction and add them to BackStack. I am doing JSON parsing and network related task on some fragments.
Problem:
My problem is that after replacing the fragment when I press back button to nevigate to last fragment the onStart and onActivityCreated methods called again. My code in these events execute each time I navigate to that fragment by using back button
But
Any value in EditText remain same in even after replacing the fragment and coming back to it using back button.
Why onStart and onActivityCreated executed each time?
Is there any method where I can put my code which do not execute after coming back to fragment?
UPDATE
Basically I want to set a button text once fragment is created. User can change that text. but when I return back to that fragment the users value change with the default text which I set on fragment creation time.
Thanks
You can put your code to run only one time at fragment creation in onCreate() of fragment..
onCreate()
The system calls this when creating the fragment. Within your implementation, you should initialize essential components of the fragment that you want to retain when the fragment is paused or stopped, then resumed.
See the life cycle of fragment.
You can also get more details here

View state at fragment on backstack with retaininstance = true

I have got loader in fragment and it loads data on background. After data are loaded, I fill edittexts with that informations. Problem is that if user changes something in edittexts and rotate screen, onLoadFinished is called again and edittexts are replaced with loaded information. I solve this by adding help variable, if data was already loaded .. But when i replace this fragment with other, rotate screen back and forth and press back button, edittexts are empty. Fragment is set to retain instance true. It looks like views lost its state when fragment is on backstack. Anyone familiar with this?
You shouldn't use the retain state.
But the proper way to do so it to save the save using the Bundle and restore it when you recreate the activity (onCreate Bundle is not new).
Please review the link I've sent you it includes a very specific example.
from the android dev guide:
To properly handle a restart, it is important that your activity
restores its previous state through the normal Activity lifecycle, in
which Android calls onSaveInstanceState() before it destroys your
activity so that you can save data about the application state. You
can then restore the state during onCreate() or
onRestoreInstanceState().
Android Rotation Change

Working with onPause, onRestart, in Tab Widget - Android

I have a ListView that is inside a TabWidget. When I select an item on the ListView and go to the child ListView, the TabWidget disappears. This is fine, except that it invokes the onPause method, and thus causes onRestart to be called when I return to the parent ListView.
I have onRestart setup to retrieve updated data from the server, but I do not want this to occur everytime the user returns to the parent ListView. I only want onRestart to be called when the app comes alive from running in the background. I have tried implementing a Boolean variable to determine if I should execute the code that is inside onRestart, but there doesn't seem to be a way to get around the effects of this.
Ideas?
You could fire off your child activity with startActivityForResult and set a flag in onActivityResult to not reload (which should be called when the user backs into the listview from the detail page). You'd have a member variable (let's say mReload) that you'd set to true in onCreate and onRestart, and to false in onActivityResult, then process the reload onResume if mReload is true (and set it back to false so a normal resume won't trigger the reload).
Alternative: just set a flag (mLeftPage) in your activity on the row's click listener. On restart, if mLeftPage is true, don't reload the list and set mLeftPage back to false. Otherwise, reload.

Categories

Resources