`

StartForResult的基本用法

阅读更多

前阵子代码中有Activity跳转回调传值的需求,用到了StartForResult。

 

代码 demo,三个Activity,A B C ,A是启动用,跳转到B,B带参数至C,C关闭后传值给B

 

 

Test Activity

package com.nico;

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

public class Test extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Button btn1 = (Button) findViewById(R.id.btn1);
        btn1.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(Test.this,Test01.class);
				startActivity(intent);
			}
		});
    }
}
 

Test 01 Activity

 

package com.nico;

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 Test01 extends Activity {
	TextView txt2;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.test01);
        Button btn2 = (Button) findViewById(R.id.btn2);
        txt2 = (TextView) findViewById(R.id.txt2);
        btn2.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent intent = new Intent();
				intent.setClass(Test01.this, Test02.class);
				intent.putExtra("name", "nicolas");
                                 //101 是请求码,方便在onActivityResult方法内处理不同请求码返回的intent 请求码须>=0
				startActivityForResult(intent, 101);
			}
		});
    }

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		Log.v("Test","------------------------->");
		String ss = data.getStringExtra("newname");
		txt2.setText(ss);
		
	}
    
}

 Test02 Activity

 

package com.nico;


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 Test02 extends Activity {
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.test02);
		Intent intent = getIntent();
		String ss = intent.getStringExtra("myname");
		Button btn3 = (Button) findViewById(R.id.btn3);
		TextView txt3 = (TextView) findViewById(R.id.txt3);
		txt3.setText(ss);
		btn3.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				Intent in = new Intent();
				in.putExtra("newname", "dqcao+newname");
                                //-1为RESULT_OK, 1为RESULT_CANCEL..
                                // in 则是回调的Activity内OnActivityResult那个方法内处理
				setResult(-1, in);
				Log.v("Test","-------->"+Test02.this.getCallingActivity().getClassName());

				finish();

			}
		});
	}
}
 

有的时候在多图层的时候,并不好用,因为在接收的ACTIVITY内,其Activity的callingActivity为空,具体还未去理。

 

当然全局变量 Application也可以使用....

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics