1、使用Intent對象來指定一個Activity,並通過startActivity或startActivityForResult方法啟動該Activity.以及在Activity之間傳遞數據:
- public class MainActivity extends Activity {
-
- private Button btn1,btn2;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- LinearLayout view = new LinearLayout(this);
- view.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
-
- btn1 = new Button(this);
- btn1.setId(1);
- btn1.setText("使用類名開啟另一個Activity");
- btn1.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
-
- btn2 = new Button(this);
- btn2.setId(2);
- btn2.setText("使用action開啟另一個Activity");
- btn2.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
-
- view.addView(btn1);
- view.addView(btn2);
- setContentView(view);
-
- btn1.setOnClickListener(listener);
- btn2.setOnClickListener(listener);
- }
-
- private View.OnClickListener listener = new OnClickListener() {
- @Override
- public void onClick(View view) {
- Intent intent = new Intent();
- switch (view.getId()) {
- case 1:
- intent.setClass(MainActivity.this, ResultActivity.class);
- intent.putExtra("name", "JAVA");
- startActivityForResult(intent, 222);
- break;
- case 2:
- intent.setAction("forResult");
- MyData myData = new MyData();
- //使用putExtra傳遞自定義對象時,該對象必須實現序列化,該類中的所有成員屬性也必須實現序列化接口
- intent.putExtra("data", myData);
- startActivity(intent);
- break;
- default:
- break;
- }
- }
- };
-
-
-
- @Override
- protected void onActivityResult(int requestCode, int resultCode, Intent data) {
- //requestCode是請求碼,即startActivityForResult(intent, 222)中的第二個參數
- //resultCode是結果碼,即setResult(333)中設置的數字
- switch (requestCode) {
- case 222:
- if(resultCode == 333){
- Toast.makeText(this, "無結果返回...", Toast.LENGTH_LONG).show();
- } else if(resultCode == 444){
- Toast.makeText(this, data.getStringExtra("result"), Toast.LENGTH_LONG).show();
- }
- break;
- default:
- break;
- }
- }
- }
- public class ResultActivity extends Activity {
- private Button btn1 , btn2;
- private TextView tv;
- private Intent intent;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- // TODO Auto-generated method stub
- super.onCreate(savedInstanceState);
-
- intent = getIntent();
-
- LinearLayout view = new LinearLayout(this);
- view.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
- LayoutParams.FILL_PARENT));
-
- tv = new TextView(this);
- tv.setText("name is "+intent.getStringExtra("name"));
- tv.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
- LayoutParams.WRAP_CONTENT));
-
- btn1 = new Button(this);
- btn1.setId(1);
- btn1.setText("Back1");
- btn1.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
- LayoutParams.WRAP_CONTENT));
-
- btn2 = new Button(this);
- btn2.setId(2);
- btn2.setText("Back2");
- btn2.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
- LayoutParams.WRAP_CONTENT));
-
- view.addView(tv);
- view.addView(btn1);
- view.addView(btn2);
-
- btn1.setOnClickListener(listener);
- btn2.setOnClickListener(listener);
- setContentView(view);
- }
-
- private View.OnClickListener listener = new OnClickListener() {
- @Override
- public void onClick(View view) {
- switch (view.getId()) {
- case 1:
- setResult(333);
- break;
- case 2:
- intent.putExtra("result", "帶結果返回");
- setResult(444, intent);
- break;
- default:
- break;
- }
- ResultActivity.this.finish();
- }
- };
-
- }
- public class Result2Activity extends Activity {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- MyData data = (MyData)getIntent().getSerializableExtra("data");
- setTitle(data.value);
- }
- }
- public class MyData implements Serializable {
- public String value = "this is test";
- }
2、Intent指定Activity的方式
2.1 直接使用Class
code :
intent.setClass(MainActivity.this, ResultActivity.class);
manifest :
<activity Android:name=".ResultActivity"></activity>
2.2 使用Action
code :
intent.setAction("forResult");
manifest :
<activity android:name=".Result2Activity">
<intent-filter>
<action android:name="forResult"></action>
<category android:name="android.intent.category.DEFAULT"></category>
</intent-filter>
</activity>
2.3 使用包名
code :
ComponentName cn = new ComponentName(MainActivity.this, "com.zj.activity.another.ActivityA");
intent.setComponent(cn);
manifest:
<activity android:name=".another.ActivityA"></activity>