Skip to content

Commit dacbf67

Browse files
scottamainAndroid (Google) Code Review
authored andcommitted
Merge changes I1df85b6a,I7c6a68b6 into ics-mr1
* changes: docs: second set of diagram updates docs: Update lifecycle diagrams to use new omnigraffle designs; add corresponding graffle files update discussion of activity state restore for clarification
2 parents c697ebf + e63163a commit dacbf67

23 files changed

+28662
-95
lines changed

docs/html/guide/topics/fundamentals/activities.jd

Lines changed: 47 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,10 @@ page.title=Activities
1919
</li>
2020
<li><a href="#StartingAnActivity">Starting an Activity</a>
2121
<ol>
22-
<li><a href="#StartingAnActivityForResult">Starting an Activity for a Result</a></li>
22+
<li><a href="#StartingAnActivityForResult">Starting an activity for a result</a></li>
2323
</ol>
2424
</li>
25+
<li><a href="#ShuttingDown">Shutting Down an Activity</a></li>
2526
<li><a href="#Lifecycle">Managing the Activity Lifecycle</a>
2627
<ol>
2728
<li><a href="#ImplementingLifecycleCallbacks">Implementing the lifecycle callbacks</a></li>
@@ -612,62 +613,63 @@ that
612613
when an activity is paused or stopped, the state of the activity is retained. This is true because
613614
the {@link android.app.Activity} object is still held in memory when it is paused or
614615
stopped&mdash;all information about its members and current state is still alive. Thus, any changes
615-
the user made within the activity are retained in memory, so that when the activity returns to the
616+
the user made within the activity are retained so that when the activity returns to the
616617
foreground (when it "resumes"), those changes are still there.</p>
617618

618-
<div class="figure" style="width:615px">
619-
<img src="{@docRoot}images/fundamentals/restore_instance.png" alt="" />
620-
<p class="img-caption"><strong>Figure 2.</strong> The two ways in which an activity returns to user
621-
focus with its state intact: either the activity is stopped, then resumed and the activity state
622-
remains intact (left), or the activity is destroyed, then recreated and the activity must restore
623-
the previous activity state (right).</p>
624-
</div>
625-
626619
<p>However, when the system destroys an activity in order to recover memory, the {@link
627620
android.app.Activity} object is destroyed, so the system cannot simply resume it with its state
628621
intact. Instead, the system must recreate the {@link android.app.Activity} object if the user
629622
navigates back to it. Yet, the user is unaware
630623
that the system destroyed the activity and recreated it and, thus, probably
631624
expects the activity to be exactly as it was. In this situation, you can ensure that
632625
important information about the activity state is preserved by implementing an additional
633-
callback method that allows you to save information about the state of your activity and then
634-
restore it when the the system recreates the activity.</p>
626+
callback method that allows you to save information about the state of your activity: {@link
627+
android.app.Activity#onSaveInstanceState onSaveInstanceState()}.</p>
635628

636-
<p>The callback method in which you can save information about the current state of your activity is
637-
{@link android.app.Activity#onSaveInstanceState onSaveInstanceState()}. The system calls this method
638-
before making the activity vulnerable to being destroyed and passes it
639-
a {@link android.os.Bundle} object. The {@link android.os.Bundle} is where you can store
629+
<p>The system calls {@link android.app.Activity#onSaveInstanceState onSaveInstanceState()}
630+
before making the activity vulnerable to destruction. The system passes this method
631+
a {@link android.os.Bundle} in which you can save
640632
state information about the activity as name-value pairs, using methods such as {@link
641-
android.os.Bundle#putString putString()}. Then, if the system kills your activity's
642-
process and the user navigates back to your activity, the system passes the {@link
643-
android.os.Bundle} to {@link android.app.Activity#onCreate onCreate()} so you can restore the
644-
activity state you saved during {@link android.app.Activity#onSaveInstanceState
645-
onSaveInstanceState()}. If there is no state information to restore, then the {@link
646-
android.os.Bundle} passed to {@link android.app.Activity#onCreate onCreate()} is null.</p>
633+
android.os.Bundle#putString putString()} and {@link
634+
android.os.Bundle#putInt putInt()}. Then, if the system kills your application
635+
process and the user navigates back to your activity, the system recreates the activity and passes
636+
the {@link android.os.Bundle} to both {@link android.app.Activity#onCreate onCreate()} and {@link
637+
android.app.Activity#onRestoreInstanceState onRestoreInstanceState()}. Using either of these
638+
methods, you can extract your saved state from the {@link android.os.Bundle} and restore the
639+
activity state. If there is no state information to restore, then the {@link
640+
android.os.Bundle} passed to you is null (which is the case when the activity is created for
641+
the first time).</p>
642+
643+
<img src="{@docRoot}images/fundamentals/restore_instance.png" alt="" />
644+
<p class="img-caption"><strong>Figure 2.</strong> The two ways in which an activity returns to user
645+
focus with its state intact: either the activity is destroyed, then recreated and the activity must restore
646+
the previously saved state, or the activity is stopped, then resumed and the activity state
647+
remains intact.</p>
647648

648649
<p class="note"><strong>Note:</strong> There's no guarantee that {@link
649650
android.app.Activity#onSaveInstanceState onSaveInstanceState()} will be called before your
650651
activity is destroyed, because there are cases in which it won't be necessary to save the state
651652
(such as when the user leaves your activity using the BACK key, because the user is explicitly
652-
closing the activity). If the method is called, it is always called before {@link
653+
closing the activity). If the system calls {@link android.app.Activity#onSaveInstanceState
654+
onSaveInstanceState()}, it does so before {@link
653655
android.app.Activity#onStop onStop()} and possibly before {@link android.app.Activity#onPause
654656
onPause()}.</p>
655657

656658
<p>However, even if you do nothing and do not implement {@link
657659
android.app.Activity#onSaveInstanceState onSaveInstanceState()}, some of the activity state is
658660
restored by the {@link android.app.Activity} class's default implementation of {@link
659661
android.app.Activity#onSaveInstanceState onSaveInstanceState()}. Specifically, the default
660-
implementation calls {@link
661-
android.view.View#onSaveInstanceState onSaveInstanceState()} for every {@link android.view.View}
662-
in the layout, which allows each view to provide information about itself
662+
implementation calls the corresponding {@link
663+
android.view.View#onSaveInstanceState onSaveInstanceState()} method for every {@link
664+
android.view.View} in the layout, which allows each view to provide information about itself
663665
that should be saved. Almost every widget in the Android framework implements this method as
664666
appropriate, such that any visible changes to the UI are automatically saved and restored when your
665667
activity is recreated. For example, the {@link android.widget.EditText} widget saves any text
666668
entered by the user and the {@link android.widget.CheckBox} widget saves whether it's checked or
667669
not. The only work required by you is to provide a unique ID (with the <a
668670
href="{@docRoot}guide/topics/resources/layout-resource.html#idvalue">{@code android:id}</a>
669-
attribute) for each widget you want to save its state. If a widget does not have an ID, then it
670-
cannot save its state.</p>
671+
attribute) for each widget you want to save its state. If a widget does not have an ID, then the
672+
system cannot save its state.</p>
671673

672674
<div class="sidebox-wrapper">
673675
<div class="sidebox">
@@ -689,7 +691,9 @@ restored, by default).</p>
689691
android.app.Activity#onSaveInstanceState onSaveInstanceState()} helps save the state of the UI, if
690692
you override the method in order to save additional state information, you should always call the
691693
superclass implementation of {@link android.app.Activity#onSaveInstanceState onSaveInstanceState()}
692-
before doing any work.</p>
694+
before doing any work. Likewise, you should also call the supercall implementation of {@link
695+
android.app.Activity#onRestoreInstanceState onRestoreInstanceState()} if you override it, so the
696+
default implementation can restore view states.</p>
693697

694698
<p class="note"><strong>Note:</strong> Because {@link android.app.Activity#onSaveInstanceState
695699
onSaveInstanceState()} is not guaranteed
@@ -701,30 +705,33 @@ to a database) when the user leaves the activity.</p>
701705
<p>A good way to test your application's ability to restore its state is to simply rotate the
702706
device so that the screen orientation changes. When the screen orientation changes, the system
703707
destroys and recreates the activity in order to apply alternative resources that might be available
704-
for the new orientation. For this reason alone, it's very important that your activity
708+
for the new screen configuration. For this reason alone, it's very important that your activity
705709
completely restores its state when it is recreated, because users regularly rotate the screen while
706710
using applications.</p>
707711

708712

709713
<h3 id="ConfigurationChanges">Handling configuration changes</h3>
710714

711715
<p>Some device configurations can change during runtime (such as screen orientation, keyboard
712-
availability, and language). When such a change occurs, Android restarts the running Activity
713-
({@link android.app.Activity#onDestroy} is called, followed immediately by {@link
714-
android.app.Activity#onCreate onCreate()}). The restart behavior is
716+
availability, and language). When such a change occurs, Android recreates the running activity
717+
(the system calls {@link android.app.Activity#onDestroy}, then immediately calls {@link
718+
android.app.Activity#onCreate onCreate()}). This behavior is
715719
designed to help your application adapt to new configurations by automatically reloading your
716-
application with alternative resources that you've provided. If you design your activity to
717-
properly handle this event, it will be more resilient to unexpected events in the activity
718-
lifecycle.</p>
720+
application with alternative resources that you've provided (such as different layouts for
721+
different screen orientations and sizes).</p>
722+
723+
<p>If you properly design your activity to handle a restart due to a screen orientation change and
724+
restore the activity state as described above, your application will be more resilient to other
725+
unexpected events in the activity lifecycle.</p>
719726

720-
<p>The best way to handle a configuration change, such as a change in the screen orientation, is
721-
to simply preserve the state of your application using {@link
727+
<p>The best way to handle such a restart is
728+
to save and restore the state of your activity using {@link
722729
android.app.Activity#onSaveInstanceState onSaveInstanceState()} and {@link
723730
android.app.Activity#onRestoreInstanceState onRestoreInstanceState()} (or {@link
724731
android.app.Activity#onCreate onCreate()}), as discussed in the previous section.</p>
725732

726-
<p>For a detailed discussion about configuration changes that happen at runtime and how you should
727-
handle them, read <a href="{@docRoot}guide/topics/resources/runtime-changes.html">Handling
733+
<p>For more information about configuration changes that happen at runtime and how you can handle
734+
them, read the guide to <a href="{@docRoot}guide/topics/resources/runtime-changes.html">Handling
728735
Runtime Changes</a>.</p>
729736

730737

docs/html/guide/topics/fundamentals/fragments.jd

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ parent.link=activities.html
3333
</li>
3434
<li><a href="#Lifecycle">Handling the Fragment Lifecycle</a>
3535
<ol>
36-
<li><a href="#CoordinadingWithActivity">Coordinating with the activity lifecycle</a></li>
36+
<li><a href="#CoordinatingWithActivity">Coordinating with the activity lifecycle</a></li>
3737
</ol>
3838
</li>
3939
<li><a href="#Example">Example</a></li>
@@ -143,7 +143,7 @@ href="{@docRoot}guide/practices/tablets-and-handsets.html">Supporting Tablets an
143143

144144
<h2 id="Creating">Creating a Fragment</h2>
145145

146-
<div class="figure" style="width:314px">
146+
<div class="figure" style="width:327px">
147147
<img src="{@docRoot}images/fragment_lifecycle.png" alt="" />
148148
<p class="img-caption"><strong>Figure 2.</strong> The lifecycle of a fragment (while its
149149
activity is running).</p>
@@ -657,7 +657,7 @@ href="{@docRoot}guide/topics/ui/actionbar.html">Action Bar</a> developer guides.
657657

658658
<h2 id="Lifecycle">Handling the Fragment Lifecycle</h2>
659659

660-
<div class="figure" style="width:403px">
660+
<div class="figure" style="width:350px">
661661
<img src="{@docRoot}images/activity_fragment_lifecycle.png" alt=""/>
662662
<p class="img-caption"><strong>Figure 3.</strong> The activity lifecycle's affect on the fragment
663663
lifecycle.</p>

0 commit comments

Comments
 (0)