<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
intent-filter>
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
Activity的跳转需要创建Intent对象,通过设置intent对象的参数指定要跳转的Activity;通过设置Activity的包名和类名实现跳转,称为显式意图;通过指定动作实现跳转,称为隐式意图
Intent intent = new Intent();
intent.setClass(this, SecondActivity.class);
startActivity(intent);
Intent intent = new Intent();
//启动系统自带的拨号器应用
intent.setClassName("com.android.dialer", "com.android.dialer.DialtactsActivity");
startActivity(intent);
Intent intent = new Intent();
//启动系统自带的拨号器应用
intent.setAction(Intent.ACTION_DIAL);
startActivity(intent);
<intent-filter >
<action android:name="com.itheima.second"/>
<data android:scheme="asd" android:mimeType="aa/bb"/>
<category android:name="android.intent.category.DEFAULT"/>
intent-filter>
//获取启动此Activity的intent对象
Intent intent = getIntent();
Uri uri = intent.getData();
Intent intent = new Intent(this, SecondActivity.class);
intent.putExtra("maleName", maleName);
intent.putExtra("femaleName", femaleName);
startActivity(intent);
Intent intent = getIntent();
String maleName = intent.getStringExtra("maleName");
String femaleName = intent.getStringExtra("femaleName");
onCreate–>onStart–>onResume–>onPause–>onStop–>onDestory
onStart–>onResume–>onPause–>onStop
onResume–>onPause
默认情况下 ,横竖屏切换, 销毁当前的activity,重新创建一个新的activity。在一些特殊的应用程序常见下,比如游戏,不希望横竖屏切换activity被销毁重新创建。模拟器切换横竖屏快捷键ctrl+F11
需求:禁用掉横竖屏切换的生命周期
1、横竖屏写死
android:screenOrientation="landscape"
android:screenOrientation="portrait"
2、让系统的环境 不再去敏感横竖屏的切换。
android:configChanges="orientation|screenSize|keyboardHidden"
每个应用会有一个Activity任务栈,存放已启动的Activity。Activity的启动模式,修改任务栈的排列情况
步骤:
1、开启activity并且获取返回值
startActivityForResult(intent, 0);
2、在新开启的界面里面实现设置数据的逻辑
Intent data = new Intent();
data.putExtra("phone", phone);
//设置一个结果数据,数据会返回给调用者
setResult(0, data);
finish();//关闭掉当前的activity,才会返回数据
3、在开启者activity里面实现方法
onActivityResult(int requestCode, int resultCode, Intent data) //通过data获取返回的数据
4、根据请求码和结果码确定业务逻辑
<activity android:name="com.itheima.createactivity.SecondActivity">activity>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
intent-filter>
1、 创建class类继承Activity
2、 创建布局文件,作为Activity的显示内容
3、 在清单文件中注册Activity
<intent-filter >
<action android:name="com.itheima.sa"/>
<category android:name="android.intent.category.DEFAULT"/>
intent-filter>
android:screenOrientation="portrait"
android:screenOrientation="landscape"
android:configChanges="orientation|keyboardHidden|screenSize"
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
/**
* 跳转至打电话activity
* 跳转至其他应用的activity
* 隐式跳转:通过指定action和data
* @param v
*/
public void click1(View v){
Intent intent = new Intent();
//隐式意图
intent.setAction(Intent.ACTION_CALL);
intent.setData(Uri.parse("tel:110"));
//跳转
startActivity(intent);
}
/**
* 跳转至secondActivity
* 在本应用中跳转
* 显示跳转:直接指定目标Activity的包名和类名
* @param v
*/
public void click2(View v){
Intent intent = new Intent();
//cls:直接指定目标Activity的类名
//显示意图
intent.setClass(this, SecondActivity.class);
startActivity(intent);
}
/**
* 显示跳转至拨号器
*/
public void click3(View v){
Intent intent = new Intent();
//指定目标Activity的包名和类名
intent.setClassName("com.android.dialer", "com.android.dialer.DialtactsActivity");
startActivity(intent);
}
/**
* 隐式跳转至拨号器
*/
public void click4(View v){
Intent intent = new Intent();
//隐式设置拨号器的动作
intent.setAction(Intent.ACTION_DIAL);
startActivity(intent);
}
/**
* 隐式跳转至secondActivity
* @param v
*/
public void click5(View v){
Intent intent = new Intent();
intent.setAction("com.itheima.sa2");
// intent.setData(Uri.parse("heima2:qwe"));
// intent.setType("text/username");
// intent.setData(Uri.parse("heima2:qwe123"));
intent.setDataAndType(Uri.parse("heima2:qwe123"), "text/username");
//系统会自动添加默认的category
intent.addCategory(Intent.CATEGORY_DEFAULT);
startActivity(intent);
}
/**
* 显式跳转至浏览器
*/
public void click6(View v){
Intent intent = new Intent();
intent.setClassName("com.android.browser", "com.android.browser.BrowserActivity");
startActivity(intent);
}
/**
* 隐式跳转至浏览器
* @param v
*/
public void click7(View v){
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setData(Uri.parse("http://www.baidu.com"));
startActivity(intent);
}
}
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void click(View v){
Intent intent = new Intent(this, SecondActivity.class);
//把数据封装至intent对象中
// intent.putExtra("malename", "李志");
// intent.putExtra("femalename", "芙蓉姐姐");
//把数据封装至bundle对象中
Bundle bundle = new Bundle();
bundle.putString("malename", "李志");
bundle.putString("femalename", "芙蓉姐姐");
//把bundle对象封装至intent对象中
intent.putExtras(bundle);
startActivity(intent);
}
}
public class SecondActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
Intent intent = getIntent();
//从intent对象中把封装好的数据取出来
// String maleName = intent.getStringExtra("malename");
// String feMaleName = intent.getStringExtra("femalename");
Bundle bundle = intent.getExtras();
String maleName = bundle.getString("malename");
String feMaleName = bundle.getString("femalename");
Random rd = new Random();
int yinyuan = rd.nextInt(100);
TextView tv = (TextView) findViewById(R.id.tv);
tv.setText(maleName + "和" + feMaleName + "的姻缘值为" + yinyuan);
}
}
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void click(View c){
//跳转至选择联系人Activity
Intent intent = new Intent(this, ContactActivity.class);
// startActivity(intent);
//用这个api启动的Activity,在销毁时,系统会回调onActivityResult
startActivityForResult(intent, 10);
}
public void click2(View v){
//跳转至选择快捷回复的Activity
Intent intent = new Intent(this, CallbackActivity.class);
startActivityForResult(intent, 20);
}
//如果有Activity在销毁时返回了数据,那么就会调用此方法来接收数据
//requestCode:用来区分数据来自于哪一个Activity
//resultCode:用来区分返回的数据是什么类型的
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
String name = data.getStringExtra("name");
if(requestCode == 10){
EditText et = (EditText)findViewById(R.id.et);
et.setText(name);
}
else if(requestCode == 20){
EditText et_content = (EditText)findViewById(R.id.et_content);
et_content.setText(name);
}
}
}
public class ContactActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_contact);
ListView lv = (ListView) findViewById(R.id.lv);
final String[] objects = new String[]{
"小志",
"逼哥",
"世界级XXX",
"国服第一"
};
lv.setAdapter(new ArrayAdapter(this, R.layout.item_listview, R.id.tv, objects));
//给listview设置条目的点击侦听
lv.setOnItemClickListener(new OnItemClickListener() {
//当某个条目被点击时,此方法调用
@Override
public void onItemClick(AdapterView> parent, View view,
int position, long id) {
//Activity返回时传递数据,也是通过意图对象
Intent data = new Intent();
//把要传递的数据封装至意图对象中
data.putExtra("name", objects[position]);
//当前Activity销毁时,data这个意图就会传递给启动当前Activity的那个Activity
setResult(1, data);
//销毁当前Activity
finish();
}
});
}
@Override
public void onBackPressed() {
// TODO Auto-generated method stub
super.onBackPressed();
}
}
public class CallbackActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_contact);
ListView lv = (ListView) findViewById(R.id.lv);
final String[] objects = new String[]{
"免谈,没戏,滚犊子",
"媳妇我错了,求原谅",
"老子才是一家之主"
};
lv.setAdapter(new ArrayAdapter(this, R.layout.item_listview, R.id.tv, objects));
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView> parent, View view,int position, long id) {
Intent data = new Intent();
data.putExtra("name", objects[position]);
setResult(2, data);
finish();
}
});
}
}