• Aucun résultat trouvé

Restore Your Activity State

Dans le document 2. Building Your First App (Page 70-73)

static final String STATE_SCORE = "playerScore";

static final String STATE_LEVEL = "playerLevel";

...

@Override

public void onSaveInstanceState(Bundle savedInstanceState) { // Save the user's current game state

savedInstanceState.putInt(STATE_SCORE, mCurrentScore);

savedInstanceState.putInt(STATE_LEVEL, mCurrentLevel);

// Always call the superclass so it can save the view hierarchy state super.onSaveInstanceState(savedInstanceState);

}

Caution: Always call the superclass implementation of onSaveInstanceState() so the default implementation can save the state of the view hierarchy.

Restore Your Activity State

When your activity is recreated after it was previously destroyed, you can recover your saved state from the Bundle that the system passes your activity. Both the onCreate() and

onRestoreInstanceState() callback methods receive the same Bundle that contains the instance state information.

Because the onCreate() method is called whether the system is creating a new instance of your activity or recreating a previous one, you must check whether the state Bundle is null before you attempt to read it. If it is null, then the system is creating a new instance of the activity, instead of restoring a previous one that was destroyed.

For example, here's how you can restore some state data in onCreate():

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState); // Always call the superclass first

// Check whether we're recreating a previously destroyed instance if (savedInstanceState != null) {

// Restore value of members from saved state

mCurrentScore = savedInstanceState.getInt(STATE_SCORE);

mCurrentLevel = savedInstanceState.getInt(STATE_LEVEL);

} else {

// Probably initialize members with default values for a new instance }

...

}

Instead of restoring the state during onCreate() you may choose to implement

onRestoreInstanceState(), which the system calls after the onStart() method. The system calls

Recreating an Activity

71

Content from developer.android.com/training/basics/activity-lifecycle/recreating.html through their Creative Commons Attribution 2.5 license

onRestoreInstanceState() only if there is a saved state to restore, so you do not need to check whether the Bundle is null:

public void onRestoreInstanceState(Bundle savedInstanceState) { // Always call the superclass so it can restore the view hierarchy super.onRestoreInstanceState(savedInstanceState);

// Restore state members from saved instance

mCurrentScore = savedInstanceState.getInt(STATE_SCORE);

mCurrentLevel = savedInstanceState.getInt(STATE_LEVEL);

}

Caution: Always call the superclass implementation of onRestoreInstanceState() so the default implementation can restore the state of the view hierarchy.

To learn more about recreating your activity due to a restart event at runtime (such as when the screen rotates), read Handling Runtime Changes.

Building a Dynamic UI with Fragments

72

Content from developer.android.com/training/basics/fragments/index.html through their Creative Commons Attribution 2.5 license

21. Building a Dynamic UI with Fragments

Content from developer.android.com/training/basics/fragments/index.html through their Creative Commons Attribution 2.5 license

To create a dynamic and multi-pane user interface on Android, you need to encapsulate UI

components and activity behaviors into modules that you can swap into and out of your activities.

You can create these modules with the Fragment class, which behaves somewhat like a nested activity that can define its own layout and manage its own lifecycle.

When a fragment specifies its own layout, it can be configured in different combinations with other fragments inside an activity to modify your layout configuration for different screen sizes (a small screen might show one fragment at a time, but a large screen can show two or more).

This class shows you how to create a dynamic user experience with fragments and optimize your app's user experience for devices with different screen sizes, all while continuing to support devices running versions as old as Android 1.6.

Lessons

Creating a Fragment

Learn how to build a fragment and implement basic behaviors within its callback methods.

Building a Flexible UI

Learn how to build your app with layouts that provide different fragment configurations for different screens.

Communicating with Other Fragments

Learn how to set up communication paths from a fragment to the activity and other fragments.

Dependencies and prerequisites

• Basic knowledge of the Activity lifecycle (see Managing the Activity Lifecycle)

• Experience building XML layouts You should also read

Fragments

• Supporting Tablets and Handsets Try it out

Download the sample FragmentBasics.zip

Creating a Fragment

73

Content from developer.android.com/training/basics/fragments/creating.html through their Creative Commons Attribution 2.5 license

22. Creating a Fragment

Content from developer.android.com/training/basics/fragments/creating.html through their Creative Commons Attribution 2.5 license

You can think of a fragment as a modular section of an activity, which has its own lifecycle, receives its own input events, and which you can add or remove while the activity is running (sort of like a

"sub activity" that you can reuse in different activities). This lesson shows how to extend the Fragment class using the Support Library so your app remains compatible with devices running system versions as low as Android 1.6.

Note: If you decide that the minimum API level your app requires is 11 or higher, you don't need to use the Support Library and can instead use the framework's built in Fragment class and related APIs. Just be aware that this lesson is

focused on using the APIs from the Support Library, which use a specific package signature and sometimes slightly different API names than the versions included in the platform.

Before you begin this lesson, you must set up your Android project to use the Support Library. If you have not used the Support Library before, set up your project to use the v4 library by following the Support Library Setup document. However, you can also include the action bar in your activities by instead using the v7 appcompat library, which is compatible with Android 2.1 (API level 7) and also includes the Fragment APIs.

Dans le document 2. Building Your First App (Page 70-73)