activity中 调用startActivityForResult的步骤及生命周期

说明:一个activity需要另一个activity返回结果给其的时候,可以使用startActivityForResult方法

现在有两个activity,分别为ForResultActivity和ResultActivity

以下案例中ForResultActivity需要知道3+2=?需要在ResultActivity中进行计算,计算后将结果返回ForResultActivity中,并显示;

ForResultActivity界面如下:



ForResultActivity代码如下:

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class ForResultActivity extends Activity implements OnClickListener {
	private Button button1;
	private TextView textView1;

	private static final String TAG = "ForResultActivity";
	private static final int RESULT_CODE = 101;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		Log.e(TAG, "onCreate方法被调用");
		setContentView(R.layout.activity_for_result);
		button1 = (Button) findViewById(R.id.button1);
		button1.setOnClickListener(this);
		textView1 = (TextView) findViewById(R.id.textView1);

	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		Log.e(TAG, "onActivityResult方法被调用");
		if (RESULT_CODE == resultCode) {
			if (data != null) {
				String result = data.getStringExtra("RESULT");
				textView1.setText(result);
			}
		}
	}

	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
		Log.e(TAG, "onStart方法被调用");
	}

	@Override
	protected void onRestart() {
		// TODO Auto-generated method stub
		super.onRestart();
		Log.e(TAG, "onRestart方法被调用");
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		Log.e(TAG, "onResume方法被调用");
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		Log.e(TAG, "onPause方法被调用");
	}

	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
		Log.e(TAG, "onStop方法被调用");
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		Log.e(TAG, "onDestroy方法被调用");
	}

	@Override
	public void onClick(View v) {
		Intent intent = new Intent(this, ResultActivity.class);
		// 传人3和2。。。
		startActivityForResult(intent, RESULT_CODE);
	}
}

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="计算3+2等于几?" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="结果显示在这里" />

</LinearLayout>




生命周期:




说明:ForResultActivity调用startActivityForResult()方法启动ResultActivity,通过重写onActivityResult()方法获取ResultActivity返回的结果


ForResultActivity中点击按钮时的生命周期:



ResultActivity界面如下:



ResultActivity代码如下:

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class ResultActivity extends Activity implements OnClickListener{
	private Button button1;
	private static final String TAG = "ResultActivity";
	private static final int RESULT_CODE = 101; 
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		Log.e(TAG, "onCreate方法被调用");
		setContentView(R.layout.activity_result);
		// 假装收到3和2。。。计算结果
		button1 = (Button) findViewById(R.id.button1);
		button1.setOnClickListener(this);
	}


	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
		Log.e(TAG, "onStart方法被调用");
	}

	@Override
	protected void onRestart() {
		// TODO Auto-generated method stub
		super.onRestart();
		Log.e(TAG, "onRestart方法被调用");
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		Log.e(TAG, "onResume方法被调用");
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		Log.e(TAG, "onPause方法被调用");
	}

	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
		Log.e(TAG, "onStop方法被调用");
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		Log.e(TAG, "onDestroy方法被调用");
	}

	@Override
	public void onClick(View v) {
		Intent intent = new Intent(this, ForResultActivity.class);
		// 将结果返回
		intent.putExtra("RESULT", "5");
		setResult(RESULT_CODE, intent);
		finish();
	}

	
}


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="等于5-_-点击我将结果返回" />

</LinearLayout>




说明:ResultActivity通过调用setResult方法将结果返回,返回后的界面如下



ResultActivity点击按钮时的生命周期:


可以看到ForResultActivity先调用了onActivityResult方法,然后是onRestart、onStart、onResume方法;


AndroidManifest.xml中如下

 <!-- 需要返回值得Action -->
         <activity
            android:name=".ForResultActivity"
            android:label="@string/app_name" >
        </activity>
        <!-- 将数据返回到ForResultActivity这个Activity中 -->
         <activity
            android:name=".ResultActivity"
            android:label="@string/app_name" >
        </activity>



  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值