package com.example.ch3;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button=findViewById(R.id.button);
button.setxxxxOnClickListener(new View.xxxxOnClickListener() {
@Override
public void xxxxonClick(View view) {
//인턴트 객체 생성
Intent intent=new Intent(getApplicationContext(),ch5.class);
//ch5개체 생성
ch5 data=new ch5(200,"ok");
//인텐트에 ch5 객체 넣기
intent.putExtra("data",data);
//인텐트
startActivity(intent);
}
});
}
}
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class ch4 extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ch4);
Button button=findViewById(R.id.button);
button.setxxxxOnClickListener(new View.xxxxOnClickListener() {
@Override
public void xxxxonClick(View view) {
finish();
}
});
//들어온 인텐트로 초기화
Intent intent=getIntent();
processIntent(intent);
}
public void processIntent(Intent intent)
{
if(intent!=null)
{
//Bundle 인텐트안에 데이터를 관리하기 위한 부가 변수
Bundle bundle=intent.getExtras();
ch5 data=bundle.getParcelable("data");
if(data!=null)
{
Toast.makeText(this,"전달받은 객체:"+data.code+","+data.message,Toast.LENGTH_LONG).show();
}
}
}
}
package com.example.ch3;
package com.example.ch3;
import android.os.Parcel;
import android.os.Parcelable;
public class ch5 implements Parcelable {
int code;
String message;
public ch5(int code,String message)
{
this.code=code;
this.message=message;
}
public ch5(Parcel src)
{
code=src.readInt();//갑을 가지고오고 Parcle 데이터 형으로 만들겟다
message=src.readString();
}
public static final Parcelable.Creator CREATOR=new Parcelable.Creator()
{
public ch5 createFromParcel(Parcel in)
{
return new ch5(in);
}
public ch5[] newArray(int size)
{
return new ch5[size];
}
};
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel parcel, int i) {
//Parcel데이터에 넣어준다
parcel.writeInt(code);
parcel.writeString(message);
}
}