Thứ Sáu, 25 tháng 4, 2014

Understanding Activities [Activities, Fragments, And Intents]

The best way to understand the various stages of an activity is to create a new project,  implement the various events, and then subject the activity to various user interactions.
1.     Using Eclipse, create a new Android  project and name it Activity101.
2.     In the Activity101Activity.java file, add the following statements in bold:

package  net.learn2develop.Activity101;

import android.app.Activity; import android.os.Bundle; import android.util.Log;

public class  Activity101Activity  extends Activity  {
String tag =  “Lifecycle”;

/** Called when  the activity is first  created. */
@Override
public  void onCreate(Bundle savedInstanceState)  { super.onCreate(savedInstanceState); setContentView(R.layout.main);
Log.d(tag,  “In   the onCreate() event”);
}

public void onStart()
{
super.onStart();
Log.d(tag,  “In   the onStart()  event”);
}

public void onRestart()
{
super.onRestart();
Log.d(tag,  “In   the onRestart() event”);
}

public  void  onResume()
{
super.onResume();
Log.d(tag,  “In   the  onResume() event”);
}

public  void onPause()
{
super.onPause();
Log.d(tag,  “In   the  onPause() event”);
}

public  void onStop()
{
super.onStop();
Log.d(tag,  “In   the onStop() event”);
}

public  void onDestroy()
{
super.onDestroy();
Log.d(tag,  “In   the onDestroy() event”);
}
}

3.           Press F11 to debug the application on the Android  emulator.
4.           When the activity is first loaded,  you should see something  very similar to the following in the LogCat window  (click the Debug perspective; see also Figure 2-2):

11-16 06:25:59.396: D/Lifecycle(559):  In the onCreate() event 11-16 06:25:59.396: D/Lifecycle(559): In the onStart()  event 11-16 06:25:59.396: D/Lifecycle(559):  In the onResume() event


FIGURE 2-2

5.         If you click the Back button on the Android  emulator, the following is printed:

11-16 06:29:26.665: D/Lifecycle(559):  In the  onPause() event 11-16 06:29:28.465: D/Lifecycle(559):  In the onStop() event
11-16 06:29:28.465: D/Lifecycle(559):  In the onDestroy() event
6.     Click the Home button and hold it there. Click the Activities icon and observe the following:

11-16 06:31:08.905: D/Lifecycle(559):  In the onCreate() event 11-16 06:31:08.905: D/Lifecycle(559): In the onStart()  event 11-16 06:31:08.925: D/Lifecycle(559):  In the onResume() event

7.     Click the Phone button on the Android  emulator so that the activity is pushed to the background.
Observe the output in the LogCat window:

11-16 06:32:00.585: D/Lifecycle(559):  In the  onPause() event 11-16 06:32:05.015: D/Lifecycle(559):  In the onStop() event

8.     Notice that the onDestroy() event is not called, indicating  that the activity is still in memory.
Exit the phone dialer by clicking the Back button. The activity is now visible again. Observe the output in the LogCat window:

11-16 06:32:50.515: D/Lifecycle(559): In the onRestart()  event 11-16 06:32:50.515: D/Lifecycle(559): In the onStart()  event
11-16 06:32:50.515: D/Lifecycle(559):  In the onResume() event

The onRestart() event is now fired, followed by the onStart() and onResume() methods.

How It Works

As you can see from this simple example,  an activity is destroyed  when you click the Back button. This is crucial to know,  as whatever  state the activity is currently  in will be lost; hence, you need to write additional code in youractivity to preserve its state when it is destroyed  (Chapter 3 shows you how). At this point,  note that the onPause() method  is called in both scenarios — when an activity is sent to the background, as well as when it is killed whenthe user presses the Back button.

When an activity is started,  the onStart() and onResume()methods are always called, regardless of whether  the activity is restored  from the background or newly created.  When an activity is created for the first time, the onCreate()method  is called.

From the preceding example,  you can derive the following guidelines:

➤   Use the onCreate() method  to create and instantiate the objects that you will be using in your application.

➤   Use the onResume() method  to start any services or code that needs to run while your activity is in the foreground.

➤   Use the onPause() method  to stop any services or code that does not need to run when your activity is not in the foreground.
➤   Use the onDestroy() method  to free up resources before your activity is destroyed.

Applying Styles and Themes to an Activity

By default,  an activity occupies the entire screen. However,  you can apply a dialog theme to
an activity so that it is displayed as a floating dialog. For example, you might want to customize your activity to display as a pop-up,  warning  users about  some actions that they are going to perform.  In this case, displaying theactivity as a dialog is a good way to get their attention.

To apply a dialog theme to an activity, simply modify the <Activity> element in the
AndroidManifest.xml file by adding the android:theme attribute:

<?xml   version=“1.0“ encoding=“utf-8“?>
package=”net.learn2develop.Activity101” android:versionCode=”1” android:versionName=”1.0”  >

<uses-sdk  android:minSdkVersion=”14” />

<application android:icon=”@drawable/ic_launcher” android:label=”@string/app_name” android:theme=”@android:style/Theme.Dialog”>
<activity android:label=”@string/app_name” android:name=”.Activity101Activity” >
<intent-filter >
<action android:name=”android.intent.action.MAIN” />

<category  android:name=”android.intent.category.LAUNCHER” />
</intent-filter>
</activity>
</application>

</manifest>
This will make the activity appear  as a dialog, as shown in Figure 2-3.


 FIGURE 2-3

Hiding the Activity Title

You can also hide the title of an activity if desired (such as when you just want to display a status update  to the user). To do so, use the requestWindowFeature() method  and pass it the Window
.FEATURE_NO_TITLE constant, like this:

import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.view.Window;

public class  Activity101Activity extends Activity  {        
 
String tag =  “Lifecycle”;

/** Called when  the activity is first  created. */
@Override
public  void onCreate(Bundle savedInstanceState)  {
super.onCreate(savedInstanceState);
//---hides the title  bar---
requestWindowFeature(Window.FEATURE_NO_TITLE);

setContentView(R.layout.main); Log.d(tag,  “In the onCreate() event”);
}
}

This will hide the title bar, as shown in Figure 2-4.


FIGURE 2-4

Không có nhận xét nào:

Đăng nhận xét