Manifest and Activity Chien-Chung Shen [email protected].

18
Manifest and Activity Chien-Chung Shen [email protected]

Transcript of Manifest and Activity Chien-Chung Shen [email protected].

Page 1: Manifest and Activity Chien-Chung Shen cshen@udel.edu.

Manifest and Activity

Chien-Chung [email protected]

Page 2: Manifest and Activity Chien-Chung Shen cshen@udel.edu.

Android Manifest (1)

• Configure Android application– describe components and settings of an Android app

in AndroidManifest.xml– specify additional metadata for the application, e.g.,

icons and version number – read by Android system during installation of the app

• Declare components in the manifest file – All activities, services and content provider components

of the app must be statically declared in this file• Specify Permissions (for network access)

Page 3: Manifest and Activity Chien-Chung Shen cshen@udel.edu.

Android Manifest (2)

• Application– <application> section defines metadata for the app

and is also a container for declaring app’s Android components

• Component– <activity> tag defines an activity component– the name attribute points to class, which (if not fully

qualified) is relative to the package defined in the package attribute

Page 4: Manifest and Activity Chien-Chung Shen cshen@udel.edu.

Android Manifest (3)

• Activity– intent filter tells Android runtime that this activity

should be registered as a possible entry point into the app and made available in the launcher of Android system

• <action android:name="android.intent.action.MAIN” /> defines that it can be started

• <category android:name="android.intent.category.LAUNCHER” /> tells Android system to add the activity to the launcher

<activity android:name=".MainActivity367” // point to the Java class android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter></activity>

Page 5: Manifest and Activity Chien-Chung Shen cshen@udel.edu.

Example (1)<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.chienchungshen.android367" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="16" android:targetSdkVersion="19" />

<uses-permission android:name="android.permission.INTERNET" />

…….

</manifest>

Page 6: Manifest and Activity Chien-Chung Shen cshen@udel.edu.

Example (2)<application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".MainActivity367" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity></application>

Page 7: Manifest and Activity Chien-Chung Shen cshen@udel.edu.

Resource ID and R.java• Every resource gets an ID assigned by Android build system• The

./build/generated/source/r/debug/com/example/chienchungshen/Android367/app directory contains the R.java references file which contains these generated static integer values

• If you add a new resource file, the corresponding reference is automatically created in a R.java file. Manual changes in the R.java file are not necessary and will be overwritten by the tooling

• The Android system provides methods to access the corresponding resource files via these IDs – to access a String with the R.string.yourString ID in your source

code, you would use getString(R.string.yourString) method defined on the Context class

Page 8: Manifest and Activity Chien-Chung Shen cshen@udel.edu.

Activity

• An Android component (other components are service, content provider, and broadcast receiver)

• An activity is the visual representation of an Android application

• An Android application can have several activities

• Activities use views and fragments to create the user interface and to interact with the user

Page 9: Manifest and Activity Chien-Chung Shen cshen@udel.edu.

Activity

• As a user navigates through, out of, and back to your app, Activity instances in your app transition between different states in their lifecycle. For instance, – when your activity starts for the first time, it comes to

the foreground of the system and receives user focus– during this process, Android system calls a series of

lifecycle callback methods on the activity in which you set up the user interface and other components

– if the user performs an action that starts another activity or switches to another app, the system calls another set of lifecycle callback methods on your activity as it moves into the background (where the activity is no longer visible, but the instance and its state remains intact)

Page 10: Manifest and Activity Chien-Chung Shen cshen@udel.edu.

Activity

• Within the lifecycle callback methods, you can declare how your activity behaves when the user leaves and re-enters the activity. For example, – if you're building a streaming video player, you might

pause the video and terminate the network connection when the user switches to another app

– when the user returns, you can reconnect to the network and allow the user to resume the video from the same spot

Page 11: Manifest and Activity Chien-Chung Shen cshen@udel.edu.

Start an Activity

• Unlike other programming paradigms in which apps are launched with a main() method, Android system initiates code in an Activity instance by invoking specific callback methods that correspond to specific stages of its lifecycle

Java code:public class HelloWorld { public static void main(String[] args) { System.out.println("Hello, World"); }}

Page 12: Manifest and Activity Chien-Chung Shen cshen@udel.edu.

Lifecycle Callbacks

• During the life of an activity, Android system calls a core set of lifecycle callback methods in a sequence similar to a step pyramid – each stage of the activity lifecycle is a separate step on the pyramid– as the system creates a new activity instance, each callback method

moves the activity state one step toward the top– top of the pyramid is the point at which the activity is running in the

foreground and the user can interact with it

Page 13: Manifest and Activity Chien-Chung Shen cshen@udel.edu.

Lifecycle Callbacks

As the user begins to leave the activity• the system calls other methods that move the activity state back down the

pyramid in order to dismantle the activity• n some cases, the activity will move only part way down the pyramid and

wait (such as when the user switches to another app), from which point the activity can move back to the top (if the user returns to the activity) and resume where the user left off

Page 14: Manifest and Activity Chien-Chung Shen cshen@udel.edu.

“Healthy” Activity Lifecycle

• Does not crash if the user receives a phone call or switches to another app while using your app

• Does not consume valuable system resources when the user is not actively using it

• Does not lose the user's progress if they leave your app and return to it at a later time

• Does not crash or lose the user's progress when the screen rotates between landscape and portrait orientation

Page 15: Manifest and Activity Chien-Chung Shen cshen@udel.edu.

“Static” States• An activity may transition between different states • Only three of these states can be static, i.e., the

activity can exist in one of only three states for an extended period of time – Resumed - the activity is in the foreground and the user can

interact with it (Running state)– Paused - the activity is partially obscured by another activity

and the other activity that's in the foreground is semi-transparent or doesn't cover the entire screen. The paused activity does not receive user input and cannot execute any code

– Stopped - the activity is completely hidden and not visible to the user; it is considered to be in the background. While stopped, the activity instance and all its state information such as member variables is retained, but it cannot execute any code

Page 16: Manifest and Activity Chien-Chung Shen cshen@udel.edu.

Start an Activity

• When user selects your app icon from the Home screen, the system calls the onCreate() method for the Activity in your app that you've declared to be the "launcher" (or "main") activity

• This is the activity that serves as the main entry point to your app's user interface

<activity android:name=".MainActivity367" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity>

Page 17: Manifest and Activity Chien-Chung Shen cshen@udel.edu.

MainActivity367public class MainActivity367 extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);

// Set the user interface layout for this Activity    // The layout file is defined in res/layout/activity_main_activity367.xml setContentView(R.layout.activity_main_activity367); } ……}

Page 18: Manifest and Activity Chien-Chung Shen cshen@udel.edu.

Start Another Activity

In activity_a.xml

<Button android:id="@+id/btn_start_b" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="@string/btn_start_b_label » android:onClick="startActivityB" />

In ActivityA.java

public void startActivityB(View v) { Intent intent = new Intent(ActivityA.this, ActivityB.class); startActivity(intent);}