User Tools

Site Tools


android_learning:headfirst_android_development_notes:chapter_7

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
android_learning:headfirst_android_development_notes:chapter_7 [2016/02/08 01:32] – [p. 299] mithatandroid_learning:headfirst_android_development_notes:chapter_7 [2016/02/21 03:33] (current) mithat
Line 1: Line 1:
-<WRAP center round info 60%> +====== Chapter 7 ======
-Under development. +
-</WRAP> +
- +
-====== Chapter 7 notes ======+
  
 There's a //lot// of new stuff in this chapter, so you may want to set aside extra time. There's a //lot// of new stuff in this chapter, so you may want to set aside extra time.
Line 14: Line 10:
 Finally, in ''MainActivity.java'', if the ''MainActivty'' class extends ''android.support.v7.app.AppCompatActivity'', change it so it extends ''android.app.Activity'' instead. If you fail to do so, things will break around p. 302 when working with the back stack. Note that you will have to make two changes: one to change the ''extends'' class and the other to the ''import'' statement. Finally, in ''MainActivity.java'', if the ''MainActivty'' class extends ''android.support.v7.app.AppCompatActivity'', change it so it extends ''android.app.Activity'' instead. If you fail to do so, things will break around p. 302 when working with the back stack. Note that you will have to make two changes: one to change the ''extends'' class and the other to the ''import'' statement.
  
 +<WRAP center round tip 90%>
 +In an Android Studio's Design view, you can switch between various devices and orientations using the toolbar at the top of the frame.
 +{{:android_learning:headfirst_android_development_notes:android-studio-design-view-devices.png?direct|}}
 +</WRAP>
 ===== p. 275: Code block ===== ===== p. 275: Code block =====
 <file java Workout.java> <file java Workout.java>
Line 71: Line 71:
 You should change your code to use ''import android.app.Fragment'' and include the empty public constructor. You should change your code to use ''import android.app.Fragment'' and include the empty public constructor.
  
-===== p. 281 ===== +
-<WRAP center round todo 90%> +
-Should ''MainActivity.java'' import ''android.app.Activity'' or ''android.support.v7.app.AppCompatActivity''? +
-</WRAP>+
  
 ===== p. 290 ===== ===== p. 290 =====
-Again, change the Fragment import statement in ''WorkoutListFragment.java'' to <code java>import android.app.Fragment;</code> and leave the <code java>public WorkoutListFragment()</code> definition in place.+Change the Fragment import statement in ''WorkoutListFragment.java'' to <code java>import android.app.Fragment;</code> and leave the <code java>public WorkoutListFragment()</code> definition in place.
  
 ===== p. 292: A matter of style ===== ===== p. 292: A matter of style =====
Line 114: Line 111:
 </file> </file>
  
-<WRAP center round tip 90%> 
 To create an interface file in Android Studio, click on //New > Java class//. If you are asked to specify where to create the new file select the desired option. Then in the Create New Class dialog, select //Interface// from the dropdown list, fill in the name for the interface, and click //OK//. To create an interface file in Android Studio, click on //New > Java class//. If you are asked to specify where to create the new file select the desired option. Then in the Create New Class dialog, select //Interface// from the dropdown list, fill in the name for the interface, and click //OK//.
-</WRAP> 
  
 ==== "References" to interfaces ==== ==== "References" to interfaces ====
  
-You also may not have seen "references" to interfaces as seen in the statement: <code java>private WorkoutListListener</code>+You also may not have seen "references" to interfaces as seen in the statement: <code java>private WorkoutListListener listener;</code> in ''WorkoutListFragment.java''.
  
 A post on Stack Overflow(("Java: Interface reference?" http://stackoverflow.com/questions/22847942/java-interface-reference. Accessed 2015-02-07.)) does a good job of explaining "references" to interfaces. In particular, given interface: A post on Stack Overflow(("Java: Interface reference?" http://stackoverflow.com/questions/22847942/java-interface-reference. Accessed 2015-02-07.)) does a good job of explaining "references" to interfaces. In particular, given interface:
Line 259: Line 254:
     View view = getView();     View view = getView();
     if (view != null) {     if (view != null) {
-        // Cache the bits we'll need:+        // Cache the stuff we'll need:
         TextView title = (TextView) view.findViewById(R.id.textTitle);         TextView title = (TextView) view.findViewById(R.id.textTitle);
         TextView description = (TextView) view.findViewById(R.id.textDescription);         TextView description = (TextView) view.findViewById(R.id.textDescription);
Line 271: Line 266:
 </code> </code>
  
-This seems perfectly sensible, and if you run it it seems to work fine. But if you try to click the "back" button+This seems perfectly sensible, and if you run it it seems to work fine. But if you try to click the "back" button, you have problems.
-<WRAP center round todo 60%> +
-Make and embed video +
-</WRAP>+
  
-The expected behavior is for the back button to show the previously selected details, not the previous Activity (or the home screen if there was none). Thus, the need to put the changes on the //back stack// and use //format transactions//.+The user'expected behavior is for the back button to show the previously selected details. But the back button's default behavior is to show the previous Activity or the home screen if there is none---and fragments are not activities. Thus, the need to push fragment changes onto the //back stack// using //format transactions//.
  
  
Line 303: Line 295:
         WorkoutDetailFragment details = new WorkoutDetailFragment();         WorkoutDetailFragment details = new WorkoutDetailFragment();
  
 +        // Set the new fragment's properties.
 +        details.setWorkoutId(id);
 +        
         // Call up the fragment manager to start a new transaction.         // Call up the fragment manager to start a new transaction.
         FragmentTransaction ft = getFragmentManager().beginTransaction();         FragmentTransaction ft = getFragmentManager().beginTransaction();
- 
-        // Set the new fragment's properties. 
-        details.setWorkoutId(id); 
  
         // Specify the transactions you want to perform:         // Specify the transactions you want to perform:
Line 318: Line 310:
         ft.addToBackStack(null);         ft.addToBackStack(null);
  
-        // Specify a visual transitions to use between old and new fragments+        // Specify a visual transition style to use between old and new fragments
         ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);         ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
  
Line 330: Line 322:
 </WRAP> </WRAP>
  
-All those ''ft.{something}'' statements can get a bit tedious. Many ''FragmentTransaction'' methods support chaining, meaning you can "chain" calls as in the following:+All those ''ft.{something}'' statements can get a bit verbose. Many ''FragmentTransaction'' methods support chaining, meaning you can "chain" calls as in the following:
 <code java> <code java>
 @Override @Override
Line 348: Line 340:
     }     }
 </code> </code>
 +
 +===== p. 304 =====
 +My emulated tablet behaves oddly when started in landscape mode and then rotated to portrait mode. It seems to work as expected if started in portrait mode and then rotated.
 +
 +===== p 316 =====
 +If you try to run the app on a phone-sized device or emulator at this point, it will crash when you click on one of the activities. Can you explain why? (The answer should be clear after you examine the code on p. 321.)
 +
 +===== p. 319: Code block =====
 +Note that ''DetailActivity'' extends ''Activity'' rather than ''AppCompatActivity''.
 +
 +<file java DetailActivity.java>
 +package com.hfad.workout;
 +
 +import android.app.Activity;
 +import android.os.Bundle;
 +
 +public class DetailActivity extends Activity {
 +    public static final String EXTRA_WORKOUT_ID = "id";
 +
 +    @Override
 +    protected void onCreate(Bundle savedInstanceState) {
 +        super.onCreate(savedInstanceState);
 +        setContentView(R.layout.activity_detail);
 +        WorkoutDetailFragment workoutDetailFragment = (WorkoutDetailFragment)
 +                getFragmentManager().findFragmentById(R.id.detail_frag);
 +        int workoutId = (int) getIntent().getExtras().get(EXTRA_WORKOUT_ID);
 +        workoutDetailFragment.setWorkoutId(workoutId);
 +    }
 +}
 +</file>
 +
 +===== p. 321 =====
 +
 +Here's the ''MainActivity'' code with some comments thrown in and some code that's not needed taken out.
 +
 +<file java MainActivity.java>
 +package com.hfad.workout;
 +
 +import android.app.FragmentTransaction;
 +import android.app.Activity;
 +import android.content.Intent;
 +import android.os.Bundle;
 +
 +public class MainActivity extends Activity implements WorkoutListListener {
 +
 +    @Override
 +    protected void onCreate(Bundle savedInstanceState) {
 +        super.onCreate(savedInstanceState);
 +        setContentView(R.layout.activity_main);
 +    }
 +
 +    @Override
 +    public void itemClicked(long id) {
 +        // Check to see if we have a fragment_container 
 +        // ... if we do, then we're on a large screen.
 +        if (findViewById(R.id.fragment_container) != null) {
 +            // Create a new WorkoutDetailFragment to replace the previous one.
 +            WorkoutDetailFragment details = new WorkoutDetailFragment();
 +
 +            // Set the new fragment's properties.
 +            details.setWorkoutId(id);
 +
 +            // Call up the fragment manager and do a new transaction.
 +            getFragmentManager().beginTransaction()
 +                    .replace(R.id.fragment_container, details)
 +                    .addToBackStack(null)
 +                    .setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE)
 +                    .commit();
 +        }
 +        else {
 +            // Create an intent to launch a DetailActivity
 +            Intent intent = new Intent(this, DetailActivity.class);
 +            intent.putExtra(DetailActivity.EXTRA_WORKOUT_ID, (int) id);
 +            startActivity(intent);
 +        }
 +    }
 +}
 +</file>
android_learning/headfirst_android_development_notes/chapter_7.1454895129.txt.gz · Last modified: 2016/02/08 01:32 by mithat

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki