简体   繁体   中英

Android getTargetFragment().getView() returning null

I have an issue with orientation changes and a retained Fragment.

In the retained Fragment in onActivityCreated() I use getTargetFragment().getView().find...

Sometimes the method returns null. I do not know why. I use the compatibility library v.4.

I know it's late but I stumbled upon this and happen to have a work around. In my experience when working within fragments and switching between them the onCreate methods fire a little too quickly so sometimes the layout in not fully initialized. The work around I've used fire off a timer task to make the call a little after the onCreate is called. The delay seems to always give it enough time. I have my own timer singleton and you would need to construct your own timer to schedule tasks upon but you get the idea. Hope this helps.

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    final QueueFragmentTab thisFrag = this;

    TimerHelper.getInstance().scheduleTask(new TimerTask() {
        @Override
        public void run() {
             //getview goes here
            Log.w(tag, "" + thisFrag.getView());

        }
    }, 100);
    return inflater.inflate(R.layout.fragment_queue, container, false);

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM