- 화면넘기기
- callback(onActivityResult)
MainActivity.java
package com.example.myapplication;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
//final static String EXTRA_MESSAGE = "com.example.myapplication.MESSAGE";
public static final int REQUEST_CODE = 1000; // 상수 값을 선언 변하지않는수
private EditText editText;
private Button btn_move;
private int requestCode;
private int resultCode;
@Nullable
private Intent data;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = findViewById(R.id.edit_text);
btn_move = findViewById(R.id.btn_move);
btn_move.setOnClickListener(this);
// 1. 익명함수 >> 재사용 안할 때
/*btn_move.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(), IntentActivity.class);
intent.putExtra("text", editText.getText().toString());
startActivityForResult(intent, REQUEST_CODE);
}
});*/
}
// 2. 레이아웃파일에다가 지정하는 방법
public void sendMessage(View view) {
Intent intent = new Intent(this, IntentActivity.class);
intent.putExtra("text", editText.getText().toString());
startActivityForResult(intent, REQUEST_CODE);
}
// 3. 자바파일에서 버튼이랑 온클릭을 연결하는방법
@Override
public void onClick(View v) {
Intent intent = new Intent(this, IntentActivity.class); // 현재 액티비티, 이동하고 싶은 액티비티를 인자로 받음
intent.putExtra("text", editText.getText().toString()); // 내용을 실어줌
startActivityForResult(intent, REQUEST_CODE); // 액티비티 이동
}
// 뒤로가기
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// 수신 성공!!!
if(requestCode == REQUEST_CODE && resultCode == RESULT_OK && data != null) {
//String result = data.getStringExtra("result");
Toast.makeText(this, "SUCCESS", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "FAIL", Toast.LENGTH_SHORT).show();
}
}
}
IntentActivity.java
package com.example.myapplication;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
public class IntentActivity extends AppCompatActivity {
//private LinearLayout container;
private TextView textView;
private Button btn_back;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_intent);
// 1. 화면에 나타낼 뷰를 지정하는 역할
// 2. XML 레이아웃 내용을 메모리 상에 객체화하는 역할
Intent intent = getIntent();
String text = intent.getStringExtra("text"); // 데이터 수신
//container = (LinearLayout) findViewById(R.id.activity_intent);
//LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.activity_intent, container, false);
textView = findViewById(R.id.result);
btn_back = findViewById(R.id.btn_back);
textView.setText(text);
btn_back.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.putExtra("comebackhome", btn_back.getText().toString());
setResult(RESULT_OK, intent); // 결과값 설정
finish(); // 현재 액태비티 종료 >> 인텐트 액티비티가 파괴됨
}
});
}
}
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"
android:orientation="vertical"
tools:context=".MainActivity">
<EditText
android:id="@+id/edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:text="" />
<Button
android:id="@+id/btn_move"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="sendMessage"
android:text="이동" />
</LinearLayout>
activity_intent.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/activity_intent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".IntentActivity">
<TextView
android:id="@+id/result"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/btn_back"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="뒤로가기" />
</LinearLayout>
인텐트로 값을 넘기고 넘긴화면에서 다시 돌아올때 예전 화면에서 result code를 넘기고 예전화면을 파괴하는 코드
'System Programming > Android' 카테고리의 다른 글
[Android/Java] WebView (0) | 2021.06.12 |
---|---|
[Android/Java] Sharedpreferences로 임시저장(알림같은 설정값 저장) (0) | 2021.06.12 |
[Android/Java] menu (0) | 2021.06.06 |
[Android/Java] Canvas, touch event 처리 (0) | 2021.06.06 |
[Android/Java] 암시적 Intent 명시적 Intent (0) | 2021.06.05 |