0

EDIT

Changed title.

SDK Guide document says, Activity.onCreate complete after Fragment.onCreateView and Fragment.onAcvityCreated.

But If I try findViewById for a view of the fragment it returns null.

How can I access contents of the fragment?


I'm very new to Android UI dev.

Below is a sample code generated by Eclipse IDE.

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        if (savedInstanceState == null) {
            getFragmentManager().beginTransaction().add(R.id.container, new PlaceholderFragment()).commit();

            // this is null
            View rootView = findViewById(R.id.txtView);
        }
    }

    public static class PlaceholderFragment extends Fragment {

        public PlaceholderFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_main, container, false);
            return rootView;
        }
    }

}

First of all, I want to access inner contents of the 'fragment_main'.
Can I do this with findViewById?

I found that calling findViewById for a view of the fragment at onCreate call is not working.
How do I know when the Fragment views are ready at the Activity level?

I read How to implement OnFragmentInteractionListener

Am I needed to manually implement a event listener for this?

Community
  • 1
  • 1
9dan
  • 4,222
  • 2
  • 29
  • 44
  • which on `Oncreate` are you calling the view from fragment? – Rod_Algonquin Aug 18 '14 at 03:22
  • Access fragment views in the fragment itself, using its lifecycle callbacks such as `onCreateView()`. http://stackoverflow.com/questions/23653778/nullpointerexception-accessing-views-in-oncreate – laalto Aug 18 '14 at 12:23

2 Answers2

0
But If I try findViewById for a view of the fragment it returns null.

You can not just access the view of the fragment in your activity's oncreate or where ever, you can call view of the fragment in your activity.

I found that calling findViewById for a view of the fragment at onCreate call is not working.

That is because the view is not inflated yet in your fragment thus returning null.

Have a look at the fragment life cycle:

View Ghazanfar Abbas's profile on LinkedIn
(source: xamarin.com)

As you can see onCreate is before onCreateView which you inflate your view for the fragment's layout.

solution:

you call findViewByIdit in your fragment's onActivityCreated.

sample:

@Override
    public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    TextView sample = (TextView) getView().findViewById(your_id);
    }
Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Rod_Algonquin
  • 26,074
  • 6
  • 52
  • 63
0

I think short answer is 'impossible' or 'not works like that'.

If one want to manage inner contents of fragments just delete fragments and move all the contents to the activity layout.

9dan
  • 4,222
  • 2
  • 29
  • 44