Download presentation
Published byRosamond Williamson Modified over 9 years ago
1
Chien-Chung Shen cshen@udel.edu
Manifest and Activity Chien-Chung Shen
2
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)
3
Android Manifest (2) Application Component
<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
4
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 > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity>
5
Example (1) <manifest xmlns: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>
6
Example (2) <application android:allowBackup="true" > <activity android:name=".MainActivity367" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application>
7
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
8
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
9
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)
10
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
11
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"); }
12
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
13
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
14
“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
15
“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
16
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" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity>
17
MainActivity367 public class MainActivity367 extends Activity 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); } ……
18
Start Another Activity
In activity_a.xml <Button android:layout_height="wrap_content" android:layout_width="wrap_content" android:onClick="startActivityB" /> In ActivityA.java public void startActivityB(View v) { Intent intent = new Intent(ActivityA.this, ActivityB.class); startActivity(intent); }
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.