getView() returns null - android

I basicall have an AsyncTask (run from main Activity) that populates a ViewPager inside a fragment. I'm inflating an xml layout file to populate the ViewPager.
The problem is that I can't get pointers to the views inside the layout (imageview, textview) so that I can change then at runtime. I know it's probably because getView() returns null. I've read that it's probably because onCreateView() hasn't been called, yet. Anybody know what I can do to solve this?
My code's a bit of a mess right now.
Here's a simpler explanation to what I'm doing:
MainActivity Asynctask populates a database and sends pointer to Fragment. Something like SendToFragement(db);
The Fragment method ReceiveFromActivity(db) receives db pointer and populates the ViewPager.
It works fine if I'm just creating TextViews, setting text, and adding TextViews to the Viewpager. But, of course, I want to make it look better so I've inflated an xml layout. The problem is that I can't change the contents of the xml layout because getView() is returning NULL so getView().findViewById() doesn't work.

Depending on the implementation of your PagerAdapter, it is possible that a Fragment no longer in view will have their View destroyed in order to free up resources. As such, there's no need to update the View because it will be created again in onCreateView() with the updated information. getView() will return a view in between calls to onCreateView() and onDestroyView(). Outside of that, the Fragment can still exist in memory, but not attached to any View, thus getView() will return null.
So basically,
View fragmentView = getView();
if(fragmentView != null) {
// we are in view or at least exist. Update the elements.
}
// Else don't worry about it. Just update the data.

Related

Android Development - Find Text View Inside of a fragment that's inside of a TabLayout

I realize views cannot be "found" if they're not a direct child of the layout you're looking from. I have an Activity, which has a ViewPager inside of it. The 0th tab or view inside of the ViewPager has a my_fragment.xml inflated in it. That fragment then has a TextView inside of it. How can i access it from my main/root activity. Also, in the onCreate method of the fragment i've put a line of code that just calls setText on the TextView, and that doesn't seem to be called at all. Any ideas on all of this?
To access a view you need not access every parent / super parent of it. The parent or any other parent up in the view hierarchy is enough. If you get the reference to any parent/super parent of the child view, you can call View.findViewById() to get the child.
View.findViewById() - Look for a child view with the given id. If this view has the given id, return this view.
Don't try to setText in the onCreate of Fragment. You need to do this after the fragment inflates the view. This happens only in the onCreateView. So you need to do the setText in the onCreateView method or in any other method after this one in the lifecycle (preferably onActivityCreated()).
To access the fragment class instance itself from the activity, you can refer this: Is it possible to access the current Fragment being viewed by a ViewPager?

Difference between getActivity() and view in Fragment

What is the difference between
getActivity().findViewById(...)
and
View view = inflater.inflate(R.layout.fragment_fragment_v, null);
view.findViewById(...)
in Fragment (when converting Activity to Fragment)?
The difference is that with getActivty.findViewById(...) you are finding views in the scope of activity (activity's layout). With iflater.inflate(R.layout.fragment_fragment_v, null); view.findViewById(...) you are inflating the layout of your fragment and then finding view's in that layout.
But since your fragment is attached to the activity, you will find the view bothways, but I suggest you are finding view's for your fragment in your fragment's scope since there may be several fragments that have common layouts meaning there may be several view's associated with the same ID and that makes the getActivity().findBiewById(...) method unreliable
When you call findViewById with getActivity() you are telling Android to find the View within the Activity's layout, since Fragments are hosted inside Activities it will return you the correct View if its in the hierarchy.
By calling findViewById with the inflated View or in fact the getView() method provided by the framework you are essentially telling Android to find that View within the Fragment's view hierarchy, this should be your preferred approach as the findViewById algorithm will have to look for your View from a lesser set of View's and hence the search will be faster.
I'll leave you with this explanation about getView() from the developer docs.
getView()
Get the root view for the fragment's layout (the one returned by onCreateView(LayoutInflater, ViewGroup, Bundle)), if provided.

Fragment view not updating after reopening

I have a couple text views in a nested fragment that updates as you touch different objects. When I first enter the fragment it works then when I replace the fragment and reopen it. All the views are click able but my text views are not updating. If I do
((TextView)findViewById(R.id.txt_subtitle)).getText()
it returns the updated value as if it was working but the screen is not getting updated.
Its like i have an invisible copy of the text view some where
Unfortunately I can't post the source.
I'm not sure if this would work, since you haven't shown the code, but maybe you can override the onPause() method in the fragment to make the MainActivity class store the TextView information. Then, when you replace the next fragment with the fragment that's giving you the issue, you can set the TextViews to the data that was stored in the MainActivity. Does that make any sense?
EDIT: it might be the onDestroy() method if you don't invoke addToBackStack()

Why does the method "onLayout" of a custom view keep being called when used in a Fragment?

I have a custom view which overrides a method onLayout, and it is in one of my Fragment. I found when this view is created, the onLayout method just keeps being called. I think it's not my view's problem because I have tried several different custom views which extend ImageView or SurfaceView, all have the same problem. When I put my view in an Activity, the method onLayout is just called twice and I know it's ok. Anyone know why and how to fix it?
P.S. my Fragment is added using class FragmentTransaction in my code, is that the reason?

How to reuse fragment's view for a new fragment

I need to update a fragment with a different views.
As I figured out, to force Fragment::onCreateView() method call (to show my updates to the user) I can only use FragmentTransaction's method replace(, ).
It works good if I create a new Fragment and a new view for returning it from onCreateView() method every time I call FragmentTransaction::replace().
But I wish to keep some views in memory.
How can I reuse my views?
The problem is that if I use my view (that was attached to already replaced fragment) at a new fragment, I get an exception:
"java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first."
So, I try to detach the view from fragment by calling FragmentTransaction.detach() or FragmentTransaction.remove() to cause removing fragment from the activity and my view from already replaced fragment, but it doesn't help.
Does anybody know how to force a fragment to remove its child view (that was returned inside onCreateView()?
You're better off keeping the view contained within the Fragment so the Fragment itself handles it appropriately. Either have viewless Fragments send data to that Fragment to update the View or build the View with the parameters. It's cleaner and in the long run, much more maintainable.
Check out the Adding a Fragment without UI section.

Categories

Resources