[챕터 2-3-4] Parcelable

2020. 2. 10. 19:48Android/Android 챕터 2-3

반응형

결과화면

 

메뉴화면 띄우기 버튼 클릭 시

 

 


app 마우스 우클릭

New > Activity > Empty Activity

 



이름 : SimpleDate

인터페이스 : android.osParcelable


SimpleData.java

더보기
package org.minokuma.myparcelable;

import android.os.Parcel;
import android.os.Parcelable;

public class SimpleData implements Parcelable {

    int code;
    String message;

    public SimpleData(int code, String message){
        this.code = code;
        this.message = message;
    }

    public SimpleData(Parcel src){
        code = src.readInt();
        message = src.readString();

    }

    public static final Parcelable.Creator CREATOR = new Parcelable.Creator(){

        @Override
        public SimpleData createFromParcel(Parcel source) {
            return new SimpleData(source);
        }

        @Override
        public SimpleData[] newArray(int size) {
            return new SimpleData[size];
        }
    };



    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeInt(code);
        dest.writeString(message);
    }
}

 


MainActivity.java

 

더보기
package org.minokuma.myparcelable;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
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.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getApplicationContext(), MenuActivity.class);
                SimpleData data = new SimpleData(200, "OK");
                intent.putExtra("data",data);
                startActivity(intent);
            }
        });

    }
}

 


 

 


MenuActivity.java

더보기
package org.minokuma.myparcelable;

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 MenuActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_menu);

        Button button = findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        Intent intent = getIntent();
        processIntent(intent);

    }

    public void processIntent(Intent intent){
        if(intent != null){
            Bundle bundle = intent.getExtras();
            SimpleData simpleData = bundle.getParcelable("data");

            if(simpleData != null){
                Toast.makeText(this, "전달받은 객체 : " + simpleData.code + ", " + simpleData.message, Toast.LENGTH_SHORT).show();
            }
        }
    }

}

 

 

반응형

'Android > Android 챕터 2-3' 카테고리의 다른 글

[챕터 2-3-5] 라이프 사이클  (0) 2020.02.10
[챕터 2-3-3] 인텐트 2  (0) 2020.02.10
[챕터 2-3-2] 인텐트  (0) 2020.02.10
[챕터 2-3-1] 인플레이션  (0) 2020.02.10