하나의 Activity 에서 다른 Activity로 text값과 이미지를 넘기는 예제.

 

   - Test20_MyAction 받기 전 화면.

 

- Test21_MyActionCall화면에서 Test20_MyAction으로 abcd와 dog이미지를 넘긴 결과 화면.

 

 

프로젝트명 : Test20_MyAction

 

(1) activity_main.xml

<RelativeLayout 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"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/txt1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:text="인텐트테스트입니다." />

    <ImageView
        android:id="@+id/img1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:layout_marginTop="114dp"
        android:src="@drawable/ic_launcher" />

</RelativeLayout>

 

(2) AndroidManifest.xml

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

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="16" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.test20_myaction.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

            <intent-filter >

                <!-- 다른 액티비티가 호출할때 사용하는 action이름 설정 -->
                <action android:name="kr.co.hta.MyAction"/>
                <category android:name="android.intent.category.DEFAULT"/>
            </intent-filter>

    </activity>
    </application>

</manifest>

 

 

(3) MainActivity.java

package com.example.test20_myaction;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.view.Menu;
import android.widget.ImageView;
import android.widget.TextView;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //자신을 호출한 인텐트 얻어오기
        Intent intent = getIntent();

        if(intent.getAction().equals("kr.co.hta.MyAction")){
            //호출한 인텐트가 보내온 이미지와 메시지 얻어오기
            Bitmap bitmap = (Bitmap)intent.getExtras().get("img");
            String msg=(String)intent.getExtras().get("msg");

 

            //전달되어온 정보를 뷰에 넣기
            TextView txt=(TextView)findViewById(R.id.txt1);
            txt.setText("다른액티비티가 보내온 메시지:" + msg);
            ImageView img = (ImageView)findViewById(R.id.img1);
            img.setImageBitmap(bitmap);
        }


    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}

 

 

프로젝트명 : Test21_MyActionCall

 

(1) activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="다른액티비티로 보낼 문자열" />
   
    <EditText
        android:id="@+id/edt1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10" />

    <ImageView
        android:id="@+id/img1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/dog7" />

    <Button
        android:id="@+id/btn1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="다른액티비티로 전송" />

</LinearLayout>

 

(2) MainActivity.java

package com.example.test21_myactioncall;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

 

public class MainActivity extends Activity {
    EditText edt1;
    ImageView img1;
    Button btn1;
   
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
       
        edt1=(EditText)findViewById(R.id.edt1);
        img1=(ImageView)findViewById(R.id.img1);
        btn1=(Button)findViewById(R.id.btn1);

 

        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String str=edt1.getText().toString();
                //R.drawable.dog7이미지를 Bitmap객체로 만들기.
                Bitmap bitmap=BitmapFactory.decodeResource(getResources(), R.drawable.dog7);

 

                //다른 액티비티를 호출하기 위한 인텐트 생성하기.
                Intent intent=new Intent();

 

                //액션이름으로 호출하기(암시적 인텐트)
                intent.setAction("kr.co.hta.MyAction");

 

                //호출되는 액션에 보낼 부가정보 담기
                intent.putExtra("msg",str);
                intent.putExtra("img",bitmap);

 

                //다른 액티비티 호출하기
                startActivity(intent);
            }
        });

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}

+ Recent posts