(manifest.xml 에서 application activity 추가해줘야됨) 




  main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/button01"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="이동" />
    <Button
        android:id="@+id/button02"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="데이터를 갖고 이동" />

</LinearLayout>


main_two.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" >
    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />

</LinearLayout>


manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="kr.android.intent.msg"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="10" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".IntentMsg"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name="IntentTwo"></activity>
    </application>

</manifest>


package kr.android.intent.msg;

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

public class IntentMsg extends Activity implements View.OnClickListener{
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		Button button = (Button)findViewById(R.id.button01);
		Button button2 = (Button)findViewById(R.id.button02);

		button.setOnClickListener(this);
		button2.setOnClickListener(this);
	}
	public void onClick(View v){
		Intent i = null;
		if(v.getId() == R.id.button01){
			i = new Intent(this,IntentTwo.class);
		}else if(v.getId() == R.id.button02){
			//Intent에 실행시킬 Activity 정보 저장
			i = new Intent(this,IntentTwo.class);
			//실행 시킨 Activity에서 사용할 데이터 셋팅
			//사용할 데이터는 Bundle 객체가 생성된후 그 곳에 보관됨
			i.putExtra("msg", "인텐트로 전달된 데이터");
		}
		//Intent객체가 가지고 있는 Activity 정보를 이용해서 Activity 생성,실행
		startActivity(i);
	}
}


package kr.android.intent.msg;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
public class IntentTwo extends Activity{
	TextView view;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main_two);
		view = (TextView)findViewById(R.id.text);
		view.setText("intentTwo 화면\n");

		Intent i = getIntent();
		if(i.getExtras() == null){
			view.append("데이터 없이 화면 이동");
		}else{
			//Activity 실행시 데이터를 저장했다면 Bundle 객체로 부터 데이터 추출
			Bundle b = i.getExtras();
			String str = b.getString("msg");
			view.append(str);
		}
	}
}






+ Recent posts