Why does the setUserVisibleHint called before onCreateView in Fragment? - android

I have searched many post on stackoverflow but didn't get the reason behind this.

If you look at the documentation (link), this function is used by the system to prioritize; therefore you want the system to know which priority has this fragment before you ask the system to create the view.

Related

How can I track if UI is fully drawed in Android?

From Activity lifecycle we know, that in onResume the UI is visible. But if I set a breakpoint on onResume, I still don't see it, only after that. I heard there's some method which can check it, but I can't remind how it calls. We used it to make better animation. So how can I be fully sured that UI is ready?
Add global layout listener on view you want to check. Also please make sure that you remove listener once your work is done else it will keep getting called multiple times.
#onik shared a good link that should solve your problem
This ensures lay-outing. Drawing happens continuously and frame are refreshed according to sys clock.
I recommend watching: https://www.youtube.com/watch?v=Q8m9sHdyXnE
This will give you good idea on android drawing and layouting

Update a layout after an orientation change in a Fragment

Good day,
I'm working on an application that will serve as a monitor of some sort for drivers. My client would like the application to work regardless of the orientation of the device.
I implemented the solution provided in the following article , and after fiddling a bit with the debugger, I can see that the Asynctask is still working. However, the TextViews and ImageViews it is supposed to work on are not working anymore.
Here is the code of my TaskFragment.
To clarify : The AsyncTask still receive and handle the elements correctly, but the elements of the layout are not updated anymore. I would like to know how I can keep them working.
I would suggest using an AsyncTaskLoader as those can re-attach to whatever lifecycle element you created it in relatively easily. See here: https://developer.android.com/reference/android/content/AsyncTaskLoader.html
It might seem pretty involved to implement at first, however if you read https://developer.android.com/guide/components/loaders.html, most of the weirdness should be cleared up.
TLDR: Using AsyncTaskLoader allows an easy way for your AsyncTask to be reattached to your fragment after it is destroyed and recreated. You just need to call getLoaderManager().initLoader(...) in the onCreate of your fragment.
Ok, so I found a (probably not very efficient) workaround, but a workaround nonetheless.
The problem was that, after an orientation change, since the Activity is destroyed and recreated, the variables batterymonitor, valuemonitor, etc, would not point towards the new objects created because of the layout/activity change.
As a solution, I am now using a findViewById each time I need to do an operation on the layout. This way, the id is permanently refreshed to keep up with the activity changes on a rotation of the device.
The ugly line I use to do so is :
batteryMonitor = (ImageView)getActivity().findViewById(R.id.batteryMonitor);
batteryMonitor.setImageResource(R.drawable.no_battery);

Android User-interface with regards to fragments; best way to update?

So I have several fragments connected to a viewpager that is just on the generic android set up that comes with Eclipse.
I was wondering; when it comes to updating element views for things like progress bars or textviews; should I update those elements inside of the fragment's .java file or should I update them from some sort of centralized UI controller object?
The way I have it set up now is a controller object with several async tasks inside of it for each fragment.
Well the updation of views inside fragment based applications requires activity to fragment, fragment to fragment communication. So for this you need the event based mechanism so that the your target view/controller get to know that something has happened like say download is complete so hide the progressbar.
So I googled regarding this. I found one libray Otto eventbus. Which helped me in such communications. Hope you get the idea and this will help you as well.

Can anyone please help me in understanding drawableStateChanged() method of toggle button in android?

Can anyone please help me in understanding drawableStateChanged method of toggle button in android??? I wanted to know why exactly it is used for and how to implement it??
I found it on following link:
http://developer.android.com/reference/android/widget/ToggleButton.html.
from reading through the documentation which states:
This function is called whenever the state of the view changes in
such a way that it impacts the state of drawables being shown.
it seems that this function will be called by the framework whenever the component needs to be redrawn and you can override it to (for example) perform so application specific logic which you need to do when the component is redrawn, like manually drawing something over the top of the component, or changing the font or doing something which is not possible using the stock attributes.
This question has an example of how you might implement it.

DialogFragment vanishes on orientation change

Although i saw many issues on this an tried to implement all of them i decided to ask this question again since none of the suggested methods worked for me.
Problem:
Im trying to show a custom dialog based on a DialogFragment inside an ActivityFragment.
the Activity is being recreated on orientation change since it has a different layout.
Every time that happens the DialogFragment vanishes. (i am using the latest support package)
Things i have tired:
using the onRetainCustomNonConfigurationInstance to try and save the dilaog.
Use setRetainInstance (true) in the dialog onCreate.
a static newInstance() method in the dialog.
override the dialog onDestroy to remove the destroy listener on the inner dialog
and some other documented solutions.
nothing seems to work, i tired with may variations of these solutions, the reason my custom dialog holds many ui elements in various states and i really have to get this to work.
If anyone could please provide some code to a solution it would be much appreciated.
Thanks,
Totem
The problem was that the FragmentManager is also retained through the onSaveInstanceState() of the parent which i neglected to call when overriding it in the ActivityFragment for my own purposes.
Thanks,
Totem

Categories

Resources