Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Strange FragmentTransaction in the FragmentLayout class from the ApiDemos sample

I am sorry, this question is for those of you working on Eclipse with access to the ApiDemo sample codes.
Specifically, I am trying to base a fragment activity on the sample called FragmentLayout

The following code is problematic for me (you can find the full code in ApiDemo FragmentLayout.java, ShowDetails() method):

                // Execute a transaction, replacing any existing fragment
                // with this one inside the frame.
                FragmentTransaction ft = getFragmentManager().beginTransaction();
                if (index == 0) {
                    ft.replace(R.id.details, details);
                } else {
                    ft.replace(R.id.a_item, details);
                }
                ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
                ft.commit();

Two questions I have:

  1. What's the difference between index == 0 and index != 0 ?

  2. The resource R.id_a_item (only occurence in all ApiDemos, after searching it) belongs to some kind of menu shortcut resource, not clear at all why it is used here.

The android.developers guide does not explain this bit of code.

like image 240
ilomambo Avatar asked Feb 18 '13 14:02

ilomambo


1 Answers

What's the difference between index == 0 and index != 0 ?

There shouldn't be any differences between position 0 and the other positions of the list as the code is set to simply replace the previous details fragment with a new one.

The resource R.id_a_item (only occurence in all ApiDemos, after searching it) belongs to some kind of menu shortcut resource, not clear at all why it is used here.

Most likely that is an error in the sample as using that id will throw an exception because it doesn't exist in the current layout(I've run the API Demos project found on the 4.2 emulator and it throws that no view found exception...etc for that id). Probably a slip in the last version of the samples as the piece of code your questioning doesn't exist in other versions.

like image 158
user Avatar answered Oct 19 '22 09:10

user