彩蛋OneActivity 生命周期:onCreate onStart onResume 跳到Two onPause onStop Two销毁(finish)onRestart onStart onResume[启发:通过SP传参,One获取数据可以在onRestart 方法中]

应用场景:Activity在关闭时需要向上一个Activity传递数据
OneActivity :

package com.blzt.register.startactivityforresult;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.TextView;

import com.blzt.register.R;

public class OneActivity extends AppCompatActivity {
private TextView mTvOne;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_one);
mTvOne = (TextView) findViewById(R.id.tv_one);

Intent mIntent=new Intent(this,TwoActivity.class);
startActivityForResult(mIntent,1);
Log.e("wy", "onCreate: ");
}

@Override
protected void onRestart() {
super.onRestart();
Log.e("wy", "onRestart: ");
}

@Override
protected void onStart() {
super.onStart();
Log.e("wy", "onStart: ");
}

@Override
protected void onResume() {
super.onResume();
Log.e("wy", "onResume: ");
}

@Override
protected void onStop() {
super.onStop();
Log.e("wy", "onStop: ");
}

@Override
protected void onPause() {
super.onPause();
Log.e("wy", "onPause: ");
}

@Override
protected void onDestroy() {
super.onDestroy();
Log.e("wy", "onDestroy: ");
}

@Override
protected void onActivityResult(final int requestCode, final int resultCode, final Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode==1 &resultCode==RESULT_OK){
mTvOne.setText("Two Activity 回传的数据:"+data.getStringExtra("three"));
}

}
}

TwoActivity

package com.blzt.register.startactivityforresult;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;

import com.blzt.register.R;

public class TwoActivity extends AppCompatActivity {
private EditText mTvTwo;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_two);
mTvTwo = (EditText) findViewById(R.id.tv_two);

}

public void go1(View view) {

String mS = mTvTwo.getText().toString();
Intent mIntent = getIntent().putExtra("three", mS); //将计算的值回传回去
setResult(RESULT_OK, mIntent);
finish(); //结束当前的activity的生命周期
}
}

布局文件
tv_one

xml version="1.0" encoding="utf-8"?>
<RelativeLayout
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=".startactivityforresult.OneActivity">
<TextView
android:id="@+id/tv_one"
android:text="@string/one"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
RelativeLayout>

tv_two

xml version="1.0" encoding="utf-8"?>
<RelativeLayout
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=".startactivityforresult.TwoActivity">

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

<Button
android:id="@+id/bt_two"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/tv_two"
android:onClick="go1"
android:text="@string/bt_two"/>
RelativeLayout>