2swan
Android 액티비티 데이터 전달 예제 launcher (더하기) 본문
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"
android:orientation="vertical">
<EditText
android:id="@+id/edtNum1"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<EditText
android:id="@+id/edtNum2"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/btnAdd"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="더하기"/>
</LinearLayout>
sub.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_Sub"
android:orientation="vertical">
<Button
android:id="@+id/btnBack"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="BACK(MainActivity_sub)" />
</LinearLayout>
main.activity
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = findViewById(R.id.btnAdd);
EditText edtNum1 = findViewById(R.id.edtNum1);
EditText edtNum2 = findViewById(R.id.edtNum2);
ActivityResultLauncher<Intent> launcher = registerForActivityResult(
new ActivityResultContracts.StartActivityForResult(),
new ActivityResultCallback<ActivityResult>() {
@Override
public void onActivityResult(ActivityResult result) {
if (result.getResultCode() == Activity.RESULT_OK) {
//로그 찍기
Log.d("ActivityResult : ", result.toString());
Intent intent = result.getData();
int sum = intent.getIntExtra("sum",0 );
Toast.makeText(getApplicationContext(),"합계 : " + sum, Toast.LENGTH_SHORT).show();
}
}
});
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, MainActivity_Sub.class);
intent.putExtra("Num1",Integer.parseInt(edtNum1.getText().toString()));
intent.putExtra("Num1",Integer.parseInt(edtNum2.getText().toString()));
launcher.launch(intent);
}
});
}
}
sub.activity
public class MainActivity_Sub extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_sub);
Intent intent = getIntent();
int num1 = intent.getIntExtra("Num1",0);
int num2 = intent.getIntExtra("Num2",0);
int sum = num1 + num2;
Button button = findViewById(R.id.btnBack);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent1 =new Intent(getApplicationContext(),MainActivity.class);
setResult(RESULT_OK, intent1);
intent1.putExtra("sum",sum);
finish();
}
});
}
}
결과 값
'Programming > Android Example' 카테고리의 다른 글
암시적 인텐트 예제 (바인딩 방식) (0) | 2023.08.04 |
---|---|
Android 액티비티 데이터 전달 예제 launcher (0) | 2023.08.04 |
Android 액티비티 데이터 전달 예제 (0) | 2023.08.04 |
Android 화면 전환 (0) | 2023.08.03 |
Android Rating Bar 증가 (0) | 2023.08.03 |