Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access parent Activity View in Fragment

I have an ActionBarActivity and fragment. I am using FragmentPagerAdapter that provides fragment to my app. My question How can I access parent Activity View in Fragment ??

like image 632
Bot Avatar asked Apr 05 '14 16:04

Bot


People also ask

How do you find the parent activity of a fragment?

Simply call your parent activity using getActivity() method.

How can I see the views inside a fragment?

Simply declare TextView as public in fragment, initialize it by findViewById() in fragment's onCreateView(). Now by using the Fragment Object which you added in activity you can access TextView. Save this answer.

Can we navigate from activity to fragment?

Move activity logic into a fragment With the fragment definition in place, the next step is to move the UI logic for this screen from the activity into this new fragment. If you are coming from an activity-based architecture, you likely have a lot of view creation logic happening in your activity's onCreate() function.


1 Answers

You can use

View view = getActivity().findViewById(R.id.viewid); 

Quoting docs

Specifically, the fragment can access the Activity instance with getActivity() and easily perform tasks such as find a view in the activity layout

like image 177
Raghunandan Avatar answered Sep 20 '22 14:09

Raghunandan