startActivityForResult(intent, resultCode)

resultCode를 이용해 activity 1 or activity 2에 request code를 보내고

setResult(응답코드, intent)을 이용해 activity로부터 response code를 받는다

finish() method는 request 와 response 처리가 끝난후 activity를 화면애서 없앨때 사용한다

//MainActivity에서 NewActivity로 startActivityForReuslt()를 이용해 requestCode를 보내고

NewActivity에서 setResult()를 이용해 reponseCode를 보낸후 finish()를이용해 화면을 없애준다

MainActivity에서 onActivityResult()를 이용해 responseCode를 처리한다

//onActivityResult(int RequestCode, int ResultCode, intent)

 

activity_main.xml

<TextView
android:id="@+id/txt1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="click the button to open new screen"
android:textSize="20sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.288" />

<Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="256dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:onClick="btnClicked"
android:text="here"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

new_activity.xml

<Button
android:id="@+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="360dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:text="go back to main"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

AndroidManifest.xml

<activity android:name=".Main2Activity"
android:label="new activity"
android:theme="@style/Theme.AppCompat.Dialog"></activity>

Main2Activity.java

public class Main2Activity extends AppCompatActivity {

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

Button button = findViewById(R.id.btn2);
button.setOnClickListener(listener);
}
View.OnClickListener listener = new View.OnClickListener(){
public void onClick(View view){
Intent intent = new Intent();
intent.putExtra(name: "message", value: "transfered message");
//response
setResult(RESULT_OK, intent);
//activity close
finish();
}
};
}

MainActivity.java

public class MainActivity extends AppCompatActivity {
public static final int REQUEST_CODE = 111;

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

Button button = findViewById(R.id.btn1);
// button.setOnClickListener(listener);
}
//OnClickListener를 이용할수도있고 btnClicked method를 만들어서 작동할수도있다
// View.OnClickListener listener = new View.OnClickListener(){
// public void onClick(View view){
// Intent intent = new Intent(getApplicationContext(), Main2Activity.class);
// startActivityForResult(intent, REQUEST_CODE);
// }
// };
public void btnClicked(View view){
Intent intent = new Intent(getApplicationContext(), Main2Activity.class);
startActivityForResult(intent, REQUEST_CODE);
}
//main화면으로 이동
protected void onActivityResult(int requestCode, int resultCode, Intent data){
super.onActivityResult(requestCode, resultCode, data);

if(requestCode == REQUEST_CODE){
Toast.makeText(getApplicationContext(),
"onActivity() call, requestCode:"+requestCode+", responseCode: "+resultCode, Toast.LENGTH_LONG).show();
}
if(resultCode == RESULT_OK){
String responseMsg = data.getStringExtra(name: "message");
Toast.makeText(getApplicationContext(), text: "response message" +responseMsg, Toast.LENGTH_LONG).show();
}
}
}

 

 

'android studio' 카테고리의 다른 글

activity life cycle  (0) 2019.08.14
intent  (0) 2019.08.14
activity 추가하기  (0) 2019.08.13
Recycler View  (0) 2019.08.13
custom dialog  (0) 2019.08.13

+ Recent posts