์ƒˆ์†Œ์‹

Languages/Android

[Android] ์•ˆ๋“œ๋กœ์ด๋“œ View & ViewGroup | Activity Life Cycle ์˜ˆ์ œ

  • -
๋ฐ˜์‘ํ˜•

https://greenhelix.tistory.com/164

์•ˆ๋“œ๋กœ์ด๋“œ ๋ชจ๋“  ๊ธ€์€ ๊ฐœ์ธ ๊ณต๋ถ€, ๊ฐœ๋ฐœ ๋ชฉ์ ์ด๋ผ ๊ฐœ๋…์— ๋Œ€ํ•œ ์„ค๋ช…์ด ์ œ๋Œ€๋กœ ๋“ค์–ด๊ฐ€์ง€ ์•Š์œผ๋‹ˆ, ์ฐธ๊ณ ํ•˜์‹œ๊ธธ ๋ฐ”๋ž๋‹ˆ๋‹ค.


Java code

 

DisplayMessageActivity

package org.techtown.a4component_test; import androidx.appcompat.app.AppCompatActivity; import android.content.Intent; import android.os.Bundle; import android.view.ViewGroup; import android.widget.TextView; public class DisplayMessageActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_display_message); Intent intent = getIntent(); // ์–ด๋– ํ•œ ์ธํ…ํŠธ๋กœ ์‹คํ–‰๋˜๋Š” ์ง€ ๊ทธ ์ธํ…ํŠธ ๊ฐ’์„ ์‹คํ–‰์‹œ์ผœ ๋ณผ ์ˆ˜ ์žˆ์Œ. String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE); //mainactivity์—์„œ ๋„˜๊น€ ๊ฐ’์„ message๋กœ ๋ฐ›์•„์˜จ ๊ฒƒ. TextView textView = new TextView(this); //์ด๋Ÿฐ์‹์œผ๋กœ ์ž๋ฐ”์ž์ฒด์—์„œ๋„ ๋””์ž์ธ์ชฝ ๋‹ด๋‹น ๊ฐ€๋Šฅ! xml ํŒŒ์ผ์— ๋ณด์—ฌ์งˆ ํ…์ŠคํŠธ์— ๋Œ€ํ•œ ์˜ต์…˜ ์„ค textView.setTextSize(40); textView.setText(message); ViewGroup layout = findViewById(R.id.activity_disply_message);//LinearLayout์ž์ฒด๊ฐ€ ViewGroup์˜ ์ƒ์†์„ ๋ฐ›์€ ๊ฒƒ์ด๊ธฐ ๋•Œ๋ฌธ์— ๋ทฐ ๊ทธ๋ฃน์œผ๋กœ ๊ฐ€์ง€๊ณ  ์™€๋„ ๋จ! //๊ธฐ์กด์—๋Š” findViewByid์•ž์— (ViewGroup)์ด๋ ‡๊ฒŒ ์บ์ŠคํŒ…์„ ํ•ด ์ฃผ์—ˆ์ง€๋งŒ, ์ด์ œ๋Š” ์บ์ŠคํŒ…์„ ์ƒ๋žต๊ฐ€๋Šฅ. layout.addView(textView); //addView๋กœ ๋งŒ๋“  ํ…์ŠคํŠธ๋ฅผ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ๋‹ค } }

 

MainActivity

package org.techtown.a4component_test; import androidx.appcompat.app.AppCompatActivity; import android.content.Intent; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.EditText; public class MainActivity extends AppCompatActivity { public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE"; //๋‚˜์ค‘์— ํ—ท๊ฐˆ๋ฆฌ๊ฑฐ๋‚˜ ์˜คํƒ€๊ฐ€ ๋‚˜๋Š” ๊ฒƒ์„ ๋ฐฉ์ง€ํ•˜๊ธฐ ์œ„ํ•ด์„œ ๊ทธ๋ƒฅ ์ •์˜ํ•ด์ค€ ๊ฒƒ์ด๋ผ ์ƒ๊ฐํ•˜๋ฉด ๋จ. @Override //์ตœ์ดˆ ์•กํ‹ฐ๋น„ํ‹ฐ ์ƒ์„ฑ, ๋‚ด๋ถ€์— ์ž๋™์œผ๋กœ ์ƒ์„ฑ๋จ. protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Log.e("onCreate", "Create!!!!"); } @Override //์ด๋•Œ๋ถ€ํ„ฐ ์‚ฌ์šฉ์ž๊ฐ€ ์•กํ‹ฐ๋น„ํ‹ฐ๋ฅผ ๋ณผ ์ˆ˜ ์žˆ์Œ protected void onStart() { super.onStart(); Log.e("onStart", "Start!!!~"); } @Override //์•กํ‹ฐ๋น„ํ‹ฐ๊ฐ€ ์‹ค์ œ ์‚ฌ์šฉ์ž์™€ ์ƒํ˜ธ ์ž‘์šฉ์ด ๊ฐ€๋Šฅํ•œ ํฌ ๊ทธ๋ผ์šด๋“œ์— ์œ„์น˜ํ–ˆ์„ ๋•Œ ํ˜ธ์ถœ. -> ์•กํ‹ฐ๋น„ํ‹ฐ ์‹คํ–‰ ์ค‘์ธ ์ƒํƒœ. protected void onResume() { super.onResume(); Log.e("onResume", "Resume!"); } @Override //์•กํ‹ฐ๋น„ํ‹ฐ๊ฐ€ ๋”์ด์ƒ ๋ณด์ด์ง€ ์•Š์„ ๋•Œ ํ˜ธ์ถœ๋œ๋‹ค. protected void onStop() { super.onStop(); Log.e("onStop", "Stop!"); } @Override //์•กํ‹ฐ๋น„ํ‹ฐ๊ฐ€ ์ข…๋ฃŒ๋˜๊ฑฐ๋‚˜ ์•ฑ์ด ์ข…๋ฃŒ๋˜๋ฉด ํ˜ธ์ถœ๋œ๋‹ค. protected void onDestroy() { super.onDestroy(); Log.e("onDestroy", "Destroy~!"); } @Override //์•กํ‹ฐ๋น„ํ‹ฐ๊ฐ€ ์ค‘์ง€๋œ ์ดํ›„ ๋‹ค์‹œ ์‹œ์ž‘๋˜๊ธฐ ๋ฐ”๋กœ์ „์— ํ˜ธ์ถœ protected void onRestart(){ super.onRestart(); Log.e("onRestart","Restart!"); } //์—ฌ๊ธฐ ์œ„๊นŒ์ง„ ์•ˆ๋“œ๋กœ์ด๋“œ ์•กํ‹ฐ๋น„ํ‹ฐ ์ƒ๋ช…์ฃผ๊ธฐ๋ฅผ ํ‘œํ˜„ํ•œ ๊ฒƒ. public void sendMessage(View view) { Intent intent = new Intent(this,DisplayMessageActivity.class); //์ธํ…ํŠธ ๊ฐ์ฒด ์•ˆ์— displaymessageactivity๋ฅผ ๋„ฃ์–ด์คŒ. -> ์ƒˆ๋กœ ๋งŒ๋“  ์•กํ‹ฐ๋น„ํ‹ฐ์ž„. EditText editText = findViewById(R.id.edit_message); //id๊ฐ€ edit_message์ธ editText๋ฅผ ๊ฐ€์ ธ์˜ค๊ฒ ๋‹ค. ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด ๋ณ€์ˆ˜๋ช… ์จ์ฃผ๊ณ  ๊ฐ€์ ธ์˜จ ๊ฒƒ. String message = editText.getText().toString(); intent.putExtra(EXTRA_MESSAGE,message); //name์€ ํ‚ค๋ฅผ ์˜๋ฏธํ•œ๋‹ค๊ณ  ์ƒ๊ฐํ•˜๋ฉด ๋จ. ->์‹ค์ œ ๋ฐ์ดํ„ฐ๋Š” message์— ์žˆ๋‹ค๋Š” ๋œป. ๋ฐ›๋Š”์ชฝ์—์„œ ํ‚ค๋ฅผ ๋˜‘๊ฐ™์ด ์จ์„œ ํ‚ค์˜ ํ•ด๋‹นํ•˜๋Š” ๊ฐ’์„ ์–ป์„ ์ˆ˜ ์žˆ๋‹ค. startActivity(intent); } }

 

 

xml code

activity_display_message.xml

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id ="@+id/activity_disply_message" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="org.techtown.a4component_test.DisplayMessageActivity"> </LinearLayout>

 

activity_main.xml

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <EditText android:id="@+id/edit_message" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" android:hint="@string/edit_message"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="sendMessage" android:text="@string/button_send"/> </LinearLayout>

 

values.strings.xml

<resources> <string name="app_name">4component_test</string> <string name="edit_message">๋ฉ”์„ธ์ง€๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”.</string> <string name="button_send">๋ณด๋‚ด๊ธฐ</string> </resources>

 

๊ฒฐ๊ณผ

 

 

Life Cycle 

๋ฐ˜์‘ํ˜•
Contents

ํฌ์ŠคํŒ… ์ฃผ์†Œ๋ฅผ ๋ณต์‚ฌํ–ˆ์Šต๋‹ˆ๋‹ค

์ด ๊ธ€์ด ๋„์›€์ด ๋˜์—ˆ๋‹ค๋ฉด ๊ณต๊ฐ ๋ถ€ํƒ๋“œ๋ฆฝ๋‹ˆ๋‹ค.