상세 컨텐츠

본문 제목

[Android]Intent 예제

Android 개발

by mobile 2012. 8. 11. 06:49

본문

반응형

▶ SampleIntentActivity.java

import android.app.Activity;

import android.content.Intent;

import android.os.Bundle;

import android.view.Menu;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.Toast;


public class SampleIntentActivity extends Activity {

// 다른 activity를 띄우기 위한 요청 코드 정의

public static final int REQUEST_CODE_ANOTHER = 1001;


@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);


Button startBtn = (Button) findViewById(R.id.startBtn);

startBtn.setOnClickListener(new OnClickListener() {

public void onClick(View v) {

// 또 다른 Activity를 띄우기 위한 Intent 객체 생성 

Intent intent = new Intent(getBaseContext(),

AnotherActivity.class);

// Activity 띄우기

startActivityForResult(intent, REQUEST_CODE_ANOTHER);

}

});

}


@Override

protected void onActivityResult(int requestCode, int resultCode, Intent data) {

// TODO Auto-generated method stub

super.onActivityResult(requestCode, resultCode, data);

// 또 다른 Activity에서 보내온 응답인지 요청 코드로 확인

if (requestCode == REQUEST_CODE_ANOTHER) {

// toast message

Toast toast = Toast.makeText(getBaseContext(),

"onActivityResult called with code : " + resultCode,

Toast.LENGTH_LONG);

toast.show();

if(resultCode == 1) {

String name = data.getExtras().getString("name");

toast = Toast.makeText(getBaseContext(), "result name : " + name, Toast.LENGTH_LONG);

toast.show();

}

}

}


@Override

public boolean onCreateOptionsMenu(Menu menu) {

getMenuInflater().inflate(R.menu.main, menu);

return true;

}


}


▶ main.xml

<LinearLayout 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" 

    android:orientation="vertical"

    >

<TextView 

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="SampleIntentActivity"    

/>

    <Button

        android:id="@+id/startBtn"

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:text="Start Another Activity" />


</LinearLayout>


▶ AnotherActivity.java

import android.app.Activity;

import android.content.Intent;

import android.os.Bundle;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;


public class AnotherActivity extends Activity {


@Override

protected void onCreate(Bundle savedInstanceState) {

// TODO Auto-generated method stub

super.onCreate(savedInstanceState);

setContentView(R.layout.another);

Button returnBtn = (Button) findViewById(R.id.returnBtn);

returnBtn.setOnClickListener(new OnClickListener() {

public void onClick(View v) {

Intent resultIntent = new Intent();

// intent object를 생성하고 name의 값을 부가 데이터로 넣기

resultIntent.putExtra("name", "mike");

// 응답보내기

setResult(1, resultIntent);

// activity close

finish();

}

});

}

}


▶ Another.xml

<LinearLayout 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" 

    android:orientation="vertical"

    >

<TextView 

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="Another Activity"    

/>

    <Button

        android:id="@+id/returnBtn"

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:text="return" />


</LinearLayout>


▶ AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

    package="com.example.sampleintent"

    android:versionCode="1"

    android:versionName="1.0" >


    <uses-sdk

        android:minSdkVersion="8"

        android:targetSdkVersion="15" />


    <application

        android:icon="@drawable/ic_launcher"

        android:label="@string/app_name"

        android:theme="@style/AppTheme" >

        <activity

            android:name=".SampleIntentActivity"

            android:label="@string/title_activity_sample_intent" >

            <intent-filter>

                <action android:name="android.intent.action.MAIN" />


                <category android:name="android.intent.category.LAUNCHER" />

            </intent-filter>

        </activity>

        <activity 

            android:name=".AnotherActivity"

            android:label="@string/app_name" >

        </activity>

    </application>


</manifest>


▶ 결과




반응형

관련글 더보기

댓글 영역