getVisibility ViewVISIBLE if savedInstanceState null mCurPosition

Getvisibility viewvisible if savedinstancestate null

This preview shows page 21 - 26 out of 55 pages.

&& detailsFrame.getVisibility() == View.VISIBLE; if (savedInstanceState != null) { mCurPosition = savedInstanceState.getInt("curChoice", 0); } if (mShowTwoFragments == true || mCurPosition != -1) { viewVeggieInfo(mCurPosition); } getFragmentManager().addOnBackStackChangedListener(this); } Most of the Fragment control’s initialization happens in the onActivityCreated() callback method so that we initialize the ListView only once. We then check to see which display mode we want to be in by checking to see if our second component is defined in the layout.
Image of page 21

Subscribe to view the full document.

Implementing a ListFragment (Cont’d) void viewVeggieInfo(int index) { mCurPosition = index; if (mShowTwoFragments == true) { // Check what fragment is currently shown, replace if needed. VeggieGardenWebViewFragment details = (VeggieGardenWebViewFragment) getFragmentManager() .findFragmentById(R.id.veggieentry); if (details == null || details.getShownIndex() != index) { VeggieGardenWebViewFragment newDetails = VeggieGardenWebViewFragment.newInstance(index); FragmentManager fm = getFragmentManager(); FragmentTransaction ft = fm.beginTransaction(); ft.replace(R.id.veggieentry, newDetails); Details on the next slide!!
Image of page 22
Implementing a ListFragment (Cont’d) We leave the display details to the helper method called viewVeggieInfo() , which is also called whenever an item in the ListView control is clicked. The logic for the viewVeggieInfo() method takes into account both display modes. If the device is in portrait mode, the VeggieGardenViewActivity is launched via an Intent . However, if the device is in landscape mode, we have some Fragment finagling to do. Specifically, the FragmentManager is used to find the existing VeggieGardenWebViewFragment by its unique identifier ( R.id.veggieentry , as defined in the layout resource file). Then, a new VeggieGardenWebViewFragment instance is created for the new veggie blog post being requested. Next, a FragmentTransaction is started, in which the existing VeggieGardenWebViewFragment is replaced with the new one. We put the old one on the back stack so that the Back button works nicely, set the transition animation to fade between the blog entries, and commit the transaction, thus causing the screen to update asynchronously.
Image of page 23

Subscribe to view the full document.

Implementing a ListFragment (Cont’d) if (index != -1) { String[] veggies = getResources().getStringArray(R.array.veggies_array); String strBackStackTagName = veggies[index]; ft.addToBackStack(strBackStackTagName); } ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE); ft.commit(); } } else { Intent intent = new Intent(); intent.setClass(getActivity(), VeggieGardenViewActivity.class); intent.putExtra("index", index); startActivity(intent); } } }
Image of page 24
Implementing a ListFragment (Cont’d) @Override public void onBackStackChanged() { VeggieGardenWebViewFragment details = (VeggieGardenWebViewFragment) getFragmentManager() .findFragmentById(R.id.veggieentry); if (details != null) { mCurPosition = details.getShownIndex(); getListView().setItemChecked(mCurPosition, true); if (!mShowTwoFragments) { viewVeggieInfo(mCurPosition); } } } Finally, we can monitor the back stack with a call to the addOnBackStackChangedListener() method. The callback, onBackStackChanged() , updates the list to the current selected item. This provides a
Image of page 25

Subscribe to view the full document.

Image of page 26
  • Summer '16

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern

Ask Expert Tutors You can ask You can ask ( soon) You can ask (will expire )
Answers in as fast as 15 minutes