2011. 12. 6. 16:40

안드로이드 액티비티(Android Activity) Intent로 전환 이동하기

안드로이드는 액정 화면 크기가 작아서 많은 정보를 나타내기 힘들므로 프로그램의 크기가 커지면 기능을 분할하듯이 화면도 전환을 해서 처리하게 됩니다. 이럴때 사용하는 명령어가 Intent인데, 이렇게 액티비티를 전환할때 그냥 다른 액티비티로 이동하기도 하지만 특정 값을 주어서 가거나 거기서 처리한 값을 다시 받아와야 하는 경우도 있겠죠. 각각의 경우에 처리하는 방법을 하나씩 알아봅시다. 뿌잉 뿌잉~

먼저 XML에는 아래처럼 5개의 버튼을 배치합니다.

첫 번째 버튼을 눌러 단순 액티비티 호출과 이동의 경우.
IntentTestMainActivity.java  --->  TargetActivity.java

메인 파일, IntentTestMainActivity.java

package net.br ;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

public class IntentTestActivity extends Activity {    
 /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }//end of onCreate
    
    // 첫 번째 버튼 이벤트. 액티비티를 호출하는 Activity Intent
    public void onClickExplicitIntent(View v){
     Intent intent = new Intent(this, TargetActivity.class);
     this.startActivity(intent);
    }           
}

1. 첫 번째 버튼 - 단순 Activity 전환이동 (TargetActivity.java)

package net.br ;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;

public class TargetActivity extends Activity {
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  // TODO Auto-generated method stub
  super.onCreate(savedInstanceState);
  this.setContentView(R.layout.target);
 }
 
 public void onClickBack(View v){
  finish();
 }
}