|
1 | 1 | /* |
2 | | -* Copyright (C) 2017 The Android Open Source Project |
3 | | -* |
4 | | -* Licensed under the Apache License, Version 2.0 (the "License"); |
5 | | -* you may not use this file except in compliance with the License. |
6 | | -* You may obtain a copy of the License at |
7 | | -* |
8 | | -* http://www.apache.org/licenses/LICENSE-2.0 |
9 | | -* |
10 | | -* Unless required by applicable law or agreed to in writing, software |
11 | | -* distributed under the License is distributed on an "AS IS" BASIS, |
12 | | -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13 | | -* See the License for the specific language governing permissions and |
14 | | -* limitations under the License. |
15 | | -*/ |
| 2 | + * Copyright (C) 2017 The Android Open Source Project |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
16 | 16 |
|
17 | 17 | package com.example.android.android_me.ui; |
18 | 18 |
|
| 19 | +import android.os.Bundle; |
19 | 20 | import android.support.v4.app.FragmentManager; |
20 | 21 | import android.support.v7.app.AppCompatActivity; |
21 | | -import android.os.Bundle; |
22 | 22 |
|
23 | 23 | import com.example.android.android_me.R; |
24 | 24 | import com.example.android.android_me.data.AndroidImageAssets; |
25 | 25 |
|
26 | | -// This activity will display a custom Android image composed of three body parts: head, body, and legs |
| 26 | +// This activity will display a custom Android image composed of three body parts: head, body, and |
| 27 | +// legs |
27 | 28 | public class AndroidMeActivity extends AppCompatActivity { |
28 | 29 |
|
| 30 | + @Override |
| 31 | + protected void onCreate(Bundle savedInstanceState) { |
| 32 | + super.onCreate(savedInstanceState); |
| 33 | + setContentView(R.layout.activity_android_me); |
29 | 34 |
|
30 | | - @Override |
31 | | - protected void onCreate(Bundle savedInstanceState) { |
32 | | - super.onCreate(savedInstanceState); |
33 | | - setContentView(R.layout.activity_android_me); |
34 | | - |
35 | | - // Only create new fragments when there is no previously saved state |
36 | | - if(savedInstanceState == null) { |
37 | | - |
38 | | - // TODO (5) Retrieve list index values that were sent through an intent; use them to display the desired Android-Me body part image |
39 | | - // Use setListindex(int index) to set the list index for all BodyPartFragments |
40 | | - |
41 | | - // Create a new head BodyPartFragment |
42 | | - BodyPartFragment headFragment = new BodyPartFragment(); |
43 | | - |
44 | | - // Set the list of image id's for the head fragment and set the position to the second image in the list |
45 | | - headFragment.setImageIds(AndroidImageAssets.getHeads()); |
46 | | - headFragment.setListIndex(1); |
| 35 | + // Only create new fragments when there is no previously saved state |
| 36 | + if (savedInstanceState == null) { |
47 | 37 |
|
48 | | - // Add the fragment to its container using a FragmentManager and a Transaction |
49 | | - FragmentManager fragmentManager = getSupportFragmentManager(); |
| 38 | + int headIndex = 0, bodyIndex = 0, legIndex = 0; |
50 | 39 |
|
51 | | - fragmentManager.beginTransaction() |
52 | | - .add(R.id.head_container, headFragment) |
53 | | - .commit(); |
| 40 | + Bundle bundle = getIntent().getExtras(); |
| 41 | + if (bundle != null) { |
| 42 | + headIndex = bundle.getInt("headIndex"); |
| 43 | + bodyIndex = bundle.getInt("bodyIndex"); |
| 44 | + legIndex = bundle.getInt("legIndex"); |
| 45 | + } |
54 | 46 |
|
55 | | - // Create and display the body and leg BodyPartFragments |
| 47 | + // Add the fragment to its container using a FragmentManager and a Transaction |
| 48 | + FragmentManager fragmentManager = getSupportFragmentManager(); |
56 | 49 |
|
57 | | - BodyPartFragment bodyFragment = new BodyPartFragment(); |
58 | | - bodyFragment.setImageIds(AndroidImageAssets.getBodies()); |
59 | | - fragmentManager.beginTransaction() |
60 | | - .add(R.id.body_container, bodyFragment) |
61 | | - .commit(); |
| 50 | + BodyPartFragment headFragment = new BodyPartFragment(); |
| 51 | + headFragment.setImageIds(AndroidImageAssets.getHeads()); |
| 52 | + headFragment.setListIndex(headIndex); |
| 53 | + fragmentManager.beginTransaction().add(R.id.head_container, headFragment).commit(); |
62 | 54 |
|
63 | | - BodyPartFragment legFragment = new BodyPartFragment(); |
64 | | - legFragment.setImageIds(AndroidImageAssets.getLegs()); |
65 | | - fragmentManager.beginTransaction() |
66 | | - .add(R.id.leg_container, legFragment) |
67 | | - .commit(); |
68 | | - } |
| 55 | + BodyPartFragment bodyFragment = new BodyPartFragment(); |
| 56 | + bodyFragment.setImageIds(AndroidImageAssets.getBodies()); |
| 57 | + bodyFragment.setListIndex(bodyIndex); |
| 58 | + fragmentManager.beginTransaction().add(R.id.body_container, bodyFragment).commit(); |
69 | 59 |
|
| 60 | + BodyPartFragment legFragment = new BodyPartFragment(); |
| 61 | + legFragment.setImageIds(AndroidImageAssets.getLegs()); |
| 62 | + legFragment.setListIndex(legIndex); |
| 63 | + fragmentManager.beginTransaction().add(R.id.leg_container, legFragment).commit(); |
70 | 64 | } |
| 65 | + } |
71 | 66 | } |
0 commit comments