Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 30

Android Ap

plication
Introductio
n Component
s
Topics to be covered
0 Introduction.
0 Android Activity life cycle.
0 What is Android Intent?
Introduction
0 Android is designed with the maximum amount of modularity in
mind. This modularity makes it easy for developers to exchange
functionality between their applications, which is a central concept
of the open source paradigm upon which Android is based.
0 For instance, if you have coded a cool animated UI element, and you
make this available to other applications, they can implement your
class and use that element. And you do not need to have that code
inside those other applications.

0 As long as the application that contains the element’s code is


running (or can be launched) on the Android smartphone, you can
call the method via the Android operating system.
Introduction cont…
0 There are four main types of components that can be (but
do not need to be) used within an Android application:

0 Activities handle the UI to the smartphone screen.


0 Services handle background processing.
0 Broadcast receivers handle communication in your apps.
0 Content providers handle data and database management
issues.

0 Let’s take a closer at each of these components


Intents

0 which are messaging objects that carry communications


between the major components of your application—your
activities, services, and broadcast receivers, which handle
Android messaging.
0 We have seen that Android development is highly
modularized, and intents provide a way to wire these
modules together to form a cohesive yet flexible application
with secure, fluid communication among all of its
components.
Intents cont…
0 provides a description of some sort of standard
operating system or developer created “action” that
needs to be performed and passes the data which that
action needs to operate on to the code receiving the
intent.
0 the Intent object can also contain relevant data
needed to complete that action, as well as data type
specifications, constants, flags, and even extra data
related to the data needed by the action. Because
Intents cont…
0 Intent Resolution: Implicit Intents & Explicit Intents
0 There are two broad categories of intents—explicit intents and
implicit intents.
Explicit Intents
0 use the component portion of the Intent object via the
ComponentName data field. You’ll generally use these when
working with applications you have developed, as you’ll know
which packages and classes are appropriate for the Intent
object to send an action message and data to be acted on.
0 Because the component is specified explicitly, this type of
intent is also safer as there is zero room for error in
interpretation.
Intents cont…
0 There are two ways to specify a component.
0 .setComponent(ComponentName);
0 setClass(Context, Class) method to provide the exact class to use to
process the intent.
Implicit Intents
0 Implicit intents are those that don’t specify the component within
the intent object.
0 Android does this inference based on a comparison of the various
actions, data, and categories defined in the intent object with the
code components that are available to process the intent.
0 This is usually done via intent filters that are defined in the
AndroidManifest.xml file.
Android Activity
0 An activity represents a single screen with a user interface just like 
window orframe . Java.Android activity is the subclass of ContextT
hemeWrapper class.

0 With C, C++ or Java programming language must have seen  that 


your  program  starts  from  main()  function.  Very  similar  way, 
Androidsystem initiates its program with in an Activity starting wit
h a call on onCreate() callback method. 
Android Activity cont…
0 An Android activity contains a UI construct that accomplishes a given user-
input task via the smartphone display screen. Android applications can
have more than one activity. In fact, more complex applications usually
have one activity for each UI screen implementation.
0 For example, if you are programming a game, you might have the following
activities:
0 The introductory splash screen with the Continue to Play Game OR Press
Button to Play Game
0 The instructions screen, with a scrolling text UI
0 The high-score screen, with UI elements that allow the user to manage high-
score entries
0 A player groups screen, where users choose who will play the game with them
0 The actual core gameplay screen itself
Android Activity cont…
Android Activity Lifecycle
0 Android  Activity  Lifecycle  is controlled  by  7  methods  of
android.app.Activity  class.  They are also called callback
methods. The android Activity is the subclass
of ContextThemeWrapper class.
0 Callback & Description
onCreate()
0 This is the first callback and called when the activity is first created.
onStart()
0
This callback is called when the activity becomes visible to the user.
Android Activity cont…
onResume()
0 This is called when the user starts interacting with the application.
onPause()
0 The paused activity does not receive user input and cannot execute any
code and called when the current activity is being paused and the previou
s activity is being resumed.
onStop()
0 This callback is called when the activity is no longer visible
onDestroy()
0 This callback is called before the activity is destroyed by the system
onRestart()
This callback is called when the activity restarts after stopping it.
Android Activity cont…
Launching Activities and Subactivities
0 One key question you need to answer when you decide to
launch an activity is this: does your activity need to know
when the launched activity ends?
0 to launch the child as subordinate to the launching activity you
probably want to launch the child as a subactivity, which means
your activity will be notified when the child activity is complete
0 to launch the child just as a regular activity your activity will not
be informed when the child is done, but, then again, your activity
really does not need to know.
Android Activity cont…
o The two pieces for starting an activity are an intent and your
choice of how to start it up.
Make An Intent
o If the activity you intend to launch is one of your own, you may
find it simplest to create an explicit intent, naming the component
you wish to launch.
new Intent(this, HelpActivity.class);
o This stipulates that you want to launch the HelpActivity.
o This activity would need to be named in your
AndroidManifest.xml file, though not necessarily with any intent
filter, since you are trying to request it directly.
Android Activity example2
0 ActivityDemo/rec/layout/activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="@string/hello_world" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/textView1"
android:layout_marginTop="47dp"
android:text="@string/next" />

<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/textView1"
android:layout_alignRight="@+id/button1"
android:layout_marginBottom="100dp"
android:text="@string/firstActivity" />

</RelativeLayout>
0ActivityDemo/rec/layout/activity_activity2_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Activity2" >

<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="@string/hello_world" />

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/textView2"
android:layout_alignLeft="@+id/textView2"
android:layout_marginBottom="85dp"
android:text="@string/secondActivity" />

</RelativeLayout>
0 ActivityDemo/res/values//strings.xml

<?xml version="1.0" encoding="utf-8"?>


<resources>

<string name="app_name">ActivityDemo</string>
<string name="hello_world">Hello world!</string>
<string name="menu_settings">Settings</string>
<string name="title_activity_activity4">Activity4</string>
<string name="next">next</string>
<string name="firstActivity">FirstActivity</string>
<string name="secondActivity">secondActivity</string>
</resources>
ActivityDemo/srs/com/example/activitydemo/MainActivity.java

package com.example.activitydemo;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

Button Next = (Button) findViewById(R.id.button1) ;


Next.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
Intent i = new Intent(getApplicationContext(),Activity4.class);
i.putExtra("requestCode1", "Message From MainAcivity");
startActivity(i);

}
});
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}

}
0 ActivityDemo/srs/com/example/activitydemo/Activity2.java

package com.example.activitydemo;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.widget.Toast;

public class Activity2 extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_activity4);
Bundle extras =getIntent().getExtras();
String v1= extras.getString("requestCode1");
Toast.makeText(getApplicationContext(), "the message passed is:"+v1,
Toast.LENGTH_LONG).show();
Intent i =new Intent(getApplicationContext(),MainActivity.class);
//startActivity(i);

}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_activity4, menu);
return true;
}
Android Activity example 2
0 AndroidActivityDemo/rec/layout/activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="84dp"
android:layout_marginTop="35dp"
android:text="@string/hello_world" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/textView1"
android:layout_marginTop="26dp"
android:text="@string/NextActivity" /> </RelativeLayout>
0AndroidActivityDemo/rec/layout/activity_socond_activity.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".SecondAcivity" >

<Button
android:id="@+id/sendMessage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/setMessage"
android:layout_centerHorizontal="true"
android:layout_marginTop="40dp"
android:text="@string/SendMessage" />

<EditText
android:id="@+id/setMessage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="144dp"
android:ems="10" >

<requestFocus />
</EditText>

</RelativeLayout>
AndroidActivityDemo/res/values//strings.xml
?xml version="1.0" encoding="utf-8"?>
<resources>

<string name="app_name">AndroidActivityDemo</string>
<string name="hello_world">Hello world!</string>
<string name="menu_settings">Settings</string>
<string
name="title_activity_second_acivity">SecondAcivity</string>
<string name="NextActivity">Get Message</string>
<string name="SendMessage">SendMessage</string>
</resources>
AndroidActivityDemo/srs/com/example/androidactivitydemo/MainActivity.j
ava

package com.example.androidactivitydemo;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {


TextView tv;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn =(Button)findViewById(R.id.button1);
tv =(TextView)findViewById(R.id.textView1);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
Intent i = new Intent(getApplicationContext(),SecondAcivity.class);
startActivityForResult(i,3);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode,
Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);

if(requestCode==3)
{
String Message= data.getStringExtra("MyMessage");
tv.setText(Message);
}
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}}

0 AndroidActivityDemo/srs/com/example/androidactivitydemo
/SecondActivity.java

package com.example.androidactivitydemo;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class SecondAcivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second_acivity);
Button btn1 =(Button)findViewById(R.id.sendMessage);
final EditText et =(EditText)findViewById(R.id.setMessage);
btn1.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View arg0) {
String message=et.getText().toString();
Intent i = new Intent();
i.putExtra("MyMessage", message);
setResult(3,i);
finish();
}
});
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_second_acivity, menu);
return true;
If you correctly create AVD and write the above code the
above code will look like the following on emulator.

You might also like