Professional Documents
Culture Documents
Android - Activity and Intent Finished
Android - Activity and Intent Finished
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.
<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
<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;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
});
}
@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;
@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;
@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.