1.直接启动Activity
Intent i=new Intent(NewActivity.class);
startActivity(i);
2.带返回值启动Activity
入口Activity
package com.anjoyo.startactivity;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
private Button btn1, btn2;
public static final int REQUEST_A = 1;
public static final int REQUEST_B = 2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn1 = (Button) findViewById(R.id.btn);
btn2 = (Button) findViewById(R.id.btn2);
btn1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
startActivityForResult(
new Intent(MainActivity.this, Act2.class), REQUEST_A);
}
});
btn2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
startActivityForResult(
new Intent(MainActivity.this, Act3.class), REQUEST_B);
}
});
}
// 当一个任务结束的时候出发
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case REQUEST_A:
switch (resultCode) {
case 1:
System.out.println(data.getStringExtra("act2"));
break;
case 2:
System.out.println("未得到数据");
break;
}
break;
case REQUEST_B:
switch (resultCode) {
case 1:
System.out.println("未得到数据");
break;
case 2:
System.out.println(data.getStringExtra("act3"));
break;
}
break;
}
super.onActivityResult(requestCode, resultCode, data);
}
}
新启动的Activity
package com.anjoyo.startactivity;
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 Act2 extends Activity {
/**
* 研究Activity启动方式:2种 Activity回传值:接收第二个act的传递的数据
*/
private Button btn;
public static final int RESULT_ACT2 = 2;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.act2);
btn = (Button) findViewById(R.id.bt1);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent();
i.putExtra("act2", "我是Act2");
setResult(RESULT_ACT2, i);
finish();
}
});
}
}
package com.anjoyo.startactivity;
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 Act3 extends Activity {
private Button btn;
public static final int RESULT_ACT2 = 2;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.act3);
btn = (Button) findViewById(R.id.bt2);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent();
i.putExtra("act3", "我是Act3");
setResult(RESULT_ACT2, i);
finish();
}
});
}
}
3.关闭Activity
其中一个是finish();另外一个finishActivity(intrequestCode).
finish();在Activity执行完成所有任务且要被关闭时调用
finishActivity(int requestCode):不是用来关闭自身的,而是通过requestCode参数来关闭其他的Activity
转载于:https://blog.51cto.com/7617661/1279260