Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Android Android Fragments Managing Fragments Fragment Flavors

Can you please make sure that we retrieve by tag and every time we add or set our fragment in our fragment transactions,

Challenge Task 3 of 3 Awesome! So, finally, now that we are using 2 fragment types we need to remember to set and retrieve our fragments using tags. I've added the tags as constants to the MainActivity.java file. Can you please make sure that we retrieve by tag and every time we add or set our fragment in our fragment transactions, that we set the tag as well.

MainActivity.java
public class MainActivity extends AppCompatActivity implements NewsFragment.NewsListener {
    // New tags for referring to fragments
    public static final String NEWS_FRAGMENT = "news_fragment";
    public static final String NEWSITEM_FRAGMENT = "newsitem_fragment";

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

        FragmentManager fragmentManager = getFragmentManager();

        //NewsFragment savedFragment = (NewsFragment) fragmentManager.findFragmentById(R.id.placeholder);
        savedFragment = fragmentManager.getFragmentManager("NEWS_FRAGMENT");

      //FragmentManager fm = getSupportFragmentManager();
    //fragment = fm.findFragmentByTag("myFragmentTag");


        if (savedFragment == null) {
            NewsFragment newsFragment = new NewsFragment();
            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
            fragmentTransaction.add(R.id.placeholder, newsFragment,"NEWS_FRAGMENT");
            fragmentTransaction.commit();

          //getFragmentManager().beginTransaction().replace(R.id.MainFrameLayout,fragmentA,"YOUR_TARGET_FRAGMENT_TAG").addToBackStack("YOUR_SOURCE_FRAGMENT_TAG").commit(); 

        }

    }

    @Override
    public void onNewsItemSelected(int index) {
        NewsItemFragment fragment = new NewsItemFragment();
        // TODO: how do we pass arguments to the new Fragment?
        FragmentManager fragmentManager = getFragmentManager();
            Bundle bundle = new Bundle();
             FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                bundle.putInt(NewsItemFragment.KEY_NEWS_ITEM_INDEX,0); 
               fragment.setArguments(bundle);

        fragmentTransaction.replace(R.id.placeholder, fragment, "NEWSITEM_FRAGMENT");
        fragmentTransaction.addToBackStack(null);
        fragmentTransaction.commit();
    }
}
NewsItemFragment.java
public class NewsItemFragment extends Fragment {
    public static final String KEY_NEWS_ITEM_INDEX = "news_item_index";

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO:  Hmmm, how do we get this index?
        int index = getArguments().getInt(KEY_NEWS_ITEM_INDEX) ;
        getActivity().setTitle(News.headlines[index]);
        View view = inflater.inflate(R.layout.fragment_newsitem, container, false);
        return view;
    }





    @Override
    public void onStop() {
        super.onStop();
        getActivity().setTitle(getResources().getString(R.string.app_name));
    }
}
News.java
public class News {
    public static String[] headlines = new String[]{
      "Treehouse student develops #1 App in Google App Store",
      "Oracle and Google decide to hug it out",
      "Java8 coming to Android!  Is it too late?"
    };
}

1 Answer

Hi,

Rewatching the video between the 4:00 (minute) to about 6:18 will explain the use of the findFragmentByTag.

Changing the code on three lines in the MainActivity.java will change findFragmentById to use findFragementByTag.

// remove placeholder and change to NEWS_FRAGMENT & findFragmentById to findFragmentByTag
NewsFragment savedFragment = (NewsFragment) fragmentManager.findFragmentByTag(NEWS_FRAGMENT);

// add third parameter
fragmentTransaction.add(R.id.placeholder, newsFragment, NEWS_FRAGMENT);

// add NEWSITEM_FRAGMENT
 fragmentTransaction.replace(R.id.placeholder, fragment, NEWSITEM_FRAGMENT);

Should help,

Michael