startActivityForResult
startActivityForResult與startActivity類似都是用來跳轉(zhuǎn)界面。
但是,startActivityForResult和startActivity也有不同的地方:
- startActivity僅僅是跳轉(zhuǎn)到目標頁面殉农,若是想跳回當前頁面,則必須再使用一次startActivity躬贡。
- startActivityForResult可以一次性完成這項任務劫谅,當程序執(zhí)行到這段代碼的時候,假若從T1Activity跳轉(zhuǎn)到下一個Text2Activity粹污,而當這個Text2Activity調(diào)用了finish()方法以后段多,程序會自動跳轉(zhuǎn)回T1Activity,并調(diào)用前一個T1Activity中的onActivityResult方法壮吩。
也就是執(zhí)行startActivityForResult后可以從打開的界面进苍,再返回到主界面去加缘。
相關的接口:
startActivityForResult(Intent intent, Int requestCode)
setResut(int resultCode, Intent intent)
onActivityResult(int requestCode, int resultCode, Intent intent)
實戰(zhàn)代碼
MainActivity.java:
package com.exmple.transition;
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;
public class MainActivity extends AppCompatActivity {
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.switchButton);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d("test---", "onClick---111--");
Intent intent = new Intent();
intent.setClass(MainActivity.this, test2.class);
startActivityForResult(intent, 1);
overridePendingTransition(android.R.anim.slide_in_left, android.R.anim.slide_out_right);
// overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
// overridePendingTransition(0, 0);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Log.d("test---", "resultCode="+resultCode);
}
}
test2.java:
package com.exmple.transition;
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;
public class test2 extends AppCompatActivity {
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test2);
button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d("test---", "onClick---222--");
setResult(1);
finish();
}
});
}
}
activity_main.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=".MainActivity">
<Button
android:id="@+id/switchButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
activity_test2.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=".test2">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="go home"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
執(zhí)行效果: