1.从当前activity,跳转到当前应用程序的activity
Intent intent = new Intent(MainActivity.this, Intent2Activity.class);//当前的activity,目的activity
startActivity(intent); //启动intent
2.activity的唯一标识
(1)action (系统的和自定义的)
(2)category
(3)data
<activity android:name=".ThirdActivity">
<intent-filter>
<action android:name="android.intent.action.VIEW"></action>
<category android:name="android.intent.category.DEFAULT"></category>
</intent-filter>
</activity>
3.传递参数,返回结果
(1)
Intent intent = new Intent(MainActivity.this, Intent2Activity.class);
intent.putExtra("username", "admin");//设置参数
// startActivity(intent);
startActivityForResult(intent, 1);//目的activity,requestCode
(2)终结有两种方式
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//返回数据
Intent intent = new Intent();
intent.putExtra("rs", "success...");
setResult(RESULT_OK, intent);
finish();
}
});
//用户按下返回按钮
@Override
public void onBackPressed() {
Intent intent = new Intent();
intent.putExtra("rs", "back success....");
setResult(RESULT_OK, intent);
finish();
}
当目的activity销毁之后,会调用上一个activity的OnActivityResult方法:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case 1:
if (resultCode == RESULT_OK) {
Toast.makeText(this, "result is " + data.getStringExtra("rs"), Toast.LENGTH_SHORT).show();
}
default:
break;
}
}