๊ด€๋ฆฌ ๋ฉ”๋‰ด

ruriruriya

[Android] ์•ˆ๋“œ๋กœ์ด๋“œ - ๊ฐ์ฒด ์ „์ฒด ๋ฐ์ดํ„ฐ๋ฅผ ํ•œ๊บผ๋ฒˆ์— ๋‹ค๋ฅธ ์•กํ‹ฐ๋น„ํ‹ฐ์— ๋ณด๋‚ด๊ธฐ Serializable (๋ฐ์ดํ„ฐ ์ง๋ ฌํ™”) ๋ณธ๋ฌธ

๐Ÿค–Android/Java

[Android] ์•ˆ๋“œ๋กœ์ด๋“œ - ๊ฐ์ฒด ์ „์ฒด ๋ฐ์ดํ„ฐ๋ฅผ ํ•œ๊บผ๋ฒˆ์— ๋‹ค๋ฅธ ์•กํ‹ฐ๋น„ํ‹ฐ์— ๋ณด๋‚ด๊ธฐ Serializable (๋ฐ์ดํ„ฐ ์ง๋ ฌํ™”)

๋ฃจ๋ฆฌ์•ผใ…‘ 2024. 1. 8. 15:20
๋ฐ˜์‘ํ˜•

์ž๋ฐ”์—์„œ Serializable๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ์ง๋ ฌํ™”ํ•˜๋Š” ์ธํ„ฐํŽ˜์ด์Šค๋‹ค.
์ด๋ฅผ ํ†ตํ•ด ๊ฐ์ฒด์˜ ์ƒํƒœ๋ฅผ ๋ฐ”์ดํŠธ ์ŠคํŠธ๋ฆผ์œผ๋กœ ๋ณ€ํ™˜ํ•˜์—ฌ ๋„คํŠธ์›Œํฌ๋ฅผ ํ†ตํ•ด ์ „์†กํ•˜๊ฑฐ๋‚˜ ํŒŒ์ผ๋กœ ์ €์žฅํ•  ์ˆ˜ ์žˆ๋‹ค.
์•ˆ๋“œ๋กœ์ด๋“œ ์•ฑ ๊ฐœ๋ฐœ์—์„œ๋„ Serializable ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ฐ์ฒด๋ฅผ ์ง๋ ฌํ™”ํ•  ์ˆ˜ ์žˆ๋‹ค.

 

1. ํ™”๋ฉด ์„ค๊ณ„

activity_main.xml / acitivity_sub.xml

 

2. Data.java Serializable ์ƒ์†

import java.io.Serializable;

public class Data implements Serializable {

    public String name;
    public int age;

    public Data() {
    }

    public Data(String name, int age) {
        this.name = name;
        this.age = age;
    }
}

 

3. MainActivity.java

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.sunny.serializable.model.Data;

public class MainActivity extends AppCompatActivity {

    TextView txtMain;

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

        txtMain = findViewById(R.id.txtMain);

        txtMain.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                // Intent ๊ฐ์ฒด ์ƒ์„ฑ
                Intent intent = new Intent(MainActivity.this, SubActivity.class);
                // Data ํด๋ž˜์Šค ๊ฐ์ฒด ์ƒ์„ฑ
                Data data = new Data("ํ™๊ธธ๋™",30);

                // Data ํด๋ž˜์Šค์˜ ์ง๋ ฌํ™” ๋ฐ์ดํ„ฐ ์ „๋‹ฌ
                intent.putExtra("data",data);

                startActivity(intent);


            }
        });
    }
}

 

4. SubActivity.java

import android.os.Bundle;
import android.widget.TextView;

import com.sunny.serializable.model.Data;

public class SubActivity extends AppCompatActivity {

    TextView txtName;
    TextView txtAge;

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

        txtName = findViewById(R.id.txtName);
        txtAge = findViewById(R.id.txtAge);

        // Data ํด๋ž˜์Šค ๊ฐ์ฒด ์ƒ์„ฑ, data
        Data data = new Data();

        // ์ง๋ ฌํ™”๋œ Data ํด๋ž˜์Šค์˜ ๊ฒ์ฒด๋ฅผ getSerializableExtra ๋ฉ”์†Œ๋“œ๋กœ ์ „๋‹ฌ ๋ฐ›์Œ.
        data = (Data) getIntent().getSerializableExtra("data");

        txtName.setText(data.name);
        txtAge.setText(data.age + "์‚ด ์ž…๋‹ˆ๋‹ค.");






    }
}

 

5. MainActivity์—์„œ SubActivity๋กœ ์‹คํ–‰ํ™”๋ฉด

MainActivity TextView ํด๋ฆญ / SubActivity ํ™”๋ฉด

 

 

 

๋ฐ˜์‘ํ˜•