- startActivity(intent):只是从当前活动界面跳转到另外一个界面,两个界面中不再有联系。
- startActivityForResult(intnet,requestcode):可以经当前活动界面认为是一个父窗体,要跳转的界面为子窗体,当子窗体关闭时,父窗体会执行onActivityResult()方法,并可以获取子窗体的返回值.
以startActivityForResult为例:
MainActivity:
package com.example.tiaozhuantest01;
import android.content.Intent;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private TextView tv_text;
private Button btn_jump;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv_text=findViewById(R.id.tv_text);
btn_jump=findViewById(R.id.btn_jump);
btn_jump.setOnClickListener(this);
}
@Override
public void onClick(View v) {
if(v.getId()==R.id.btn_jump){
Intent intent = new Intent();
intent.setClass(this,SettingActivity.class);
startActivityForResult(intent,0);
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode==0){
if(resultCode==0){
Bundle mBundle=data.getExtras();
if(mBundle!=null)
tv_text.setText("回传的信息为:"+mBundle.getString("send"));
}
}
}
}
SettingActivity:
package com.example.tiaozhuantest01;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class SettingActivity extends AppCompatActivity implements View.OnClickListener {
private Button btn_back;
private EditText et_text;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_setting);
btn_back=findViewById(R.id.btn_back);
btn_back.setOnClickListener(this);
et_text=findViewById(R.id.et_text);
}
@Override
public void onClick(View v) {
if(v.getId()==R.id.btn_back){
Intent intent = new Intent();
intent.putExtra("send",et_text.getText().toString());
setResult(0,intent);
finish();
}
}
}
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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical">
<View
android:layout_width="match_parent"
android:layout_height="200dp"></View>
<TextView
android:id="@+id/tv_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="回传的信息为:"
android:textSize="25sp"/>
<Button
android:id="@+id/btn_jump"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="跳转"/>
</LinearLayout>
activity_setting:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<View
android:layout_width="match_parent"
android:layout_height="200dp"></View>
<EditText
android:id="@+id/et_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:hint="输入文字"/>
<Button
android:id="@+id/btn_back"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="返回"
android:layout_gravity="center"/>
</LinearLayout>
效果图如下:
image.pngimage.png
image.png
总结:
- 调用startActivityForResult方法可以让跳转的目的Activity实现onActivityResult方法。
- 调用startActivity方法只是普通的跳转。
网友评论