31

parent activity layout

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".LockerCodeActivity" >

    <LinearLayout
        android:id="@+id/fragment_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </LinearLayout>

    <ProgressBar
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:id="@+id/ctrlActivityIndicator"
        android:indeterminateOnly="true"
        android:keepScreenOn="false"
     />

    <TextView
        android:id="@+id/tv_results"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="" />

</RelativeLayout>

Inflate the fragment in the parent activity onCreate function

FragmentManager fragmentManager = getFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    Fragment scannerFragment = new ScanFragment();
    fragmentTransaction.add(R.id.fragment_container, scannerFragment);
    fragmentTransaction.commit();

Working great so far... now how do I hide the progressbar? This is what I've tried

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_scan, container, false);

    ProgressBar progressBar = (ProgressBar) view.findViewById(R.id.ctrlActivityIndicator);
    progressBar.setVisibility(View.INVISIBLE);
    return view;
    }

I get a null pointer exception

Matt
  • 2,341
  • 4
  • 20
  • 20

3 Answers3

64

Since you want the Activity's views, you're going to want to do this:

ProgressBar progressBar = (ProgressBar) getActivity().findViewById(R.id.ctrlActivityIndicator);

You call getActivity() to get the Activity instance then you use findViewById() as normal (provided that R.id.ctrlActivityIndicator is part of the Activity layout, you won't get NPEs).

A--C
  • 36,351
  • 10
  • 106
  • 92
3

This didn't work for me unitl i cast the result of getActivity to the parent activity. See link below: Call parent's activity from a fragment

Community
  • 1
  • 1
m-j
  • 31
  • 2
  • 1
    It is good practice to include the relevant information from the link in your answer. What if the link breaks in the future? – Theresa May 16 '15 at 22:36
  • As a thumb rule, you should always cast the getActivity() method call to the activity it supposedly returning per se the logic of your code. And to safely do that you make a check befor casting. Example below, to cast the activity returned by getActivity method inside fragment to MainActivity if (getActivity instanceof MainActivity){ MainActivity activity = (MainActivity) getActivity(); } – Khay May 17 '17 at 18:32
2

You can use:

((ParentActivity)getActivity()).UI_COMPONENT_NAME 

to access the ui component of the parent activity.

UI_COMPONENT_NAME is the name of the component as initialised in the ParentActivity.

Anubhav
  • 1,984
  • 22
  • 17