詳細なonActivityResult
インターフェイス間をジャンプしてデータを転送する場合、ジャンプ後にインターフェイスが返すステータス、データ、およびその他の情報を取得する必要がある場合があります。現時点では、最初に設定されたインターフェイスに戻るために必ずしもIntentを使用する必要はありませんが、この問題を解決するにはonActivityResultメソッドを使用します。
onActivityResult(int requestCode, int resultCode, @Nullable Intent data)
requestCode:
startActivityForResult(Intent intent, int requestCode)
渡したものに対応するリクエストコード
requestCode
、このパラメーターの機能は、複数のアクティビティーが結果を返すときに返されるアクティビティーを判別できるようにすることです。
resultCode
:戻りコード。このパラメーターを使用して、サブアクティビティによって返されるステータスを判別します。OK、CANCELED、RESULT_FIRST_USERなどが
あります。これは、基本的にサブアクティビティのインスタンス変数int mResultCodeです。この変数は、次の場合にRESULT.CANCELEDに割り当てられます。定義されているため、デフォルトで戻りますRESULT.CANCELEDはsetResult(int resultCode)で変更できます
data:子アクティビティのインスタンス変数Intent mResultDataに対応し、nullに初期化されたデータの転送に使用され、setResult(int resultCode、Intent data)で変更できます。
例:
MyActivityA.java
public class MyActivityA extends AppCompatActivity implements View.OnClickListener {
public final int REQUEST_CODE_B = 1;
public final int REQUEST_CODE_C = 2;
private Button buttonB;
private Button buttonC;
private Intent intent;
private TextView result_text;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_activity_a);
buttonB = findViewById(R.id.btn_B);
buttonC = findViewById(R.id.btn_C);
result_text = findViewById(R.id.result_text);
buttonB.setOnClickListener(this);
buttonC.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.btn_B:
intent = new Intent(MyActivityA.this,MyActivityB.class);
startActivityForResult(intent,REQUEST_CODE_B);
break;
case R.id.btn_C:
intent = new Intent(MyActivityA.this,MyActivityC.class);
startActivityForResult(intent,REQUEST_CODE_C);
break;
default:
break;
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
switch (requestCode) {
case REQUEST_CODE_B:
case REQUEST_CODE_C: //返回的结果是来自于Activity C
if (resultCode == Activity.RESULT_OK) {
result_text.setText(data.getStringExtra("respond"));
} else {
result_text.setText("No Name Activity");
}
break;
default:
break;
}
super.onActivityResult(requestCode, resultCode, data);
}
}
MyActivityB.java
public class MyActivityB extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_b);
Button btn = findViewById(R.id.b_back_a);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.putExtra("respond", "Hello,I'm B Activity.");
setResult(Activity.RESULT_OK, intent);
finish();
}
});
}
}
MyActivityC.java
public class MyActivityC extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_c);
Button btn = findViewById(R.id.c_back_a);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.putExtra("respond", "I'm C Activity.");
setResult(Activity.RESULT_OK, intent);
finish();
}
});
}
}
activity_my_activity_a.xml:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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="com.example.myscrollview.acivity.MyActivityA">
<Button
android:id="@+id/btn_B"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:text="B Activity"
/>
<Button
android:id="@+id/btn_C"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="@id/btn_B"
android:text="C Activity"
/>
<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/result_text"
android:layout_width="match_parent"
android:layout_height="300dp"
android:gravity="center"
android:textSize="30sp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="@id/btn_C"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
activity_my_b.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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="com.example.myscrollview.acivity.MyActivityB">
<Button
android:id="@+id/b_back_a"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:gravity="center"
android:text="b_back_a"
android:textSize="18sp"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="ACTIVITY B"
android:textSize="32sp"
android:gravity="center"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
activity_my_c.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context="com.example.myscrollview.acivity.MyActivityB">
<Button
android:id="@+id/c_back_a"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:gravity="center"
android:text="c_back_a"
android:textSize="18sp"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="ACTIVITY C"
android:textSize="32sp"
android:gravity="center"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
意図的な知識のレビューは次のとおりです。
インテントの概念:
Androidは、アプリケーション間の相互作用と通信を支援する、またはより正確なステートメントを採用するためのIntentメカニズムを提供します。Intentは、アプリケーション間だけでなく、アプリケーション内のアクティビティ、サービス、およびブロードキャストレシーバー間の相互作用にも使用できます。
インテントは、プログラムの実行中に2つの異なるコンポーネントを接続できるランタイムバインディング(ランタイムバインディング)メカニズムです。インテントを通じて、プログラムはAndroidに対して特定の要求または意欲を表現でき、Androidは意欲の内容に応じて適切なコンポーネントを選択して応答します。
アクティビティ、サービス、およびブロードキャストレシーバーは、インテントを介して通信します。別のコンポーネントであるコンテンツプロバイダーは、それ自体が通信メカニズムであり、インテントを必要としません。
Context.startActivity()
またはを使用してActivity.startActivityForResult()
、アクティビティを開始するインテントを渡します。を使用Activity.setResult()
して、アクティビティから結果を返すインテントを渡します。
インテントオブジェクトContext.startService()
を渡して、サービスを開始するか、実行中のサービスにメッセージを送信します。Context.bindService()
サービスをバインドするために渡されたインテントオブジェクト。
インテントオブジェクトをブロードキャストメソッドに渡すContext.sendBroadcast(),Context.sendOrderedBroadcast()
かContext.sendStickyBroadcast()
、ブロードキャストメソッドを待機してから、ブロードキャストレシーバーに渡します。
インテントの関連属性:
インテントは次のコンポーネントで構成されます
component(组件)
::目的コンポーネント
action(动作)
:インテントを表現する
category(类别)
ために使用されるアクション
data(数据)
:アクションを表現するために使用されるタイプ:アクションによって操作される
type(数据类型)
データ
extras(扩展信息)
:データ例の説明:拡張情報
Flags(标志位)
:の動作モード意図が期待されます