一 : FirstActivity.java
package com.example.activitytest;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.Toast;
import android.view.View.OnClickListener;
public class FirstActivity extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.first_layout);
Button button1 = (Button) findViewById(R.id.button_1);
//****test1
// button1.setOnClickListener(new OnClickListener(){
// @Override
// public void onClick(View v){
// Toast.makeText(FirstActivity.this,"You clicked Button 1", Toast.LENGTH_SHORT).show();
// button1.setOnClickListener(new OnClickListener(){
// @Override
//****顯式Intent
// public void onClick(View v){
// Intent intent = new Intent(FirstActivity.this,SecondActivity.class);
// startActivity(intent);
//****隱式Intent
// button1.setOnClickListener(new OnClickListener(){
// @Override
// public void onClick(View v){
// Intent intent =new Intent("com.example.activitytest.ACTION_START");
// startActivity(intent);
button1.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v){
// Intent intent =new Intent("com.example.activitytest.ACTION_START");
// intent.addCategory("com.example.activitytest.MY_CATEGORY");
// startActivity(intent);
//***調用瀏覽器
// Intent intent =new Intent(Intent.ACTION_VIEW);
// intent.setData(Uri.parse("http://www.baidu.com"));
// startActivity(intent);
//***調用系統撥號
// Intent intent =new Intent(Intent.ACTION_DIAL);
// intent.setData(Uri.parse("tel:10010"));
// startActivity(intent);
//***向下一活動傳遞數據
// String data ="Hello SecondActivity";
// Intent intent=new Intent(FirstActivity.this,SecondActivity.class);
// intent.putExtra("extra_data", data);
// startActivity(intent);
//***返回數據給上一個活動
Intent intent=new Intent(FirstActivity.this,SecondActivity.class);
startActivityForResult(intent,1);
}
});
}
@Override
protected void onActivityResult(int requestCode,int resultCode,Intent data){
switch(requestCode){
case 1:
if (resultCode==RESULT_OK){
String returnedData =data.getStringExtra("data_return");
Log.d("FirstActivity",returnedData);
}
break;
default:
}
}
public boolean onCreateOptionsMenu(Menu menu){
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item){
switch (item.getItemId()){
case R.id.add_item:
Toast.makeText(this, "You clicked Add", Toast.LENGTH_SHORT).show();
break;
case R.id.remove_item:
Toast.makeText(this, "You clicked Remove", Toast.LENGTH_SHORT).show();
break;
default:
}
return true;
}
}
二 : SecondActivity.java
package com.example.activitytest;
import android.app.Activity;
import android.os.Bundle;
import android.view.Window;
public class SecondActivity extends Activity{
@Override
protected void onCreate(Bundle saveInstanceState){
super.onCreate(saveInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.second_layout);
//***向下一活動傳遞數據
// Intent intent =getIntent();
// String data = intent.getStringExtra("extra_data");
// Log.d("SecondActivity",data);
//***返回數據給上一個活動
Button button2 =(Button) findViewById(R.id.button_2);
button2.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v){
Intent intent = new Intent();
intent.putExtra("data_return","Hello FirstActivity");
setResult(RESULT_OK,intent);
finish();
}
});
}
@Override
public void onBackPressed(){
Intent intent =new Intent();
intent.putExtra("data_return", "Hello FirstActivity");
setResult(RESULT_OK, intent);
finish();
}
}
三 : ThirdActivity.java
package com.example.activitytest;
import android.app.Activity;
import android.os.Bundle;
import android.view.Window;
public class ThirdActivity extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.third_layout);
}
}
四 : BaseActivity
package com.example.activitytest;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
public class BaseActivity extends Activity{
@Override
protected void onCreate(Bundle saveInstanceState){
super.onCreate(saveInstanceState);
Log.d("BaseActivity",getClass().getSimpleName());
ActivityCollector.addActivity(this);
}
@Override
protected void onDestroy(){
super.onDestroy();
ActivityCollector.removeActivity(this);
}
}
五 :ActivityCollector
package com.example.activitytest;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
public class ActivityCollector {
public static List<Activity> activities = new ArrayList<Activity>();
public static void addActivity(Activity activity){
activities.add(activity);
}
public static void removeActivity(Activity activity){
activities.remove(activity);
}
public static void finishAll(){
for(Activity activity:activities){
if (!activity.isFinishing()){
activity.finish();
}
}
}
}
六 : AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.activitytest"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="@drawable/logo"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".FirstActivity"
android:label="This is my first activity">
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".SecondActivity">
<intent-filter>
<action android:name="com.example.activitytest.ACTION_START"/>
<category android:name="android.intent.category.DEFAULT"/>
<category android:name="com.example.activitytest.MY_CATEGORY"/>
</intent-filter>
</activity>
<activity android:name=".ThirdActivity">
<intent-filter>
<action android:name="android.intent.action.VIEW"/>
<category android:name="android.intent.category.DEFAULT"/>
<data android:scheme="http"/>
</intent-filter>
</activity>
</application>
</manifest>
七 : first_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@+id/button_1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button 1"
/>
</LinearLayout>
八: second_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@+id/button_2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button 2"/>
</LinearLayout>
九 : third_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@+id/button_3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button 3"
/>
</LinearLayout>
menu/main.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/add_item"
android:title="Add"/>
<item
android:id="@+id/remove_item"
android:title="Remove"/>
</menu>
完。