Languages/Android
[Android] ์๋๋ก์ด๋ View & ViewGroup | Activity Life Cycle ์์
- -
๋ฐ์ํ
์๋๋ก์ด๋ ๋ชจ๋ ๊ธ์ ๊ฐ์ธ ๊ณต๋ถ, ๊ฐ๋ฐ ๋ชฉ์ ์ด๋ผ ๊ฐ๋ ์ ๋ํ ์ค๋ช ์ด ์ ๋๋ก ๋ค์ด๊ฐ์ง ์์ผ๋, ์ฐธ๊ณ ํ์๊ธธ ๋ฐ๋๋๋ค.
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
๋ฐ์ํ
'Languages > Android' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Android] ์ธ์ํ์ด ๋ฑ ํฌ ์ทจ์ฝ์ ์ง๋จ | ํ๊ฒฝ์ค์ ํ๊ธฐ. (0) | 2021.03.13 |
---|---|
[Android] ์๋๋ก์ด๋ ์ดํด (0) | 2021.03.13 |
[Android] ์๋๋ก์ด๋ 4๋ ์ปดํฌ๋ํธ (๊ฐ๋ฐ) (0) | 2021.03.13 |
[Android] build.gradle | ๋น๋ ๊ตฌ์ฑ (0) | 2021.03.07 |
Contents
์์คํ ๊ณต๊ฐ ๊ฐ์ฌํฉ๋๋ค