Android四大组件
1、Activity
1.1、 初始Activity
-
Activity 是什么? -
Activity有什么作用?
- 用户和应用程序交互的接口
- 摆放各种空间的容器
-
怎样创建Activity?
-
继承Activity -
重写onCreate()方法 -
为Activity提供布局xml文件 -
清单文件中配置
-
创建OtherActivity类,并继承Activity类 package com.tinno.createactivity;
import android.app.Activity;
import android.os.Bundle;
import androidx.annotation.Nullable;
public class OtherActivity extends Activity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_other);
}
}
-
为OtherActivity类提供布局文件xml文件activity_other.xml <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/tv_show"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/tv_show"
android:textSize="25sp"
android:textColor="#00AA00"/>
</RelativeLayout>
-
在配置文件中配置,使OtherActivity启动 AndroidManifest.xml <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.tinno.createactivity">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.TencentClass">
<activity
android:name=".MainActivity"
android:exported="true">
</activity>
<activity android:name=".OtherActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
-
启动服务 出现绿色文字 (我是OtherActivity) -
两个Activity之间的跳转 MainActivity.java ==> 使用意图进行跳转 package com.tinno.createactivity;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onClick(View view){
Intent intent = new Intent(MainActivity.this,OtherActivity.class);
startActivity(intent);
}
}
MainActivity 的布局文件 activity_main.xml <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="点击跳转"
android:onClick="onClick"
tools:ignore="OnClick" />
</RelativeLayout>
配置 AndroidManifest.xml <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.tinno.createactivity">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.TencentClass">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".OtherActivity">
</activity>
</application>
</manifest>
-
启动服务 点击跳转 --> 显示 我是OtherActivity
1.2 、Activity的生命周期
- 什么是生命周期?
- 研究Activity的生命周期有什么作用?
- Activity生命周期的执行顺序?
- 横竖屏切换时Activity生命周期的如何变化?
1、Activiy生命周期的方法
Method | Description | Killable | Next |
---|
onCreate | 当启动新的Activity的时候调用 | No | onStart() | onStart | 当Activity对用户即将可见时调用 | No | onResume() or onStop() | onResume | 当Activity界面可与用户交互时调用 | No | onPause() | onPause | 当系统要启动一个其他的Activity时调用,用户保存当前数据 | Yes | onResume() or onStop() | onStop | 该Activity已经不可见时调用 | Yes | onRestart() or onDestroy() | onRestart | 重新启动Activity时调用(此方法是重启留在缓存中的Activity) | No | onStart() | onDestroy | 当Activity被finish或手机内存不足被销毁时候调用 | Yes | nothing |
- onCreate():表示 activity 被创建调用的方法
- onStart():表示activity能够被用户看到时回调的方法
- onResume():表示activity获取用户焦点时 能与用户交互时调用
- onPause():表示activity失去用户焦点时回调的方法
- onStop():表示activity被完全遮挡时回调的方法
- onRestart():表示activity处于停止状态重新被启动时回调的方法
- onDestroy():表示activity被销毁时回调的方法
2、Activity生命周期执行顺序
3、横竖屏切换时Activity生命周期如何变化?
默认情况下 Activity 会关闭并且重新启动
- 表示设置activity固定方向:
android:screenOrientation="portrait" - 表示activity横竖屏切换时不会调用生命周期函数 (4.0版本之后):
设置 android:configChanges 属性 --> orientation、keyboardHidden、screenSize 多个属性值用 | 分隔
<activity android:name=".OtherActivity"
android:configChanges="orientation|keyboardHidden|screenSize">
</activity>
1.3、Activity之间的传值
- Activity之间通过Intent传值
- Activity之间通过Bundle传值
- 使用Application全局对象传值
- 启动Activity回传数据
1.3.1、Activity之间通过 Intent 传值
使用putExtra() 进行发送方的数据传递
getStringExtra()、getIntExtra()、getCharExtra() … 等方法进行接收方的数据获取
发送方:
? 以 key - value 对的形式存储需要传递的数据
接收方:
? 获取激活的 Intent 对象,根据 key 获取传递数据
-
定义两个Activity类,并为连个Activity类编写布局文件: MainActivity.java package com.tinno.activitypassvalue;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void send(View view){
Intent intent = new Intent(this,ResultActivity.class);
intent.putExtra("姓名","张三");
intent.putExtra("性别",'男');
intent.putExtra("年龄",30);
intent.putExtra("成绩",98.5);
intent.putExtra("bl",true);
startActivity(intent);
}
}
activity_main.xml <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="点击传值"
android:onClick="send"/>
</LinearLayout>
ResultActivity.java package com.tinno.activitypassvalue;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
import androidx.annotation.Nullable;
public class ResultActivity extends Activity {
private TextView tv;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_result);
tv = (TextView) findViewById(R.id.tv_show);
Intent intent = getIntent();
String name = intent.getStringExtra("姓名");
int age = intent.getIntExtra("年龄",0);
double score = intent.getDoubleExtra("成绩",0.0);
char sex = intent.getCharExtra("性别",'男');
boolean bl = intent.getBooleanExtra("bl",false);
tv.setText("name: " + name + "\n age: " + age + "\n score: " + score + "\n sex: " + sex + "\n bl: " + bl);
}
}
activity_result.xml <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/tv_show"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="25sp"
android:textColor="#AA0000"
android:layout_gravity="center"
android:text="这是ResultActivity的布局文件"/>
</LinearLayout>
-
将 Activity 在 AndroidManifest.xml 中进行注册 <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.tinno.activitypassvalue">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.TencentClass">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".ResultActivity">
</activity>
</application>
</manifest>
-
运行结果 : 点击传值 按钮 --> 数据显示
1.3.2、Activity之间通过Bundle传值
1.3.2.1、数据传递方:
- 新建一个Bundle类
- Bundle类中 key - value 键值对的形式存储数据
- 创建一个 Intent 对象,将 Bundle 存入 Intent 对象
1.3.2.2、数据接收方
- 获取激活的 Intent 对象
- 获取传递的 Bundle 对象
- 根据 Bundle 中的 key 值获取指定的 value 值
1.3.2.3、步骤如 1.3.1
-
**定义两个Activity类,并为连个Activity类编写布局文件: ** MainActivity.java package com.tinno.activitypassvaluebundle;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void send(View view){
Intent intent = new Intent(this,ResultActivity.class);
Bundle bundle = new Bundle();
bundle.putString("name","李四");
bundle.putInt("age",28);
bundle.putDouble("score",95.6);
bundle.putChar("sex",'女');
intent.putExtras(bundle);
startActivity(intent);
}
}
activity_main.xml <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="点击传值"
android:onClick="send"/>
</RelativeLayout>
ResultActivity.java package com.tinno.activitypassvaluebundle;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
import androidx.annotation.Nullable;
public class ResultActivity extends Activity {
private TextView tv_showInfo;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_result);
tv_showInfo = (TextView) findViewById(R.id.tv_showInfo);
Intent intent = getIntent();
Bundle bundle = intent.getExtras();
String name = bundle.getString("name");
int age = bundle.getInt("age");
double score = bundle.getDouble("score");
char sex = bundle.getChar("sex");
tv_showInfo.setText("name: " + name +"\n age: " + age + "\n score: " + score +"\n sex: " + sex);
}
}
activity_result.xml <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/tv_showInfo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="32sp"
android:textColor="#AA00AA"
android:textStyle="bold"/>
</LinearLayout>
-
将 Activity 在 AndroidManifest.xml 中进行注册 <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.tinno.activitypassvaluebundle">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.TencentClass">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".ResultActivity">
</activity>
</application>
</manifest>
-
运行结果 : 点击传值 按钮 --> 数据显示
1.3.3、使用 Application 全局对象传值
发送方
? 将数据存储到 Application
接收方
? 读取 Application 中的数据
注意 : Application 需要注册和配置
1、创建类继承MyAppliction继承Appliction,将需要存储的数据定义为application的属性 2、在发送的activity 中发送数据 3、接收的 activity 中,获取数据操作 4、需要进行响应的注册(在AndroidManifest.xml文件中进行注册)
-
定义两个Activity类,并为两个Activity类编写布局文件: MainActivity.java package com.tinno.activitypassvalueappliaction;
import androidx.appcompat.app.AppCompatActivity;
import android.app.Application;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends AppCompatActivity {
private MyApplication application;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void click(View view){
application = (MyApplication) getApplication();
application.setName("小明");
application.setAge(18);
startActivity(new Intent(this,ResultActivity.class));
}
}
activity_main.xml <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="点击传值"
android:onClick="click"
android:layout_gravity="center"/>
</LinearLayout>
ResultActivity.java package com.tinno.activitypassvalueappliaction;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
import androidx.annotation.Nullable;
public class ResultActivity extends Activity {
private TextView tv;
private MyApplication application;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_result);
tv = (TextView) findViewById(R.id.tv);
application = (MyApplication) getApplication();
String name = application.getName();
int age = application.getAge();
tv.setText("name: " + name + "\n age: " + age);
}
}
activity_result.xml <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#00AA00"
android:textSize="32sp"
android:text="你好"
android:layout_gravity="center"/>
</LinearLayout>
-
定义一个自定义类 MyApplication 继承自 Application,用来作为传值的实体类 MyApplication package com.tinno.activitypassvalueappliaction;
import android.app.Application;
public class MyApplication extends Application {
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
-
在AndroidManifest.xml 对目标Activity进行注册和 application 进行注册 <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.tinno.activitypassvalueappliaction">
<application
android:name=".MyApplication"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.TencentClass">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".ResultActivity">
</activity>
</application>
</manifest>
-
结果 -> 点击传值 按钮跳转到 ResultActivity中,并获取数据
1.3.4、启动Activity回传数据
-
定义两个Activity类,并为两个Activity类编写布局文件: MainActivity.java package com.tinno.activitypassforresult;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private EditText et_num1,et_num2;
private TextView tv_result;
private static final int REQUEST_CODE=1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et_num1 = (EditText) findViewById(R.id.et_num1);
et_num2 = (EditText) findViewById(R.id.et_num2);
tv_result = (TextView) findViewById(R.id.et_res);
}
public void onClick(View view){
Intent intent = new Intent(this,ResultActivity.class);
String num1 = et_num1.getText().toString();
String num2 = et_num2.getText().toString();
intent.putExtra("num1",num1);
intent.putExtra("num2",num2);
startActivityForResult(intent,REQUEST_CODE);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_CODE && resultCode == Activity.RESULT_OK){
String result = data.getStringExtra("info");
Log.e("xx", "onActivityResult: " + result );
tv_result.setText(result);
}
}
}
activity_main.xml <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical">
<EditText
android:id="@+id/et_num1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:textSize="20sp"
android:ems="3"/>
<TextView
android:id="@+id/et_add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@id/et_num1"
android:layout_alignBottom="@id/et_num1"
android:layout_toRightOf="@id/et_num1"
android:textSize="20sp"
android:text="+"/>
<EditText
android:id="@+id/et_num2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@id/et_add"
android:layout_alignBottom="@id/et_add"
android:layout_toRightOf="@id/et_add"
android:textSize="20sp"
android:ems="3"/>
<TextView
android:id="@+id/et_amount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@id/et_num2"
android:layout_alignBottom="@id/et_num2"
android:layout_toRightOf="@id/et_num2"
android:textSize="20sp"
android:text="="/>
<TextView
android:id="@+id/et_res"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@id/et_amount"
android:layout_alignBottom="@id/et_amount"
android:layout_toRightOf="@id/et_amount"
android:textSize="20sp"
android:text="?"/>
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/et_res"
android:layout_marginTop="22dp"
android:layout_toRightOf="@id/et_res"
android:onClick="onClick"
android:text="计算结果"/>
</RelativeLayout>
ResultActivity.java package com.tinno.activitypassforresult;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import androidx.annotation.Nullable;
public class ResultActivity extends Activity {
private TextView tv_num1,tv_num2;
private EditText et_result;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_result);
tv_num1 = (TextView) findViewById(R.id.textView1);
tv_num2 = (TextView) findViewById(R.id.textView3);
et_result = (EditText) findViewById(R.id.editText1);
Intent intent = getIntent();
tv_num1.setText(intent.getStringExtra("num1"));
tv_num2.setText(intent.getStringExtra("num2"));
}
public void send(View view){
String result = et_result.getText().toString();
Log.e("xx", "获取的结果为: " + result );
Intent intent = new Intent();
intent.putExtra("info",result);
setResult(Activity.RESULT_OK,intent);
ResultActivity.this.finish();
}
}
activity_result.xml <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="20dp"
android:layout_marginTop="19dp"
android:textSize="20sp"
android:text="*"/>
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@id/textView1"
android:layout_marginLeft="28dp"
android:layout_toRightOf="@id/textView1"
android:textSize="20sp"
android:text="+"/>
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@id/textView2"
android:layout_marginLeft="28dp"
android:layout_toRightOf="@id/textView2"
android:textSize="20sp"
android:text="*"/>
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@id/textView3"
android:layout_marginLeft="28dp"
android:layout_toRightOf="@id/textView3"
android:textSize="20sp"
android:text="="/>
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@id/textView4"
android:layout_alignParentRight="true"
android:layout_marginRight="32dp"
android:ems="3"/>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@id/editText1"
android:layout_below="@id/editText1"
android:layout_marginTop="30dp"
android:onClick="send"
android:text="回传数据"/>
</RelativeLayout>
-
在Androidmanifest.xml 进行注册 <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.tinno.activitypassforresult">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.TencentClass">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".ResultActivity">
</activity>
</application>
</manifest>
-
结果 ==> 在MainActivity中输入两个数字,点击计算结果 按钮 跳转到 ResultActivity ,在ResultActivity进行结果计算,点击回传数据 按钮,将数据传回到 MainActivity 中
1.4、Task、Back Stack
Task:
多个Activity一起完成一项工作时,它们的集合被称作一个Task。
Back Stack:
一个Task的所有的Activity被放置在一个stack结构 中,根据他们启动的顺序被添加。Stack不会进行重新排列,只会在打开新Activity时添加其到栈顶,或finish时从栈顶移除。所以Activity在此stack中表现为“last in,fisrt out”,因为上述特点,多个Activity在打开和关闭时,stack表现出“回退栈”这样的效果
https://blog.csdn.net/ckq5254/article/details/79474827
-
定义两个Activity类,并为两个Activity类编写布局文件: MainActivity.java package com.tinno.taskdemo;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void click(View view){
startActivity(new Intent(this,OtherActivity.class));
}
}
activity_main.xml <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="点击启动OtherActivity"
android:layout_alignParentRight="true"
android:layout_alignParentLeft="true"
android:onClick="click"/>
</RelativeLayout>
OtherActivity.java package com.tinno.taskdemo;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Settings;
import android.view.View;
import androidx.annotation.Nullable;
public class OtherActivity extends Activity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_other);
}
public void click(View view){
Intent intent = new Intent(Settings.ACTION_SETTINGS);
startActivity(intent);
}
}
activity_other.xml <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="点击启动设置页面"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:onClick="click"/>
</RelativeLayout>
-
在Androidmanifest.xml 进行注册 <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.tinno.taskdemo">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.TencentClass">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".OtherActivity">
</activity>
</application>
</manifest>
-
结果 点击启动OTHERACTIVITY 按钮跳转到 OtherActivity中,在点击 点击启动设置页面 按钮 跳转到 系统设置页面
|