title: Android - How to reference a Fragment from another Fragment within the same Activity?
body: <p><em><strong>PROBLEM:</em></strong></p>
  
  <p>When I try to reference the layout of one Fragment from another via the parent Activity. The layout I am trying to access is always null.</p>
  
  <p>I believe it is something to do with the onCreate() and onCreateView() not being called when created via a newInstance() method from an Activity. This is an area of Android that is a bit unfamiliar to me and I am not 100% sure on how the Activity &amp; Fragment lifecycle interact. Brief overview in background and all code included at the bottom of question.</p>
  
  <p>It is most likely a simple fix that I am currently blind to. Detail is included for the sake of completeness.</p>
  
  <p><em><strong>BACKGROUND:</em></strong></p>
  
  <p>The problem is, I have two <code>Fragments</code> hosted by an <code>Activity</code> Class. The <code>Activity</code> class is made up a standard base <code>Activity</code> class which I use for many Activities across the app. This keeps reference to the ActionBar Fragments layout (for changing the title, images etc) and contains methods for getting a new ActionBar instance etc. (Key method here is <code>setActionBarTitle(title)</code> - referred to later) Code included below <strong>[1]</strong> </p>
  
  <p>The second part of the <code>Activity</code> inheriting class that contains the two Fragments is a task specific <code>Activity</code>, in this case a help <code>Activity</code>. </p>
  
  <p>This is the <code>Activity</code> that loads the action bar fragment (via <code>newInstance(title)</code> method from base class) and  the corresponding fragment, in this case help fragment. <strong>[2]</strong></p>
  
  <p><em><strong>WHERE THINGS GO WRONG:</em></strong> </p>
  
  <p>In the <code>onAttach()</code> method, I get reference to the parent <code>Activity</code> - <code>Activity_Help</code>.</p>
  
  <p>The problem comes in the <code>onCreateView()</code> of my help <code>Fragment</code> <strong>[3]</strong>. The goal is to access the setActionBarTitle method of Activity_Help.</p>
  
  <p>As Activity_Help extends the Activity base class (General_Activity), the <code>setActionBarTitle()</code> method and the stored actionBarLayout are both accessible. It seems like it should have access to everything neccessary to change the action bar fragment text?</p>
  
  <p>However, this is what goes on..</p>
  
  <ul>
  <li><p>[Fragment_Help] <code>parentActivity.setActionBarTitle("New title");</code> Leads to..</p></li>
  <li><p>[General_Activity] <code>fragmentActionBar.setTitle(newTitle);</code> Leads to..</p></li>
  <li><p>[Fragment_Example1_ActionBar] <strong><code>TextView tvTitle = (TextView) actionBarLayout.findViewById(R.id.tvCustomActionBarTitle);</code> ERROR LINE - actionBarLayout is null</strong></p></li>
  </ul>
  
  <p>Full code for action bar fragment is included at the very bottom. <strong>[4]</strong></p>
  
  <p>I believe as first mentioned is in the Activity_Help.fragmentActionBar not keeping it's layout. When it is accessed for the second time to change the <code>TextView</code>, the layout is null hence NPE.</p>
  
  <p>Any advice would be truly appreciated!</p>
  
  <p><strong>Superclass Activity code [1]</strong></p>
  
  <pre><code>public class General_Activity extends Activity
  {
       Fragment_Example1_ActionBar fragmentActionBar;
  
           ....
  
           // Start Help Activity
           case R.id.bMenuHelp:
          Intent helpIntent = new Intent(this, Activity_Help.class);
          helpIntent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
          startActivity(helpIntent);
          break;
  
           ....
  
       // Create a new ActionBar with given title
       public void newActionBarInstance(String initialTitle)
       {
          fragmentActionBar = Fragment_Example1_ActionBar.newInstance(initialTitle);
       }  
  
       // Create a new ActionBar with no title
       public Fragment_Example1_ActionBar newActionBarInstance()
       {
      return Fragment_Wurth_Example1_ActionBar.newInstance("");
  }   
  
      // Set ActionBar title
      public void setActionBarTitle(String newTitle)
      {
          fragmentActionBar.setTitle(newTitle);
      }
  }
  </code></pre>
  
  <p><strong>Activity_Help code [2]</strong></p>
  
  <pre><code>public class Activity_Help extends General_Activity
  {
  @Override
  protected void onCreate(Bundle savedInstanceState)
  {       
      super.onCreate(savedInstanceState);
      setContentView(R.layout.fragment_container_layout);
  
      FragmentManager fragmentManager = getFragmentManager();
  
      Fragment_Help fragmentWholeScreen = new Fragment_Help();
  
      // SC - Create new ActionBar with given title
      newActionBarInstance("Help");
  
      if (!fragmentWholeScreen.isAdded())
      {
          FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();           
          fragmentTransaction.add(R.id.fragment_container_layout_example1, fragmentWholeScreen);
          fragmentTransaction.add(R.id.fragment_action_bar, fragmentActionBar);
          fragmentTransaction.commit();
      }
  }
  </code></pre>
  
  <p>};</p>
  
  <p><strong>Fragment_Help code [3]</strong></p>
  
  <pre><code>public class Fragment_Help extends Fragment
  {
  Activity_Help parentActivity;
  
  @Override
  public void onAttach(Activity activity)
  {       
      super.onAttach(activity);       
      parentActivity = (Activity_Help) activity;      
  }   
  
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,  Bundle savedInstanceState)
  {
      View helpLayout = inflater.inflate(R.layout.fragment_help, null);
  
      parentActivity.setActionBarTitle("New title");
  
      return helpLayout;
  }
  </code></pre>
  
  <p>}</p>
  
  <p><strong>Fragment_Example1_ActionBar code [4]</strong></p>
  
  <pre><code>public class Fragment_Example1_ActionBar extends Fragment
  {   
  OnCustomActionBarItemSelectedListener onCustomActionBarItemSelectedListener;
  View actionBarLayout;
  String title;
  
  // Pass in original title
  @Override
  public void onCreate(Bundle savedInstanceState)
  {
      Log.i("ACTiON_BAR", "++ ON CREATE ++ " + System.currentTimeMillis());
  
      super.onCreate(savedInstanceState);     
  
      Bundle receivedArguments = getArguments();
  
      title = receivedArguments.getString(App.ACTION_BAR_TITLE_KEY);
  
      actionBarLayout = getActivity().getLayoutInflater().inflate(R.layout.action_bar_layout, null);  
  }
  
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,  Bundle savedInstanceState)
  {
      Log.i("ACTiON_BAR", "+ ON CREATE VIEW + " + System.currentTimeMillis());
  
      if (title != null)
      {
          setTitle(title);
      }
  
      // Get reference to ActionBar buttons and set Listeners
      final ImageButton bActionBarLogo = (ImageButton) actionBarLayout.findViewById(R.id.bCustomActionBarHome);
      final ImageButton bActionBarMenu = (ImageButton) actionBarLayout.findViewById(R.id.bCustomActionBarMenu);
      final ImageButton bActionBarVehicleCheckIn = (ImageButton) actionBarLayout.findViewById(R.id.bCustomActionBarVehicleCheckIn);
  
      bActionBarLogo.setOnClickListener(actionBarButtonListener);
      bActionBarMenu.setOnClickListener(actionBarButtonListener);
      bActionBarVehicleCheckIn.setOnClickListener(actionBarButtonListener);
  
      return actionBarLayout;
  }
  
  // Intermediate menu button listener calling parent Activity implementation
  OnClickListener actionBarButtonListener = new OnClickListener()
  {       
      public void onClick(View actionBarItem)
      {
          onCustomActionBarItemSelectedListener.onCustomActionBarItemSelected(actionBarItem);         
      }
  };
  
  // Set ActionBar title
  public void setTitle(String newTitle)
  {
      // Get refence to the title TextView in the custom layout
      TextView tvTitle = (TextView) actionBarLayout.findViewById(R.id.tvCustomActionBarTitle);
  
      // Set the title
      tvTitle.setText(newTitle);
  }
  
  // Set ActionBar text size
  public void setTextSize(float textSize)
  {
      // Get refence to the title TextView in the custom layout
      TextView tvTitle = (TextView) actionBarLayout.findViewById(R.id.tvCustomActionBarTitle);
  
      // Set the title
      tvTitle.setTextSize(textSize);
  }
  
  // Get reference to parent activity and it's Action Bar listener
  @Override
  public void onAttach(Activity activity)
  {
      Log.i("ACTiON_BAR", "+++ ON ATTACH +++ " + System.currentTimeMillis());
  
      super.onAttach(activity);
  
      try
      {
          // Get reference to Activity's implementation of listener
          onCustomActionBarItemSelectedListener = (OnCustomActionBarItemSelectedListener) activity;   
      }
      catch (ClassCastException e)
      {
          // If Activity doesn't implement listener, throw exception explaining error
          throw new ClassCastException(activity.toString() + " must implement OnCustomActionBarItemSelectedListener"); 
      }
  }   
  
  // New instance - keep the creation within the class in one place
  public static Fragment_Example1_ActionBar newInstance(String initialTitle)
  {
      // Package the title up in a bundle
      Bundle titleBundle = new Bundle();
      titleBundle.putString(App.ACTION_BAR_TITLE_KEY, initialTitle);
  
      Fragment_Example1_ActionBar fragmentActionBar = new Fragment_Example1_ActionBar();
      fragmentActionBar.setArguments(titleBundle);
  
      return fragmentActionBar;
  }
  
  // New instance - keep the creation within the class in one place
  public static Fragment_Example1_ActionBar newInstance()
  {
      // Package the title up in a bundle
      Bundle titleBundle = new Bundle();
      titleBundle.putString(App.ACTION_BAR_TITLE_KEY, "");
  
      Fragment_Example1_ActionBar fragmentActionBar = new Fragment_Example1_ActionBar();
      fragmentActionBar.setArguments(titleBundle);
  
      return fragmentActionBar;
  }               
  }
  </code></pre>
  
  <p>Cheers!</p>