2013年5月17日 星期五

使用 Intent 跳到其他的Activity 並搭配 startActivityForResult

MainActivity.java
package com.example.basic_intent;

//使用 Intent 跳到其他的Activity
//並使用 startActivityForResult() 來傳送 requestCode
//並設定 onActivityResult() 來接收過濾 requestCode / resultCode
//並在 MainTwo.java 中設定要丟回來的資料到 Intent 中
//最後  MainActivity.java onActivityResult() 接收資料
//要修改 AndroidManifest.xml
//將 Activity 加到啟動程序中

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

public class MainActivity extends Activity {

 final static int KEY_CHAIN = 333;
 final static int KEY_HOLE = 333;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  
  Log.i("生命週期", "1_onCreate");
  
  Button go2 = (Button) this.findViewById(R.id.button1);
  
  go2.setOnClickListener(new View.OnClickListener() {
   
   @Override
   public void onClick(View v) {
    Intent intent = new Intent(getBaseContext(), MainTwo.class);    
    //startActivity(intent);
    startActivityForResult(intent, KEY_CHAIN);
   }
  });
 }
 
 @Override
 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  super.onActivityResult(requestCode, resultCode, data);
  
  Log.i("返回訊息", "回到onActivityResult");
  
  if(requestCode == KEY_CHAIN){
   //if(resultCode == RESULT_OK){
   if(resultCode == KEY_HOLE){
    Log.i("返回訊息", "正常收到KEY_HOLE的資料");
    //取回MainTwo 送回來的資料
    Log.i("返回訊息", data.getExtras().getString("result"));
   }
  }
 }
 
 @Override
 protected void onStop() {
  // TODO Auto-generated method stub
  super.onStop();
  //finish();
  Log.i("生命週期", "1_onStop");
  //Toast.makeText(getBaseContext(), "go2 結束了", Toast.LENGTH_LONG).show();
 }

 @Override
 protected void onPause() {
  // TODO Auto-generated method stub
  super.onPause();
  Log.i("生命週期", "1_onPause");
 }

 @Override
 protected void onRestart() {
  // TODO Auto-generated method stub
  super.onRestart();
  Log.i("生命週期", "1_onRestart");
 }

 @Override
 protected void onResume() {
  // TODO Auto-generated method stub
  super.onResume();
  Log.i("生命週期", "1_onResume");
 }

 @Override
 protected void onStart() {
  // TODO Auto-generated method stub
  super.onStart();
  Log.i("生命週期", "1_onStart");
 }

 @Override
 protected void onDestroy() {
  // TODO Auto-generated method stub
  super.onDestroy();
  Log.i("生命週期", "1_onDestroy");
 }
}

MainTwo.java
package com.example.basic_intent;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
//import android.widget.Toast;

public class MainTwo extends Activity {

 final static int KEY_CHAIN = 333;
 final static int KEY_HOLE = 333; 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  // TODO Auto-generated method stub
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_two);
  
  Log.i("生命週期", "2_onCreate");
  
  Button go1 = (Button) this.findViewById(R.id.button1);
  
  go1.setOnClickListener(new View.OnClickListener() {
   
   @Override
   public void onClick(View v) {
    // TODO Auto-generated method stub
    //也可以只送 resultCode,不送任何資料
    //setResult(KEY_HOLE);
    
    Intent intent = new Intent();
    //intent.setClass(getBaseContext(), MainActivity.class);
    intent.putExtra("result", "我在測試送回去的結果");
    //setResult(RESULT_OK,intent);
    setResult(KEY_HOLE,intent);
    //startActivity(intent);

    MainTwo.this.finish();
   }
  });
 }

 @Override
 protected void onStop() {
  super.onStop();
  //finish();
  Log.i("生命週期", "2_onStop");
  //Toast.makeText(getBaseContext(), "go2 結束了", Toast.LENGTH_LONG).show();
 }

 @Override
 protected void onPause() {
  super.onPause();
  Log.i("生命週期", "2_onPause");
 }

 @Override
 protected void onRestart() {
  super.onRestart();
  Log.i("生命週期", "2_onRestart");
 }

 @Override
 protected void onResume() {
  super.onResume();
  Log.i("生命週期", "2_onResume");
 }

 @Override
 protected void onStart() {
  super.onStart();
  Log.i("生命週期", "2_onStart");
 }

 @Override
 protected void onDestroy() {
  super.onDestroy();
  Log.i("生命週期", "2_onDestroy");
 }
}

沒有留言 :

張貼留言