명시적 인텐트(Explicit Intent)


 실행할 컴포넌트 이름을 명시적으로 정하는 방식이다.


 MainActivity(caller)의 intent를 기반으로 framework에 SubActivity(callee)의 실행을 요청하여 해당 액티비티를 실행한다. call 버튼에 의한 것이면 startActivity()로 결과를 받아올 수 없다.


 call_result 버튼에 의한 것이면 startActivityForResult()로 결과를 받아올 수 있다. 그에 대한 처리는 onActivityResult()에서 한다.


activity_main.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_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context="com.explicitintent.mystoryg.explicitintent.MainActivity">

<Button
android:id="@+id/call"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="call activity" />

<Button
android:id="@+id/call_result"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="call activity and get result" />

<TextView
android:id="@+id/result"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="return data" />
</LinearLayout>


MainActivity.java

 package com.explicitintent.mystoryg.explicitintent;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity implements View.OnClickListener {

private static final String TAG = MainActivity.class.getSimpleName();
public static final String INPUT = "input";
public static final String RESULT = "result";
private static final int SUB_ACTIVITY = 1;
TextView mResult;

static final int[] BUTTONS = {
R.id.call,
R.id.call_result
};

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mResult = (TextView) findViewById(R.id.result);
for (int button : BUTTONS) {
Button btn = (Button) findViewById(button);
btn.setOnClickListener(this);
}
}

@Override
public void onClick(View v) {
int id = v.getId();
switch (id) {
case R.id.call:
call();
break;
case R.id.call_result:
call_result();
break;
}
}

private void call() {
Intent intent = new Intent(getApplicationContext(), SubActivity.class);
intent.putExtra(INPUT, "This is normal call.");
mResult.setText("return data");
startActivity(intent);
}

private void call_result() {
Intent intent = new Intent(getApplicationContext(), SubActivity.class);
intent.putExtra(INPUT, "This is call and get result.");
mResult.setText("return data");
startActivityForResult(intent, SUB_ACTIVITY);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == SUB_ACTIVITY) {
if (resultCode == RESULT_OK) {
mResult.setText(data.getStringExtra(RESULT));
} else {
mResult.setText("Error!");
}
}
super.onActivityResult(requestCode, resultCode, data);
}
}


activity_sub.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_sub"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.explicitintent.mystoryg.explicitintent.SubActivity">

<EditText
android:id="@+id/result_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

<Button
android:id="@+id/response"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="response" />

</LinearLayout> 


SubActivity.java

package com.explicitintent.mystoryg.explicitintent;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class SubActivity extends Activity {

private static final String TAG = SubActivity.class.getSimpleName();
EditText mEdit;

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

String input = getIntent().getStringExtra(MainActivity.INPUT);
if (null != input) {
mEdit = (EditText) findViewById(R.id.result_message);
mEdit.setText(input);
}

Button response = (Button) findViewById(R.id.response);
response.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String response = mEdit.getText().toString();
if(response.length() != 0) {
Intent intent = new Intent();
intent.putExtra(MainActivity.RESULT, response);
setResult(RESULT_OK, intent);
} else {
setResult(RESULT_CANCELED);
}

finish();
}
});
}

} 



+ Recent posts