天天看點

一看就懂(含全部代碼)startActivityForResult

彩蛋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的生命周期
    }
}      
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>      
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>