0% found this document useful (0 votes)
9 views68 pages

Mad (Unit Ii)

Mad book

Uploaded by

j88420838
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
9 views68 pages

Mad (Unit Ii)

Mad book

Uploaded by

j88420838
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 68
Mobile Application Development UNIT «2 2 Activities 21 Understanding Activities An activity signifies a single screen in your app with an interface so that the user can interact with. For example, an email app might have one activity tha shows a list of unread emails, another activity to create a new email, another activity for reading new messages and another for deleting the unwanted spam that you either create yourself, and mails. So, an app is a collection of activiti or that you reuse from other apps. Although the activities in your app work together to form a pleasant user experience in your app, each one is independent of the others. This enables your app to start activities in other apps, and other apps can start your activities For example, a whatsapp lets you to start an activity in a camera app to take® picture, and then start the activity in an email app to let the user share that picture in email ee Mobile Application Development Typically, one activity in an app is specified as the "main" activity, which is presented to the user when launching the application for the first time. Each activity can then start other activities in order to perform different actions. Whenever a new activity starts, the previous activity is stopped, but the system | stores the activity in the back end stack . When the user is done with the current activity and presses the Back button. it is popped from the stack (and destroyed) and the previous activity resumes. When an activity is stopped because a new activity starts, the first activity is notified of that change with the activity's lifecycle callback methods. Activity Lifecycle: The Activity lifecycle is the set of states an activity can be in, from when it is first created, to each time it is stopped or resumed, to when the system destroys it. 2.1.1 Creating activities To implement an activity in your app, do the following: e Create an activity Java class. ¢ Implement a user interface for that activity. © Declare that new activity in the app manifest. When you create a new project for your app, or add a new activity to your app. in Android Studio (with File > New > Activity), a predefined template with Code is given for the users. Create the activity class Activities are subclasses of the Activity class, or one of its subclasses. When you create a new project in Android Studio, your activities are, by default, Subclasses of the AppCompatActivity class. The AppCompatActivity class is 81 Mobile Application Development a subclass of Activity that lets you to use up-to-date Android app Features sy as the action bar and material design, while still enabling your ADP toby compatible with devices running older versions of Android. The following is the code for subclass of AppCompatA tivity: public class MainActivity extends AppCompatactivity @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState) ; setContentView(R. layout.activity_main); e The following methods are implemented in activity sub class. |.onCreate() This is the first callback and called when the activity is first created. This is must to create the activity. 2.onStart() This callback is called when the activity becomes visible to the user 3.onResume() This is called when the user starts interacting with the application. ed activity does not receive user input and cannot execute 4.onPause() The pe any code and called when the current activity is being paused and the previous activity is being resumed. 1 visible S.onStop() This callback is called when the activity is no longe! by the onDestroy() This callback is called before the activity is destroyed system. : , ing it 6.onRestart() This callback is called when the activity restarts after stoppin You typically define the user interface for your activity in one or more = a layout files. When the setContentView() method is called with the path (© — 82 Mobile Application Development layout file, the system creates all the initial views from the specified layout and adds them to your activity. Some codes are implemented to handle button and input movements. 2.1.2 Lifecycle of an activity And its working model Example: program to demonstrate the life cycle methods of an activity import android. app.Activity; import android.os.Bundle; 83 Mobile Application Development import android.uti 1. Log; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super -onCreate( savedInstanceState) ; setContentView(R. layout. activity_main); Log.d("lifecycle”, "onCreate invoked”); } @override protected void onStart() { super.onStart(); Log.d("lifecycle”, “onStart invoked"); yi @override protected void onResume() { super. onResume() ; Log.d("lifecycle”, "onResume invoked"); } @Override protected void onPause() { super .onPause(); Log.d("lifecycle", "onPause invoked”); t @Override protected void onStop() { super .onStop(); Log.d("lifecycle", “onStop invoked"); } @Override 84 — Mobile Application Development protected void onRestart() { super.onRestart(); Log.d("lifecycle", "onRestart invoked"); } @Override protected void onDestroy() { super.onDestroy(); log.d("lifecycle", "onDestroy invoked"); 2.1.3 Implement a user interface The user interface for an activity is provided by a hierarchy of views, which controls a particular space within the activity's window and can respond to user interaction. User interface is generally designed using views is with an XML. layout file stored as part of app's resources, Defining the layout in XML enables you to maintain the design of your user interface separately from the source code that defines the activity's behavior. You can also create new views directly in your activity code by inserting new view objects into a ViewGroup, and then passing the root ViewGroup to setContentView(). Any number of views can be created using the user interface 2.1.4 Declare the activity in the manifest Each activity in your app must be declared in the Android app manifest with the element, inside . When you create a new project oradd a new activity to your project in Android Studio, your manifest is created °F updated to include skeleton activity declarations for each activity. Here's the declaration for the main activity. 85 Mobile Application Development The element includes a number of attributes to define properties of the activity such as its label, icon, or theme. The only required attribute is android:name, which specifies the class name for the activity such a "MainActivity” 2.2 Linking activities using Intents The element can also include declarations for intent filters. The intent filters specify the kind of intents your activity will accept. include Intent filters must include at least one element, and can also i Phe main activity for your app needs a? a and optional . intent filter that defines the "main" action and the "launcher" category 80 Al the system can launch your app. Android Studio creates this intent filter for the main activity in your project The element specifies that this is the "main" entry point © the that this activity should be usted h this activid? a application. The element specifi in the system's application launcher (to allow users to launch RO Mobile Application Development Other activities in your app can also declare intent filters, but only your main activity should include the "main" action, Add more activities to your project ‘The main activity for your app and ity associated layout file comes with your project when you create it, You can add new activities to your project in Android Studio with the Vile » New » Activity menu, Choose the activity template you want to use, or open the Gallery to see all the available templates, When you choose an activity template, you'll see the same set of screens for creating the new activity that you did when you initially created the project. Android Studio provides these three things for each new activity in your app: A Java file for the new activity with a skeleton class definition and onCreate() method, The new activity, like the main activity, isa subelass of AppCompatActivity, An XML file containing the layout for the new activity. Note that the setContentViewQ) method in the activity class inflates thin new layout, An additional “activity clement in the Android manifest that specifies the new activity, The second activity definition does not include any intent filters. If you intend to use this activity only within your app (and not enable that activity to be started by any other app), you do not need to add filters. a?

You might also like