Android开发学习-Activity

启停活动页面

1、启动和停止

startActivity(new Intent(原页面.this,目标页面.this));

startActivity(new Intent(this,ActFinishActivity.class))

从当前页面回到上一个页面,相当于关闭当前页面,返回代码如下:

finish();

2、生命周期

第一次进入页面时:

onCreate

onStart

onResume

===========================================================

页面跳转到另外一个Activity时:

onPause

onStop 

===========================================================

另外一个Activity再返回第一个Activity时:

onRestart

onStart

onResume

===========================================================

App返回到桌面:此时桌面应用成可见的了。桌面应用可以理解为一个Activity

onPause

onStop

onDestory

=======================================================

第一个activity快速跳转到第二个Activity,接着快速返回:

onPause,还没有来得及执行onPause

onResume,接着又重新渲染了

=======================================================

每个生命周期的状态:

1、onCreate:创建活动,把页面布局加载进内存,进入了初始化的状态。

2、onStart:开始活动,把互动页面显示在屏幕上,进入了就绪状态。

3、onResume:恢复活动,活动页面进入到活跃状态,能够与用户进行交互,例如,允许响应用户点击动作,允许用户输入文字等。

4、onPause:暂停活动,页面进入暂停状态,无法与用户正常交互

5、onStop:停止活动。页面将不在屏幕上显示

6、onDestory:销毁活动。回收活动占用的系统资源,把页面从内存中清除

7、onRestart:重启活动。重新加载内存中的页面数据。

8、onNewIntent:重用已有的活动实例。【因为玩游戏,导致应用进入到后台运行,之后因为内存不足,进程被杀死,需要重新激活】

3、启动模式

App应用先后打开两个Activity,会有一个TaskStack。返回的时候出栈操作。

AndroidManifest.xml,给activity节点添加属性

启动标志取值-xml:

android:launchMode="standard" 默认值,表示开辟一个新的任务栈,已启动的activity会被依次压入到栈中。如果activity2跳转到activity2,会被压入栈顶两次。

android:launchMode="singleTop" 如果是栈顶,则重用栈顶的实例。activity2跳转到activity2,则会被压入栈顶一次。

android:launchMode="singleTask" 当栈中存在待跳转的活动实例时,则重新创建一个新实例,并清除原实例上方的所有实例。activity1->activity2->activity3,如果想要跳转到activity1,2和3会被弹出栈。

这就是相当于singleTask,如果没有这个,则会返回好多次才会返回到桌面。

intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

登录成功之后,不会返回登录页面

设置启动标志,跳转到新页面时,栈中的原有实例被清空,同时开辟新任务的活动栈。

intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);

对于回不去的登陆页面的情况,可以设置启动标志FLAG_ACTIVITY_CLEAR_TASK,该标志会清空当前活动栈里所有实例。不过全部清空之后,意味着当前栈没办法使用了,必须另外找个活动栈才行,也就是必须同时设置启动标志FLAG_ACTIVITY_NEW_TASK,该标志用于开辟新的任务栈。

启动标志取值-java:

 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);//开辟一个新的任务栈
 intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);//当栈顶为待跳转的活动实例之时,则重用栈顶的实例
 intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);//当栈中存在待跳转的活动实例时,则重新创建一个新的实例,并清楚原实例上方的所有实例
 intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);//栈中不保存新启动的活动实例
 intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);//跳转到新页面时,栈中的原有实例都被清空

在活动之间传递消息

Intent的组成部分

元素名称设置方法说明与用途
ComponentsetComponent组件,指定意图的来源和目标
ActionsetAction

动作,指定意图的动作行为

DatasetData即Uri,指定动作要操纵的数据路径
CategoryaddCategory类别,指定意图的操作类别
TypesetType数据类型,它指定消息的数据类型
ExtrasputExtras扩展信息,指定装载的包裹信息
FlagssetFlags标志位,它指定活动的启动标志

1、显示intent和隐式intent

        显示Intent:

直接指定来源活动和目标活动,属于精确匹配。

        在intent中的构造函数中指定

        调用意图对象的setClass方法指定

        调用意图对象的setComponent方法指定

Intent intent = new Intent(this,RelativeActivity.class);
Intent intent1 = new Intent();
intent1.setClass(this,RelativeActivity.class);
ComponentName componentName = new ComponentName(this, RelativeActivity.class);
intent1.setComponent(componentName);
startActivity(intent1);
startActivity(intent);

隐式Intent:

android.intent.action.MAIN

App启动时的入口
android.intent.action.VIEW向用户展示数据
android.intent.action.SEND分享内容
android.intent.action.CALL直接拨号
android.intent.action.DIAL准备拨号
android.intent.action.SENDTO发送短信
android.intent.action.ANSWER接听电话
String phoneNo = "17864152222";
Intent intent1 = new Intent();
intent1.setAction(Intent.ACTION_DIAL);
Uri uri = Uri.parse("tel:"+phoneNo);
intent1.setData(uri);
startActivity(intent1);

APP之间跳转:

被暴露的app=要跳转的app,exported=true

<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>
            <intent-filter>
                <action android:name="android.intent.action.PSHDHX"/>
                <category android:name="android.intent.category.DEFAULT"/>
            </intent-filter>
        </activity>
app需要跳转的动作:只要action的名字对上,就能唤起app
intent.setAction("android.intent.action.PSHDHX");
intent.addCategory(Intent.CATEGORY_DEFAULT);
startActivity(intent);

2、向下一个Activity发送数据

intent使用Bundle对象存放传递的数据信息

Bundle对象操作各类数据类型的读写方法

字符串数组:getStringArray putStringArray

字符串列表:getStringArrayList putStringArrayList

可序列化结构:getSerializable putSerializable

Bundle bundle = new Bundle();
bundle.putString("requestTime",DateUtil.getNowTime());
bundle.putString("request_content","xxxxxxxxxxxxxxxxxx");
intent.putExtras(bundle);
startActivity(intent);
        Bundle bundle1 = getIntent().getExtras();
        String request_time = bundle1.getString("request_time");

3、向上一个Activity发送数据

处理好下一个页面的应答数据后,需要向上一个页面返回数据

1、上一个页面打包数据后,调用startActivityForResult方法执行此跳转动作

2、下一个页面解析请求数据,做出相应处理

3、下一个页面setResult方法返回数据包裹

5、上一个页面重写onActivityResult,解析下一个页面的返回数据

代码如下:

请求方代码

<?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"
    tools:context=".RequestResActivity">

    <TextView
        android:id="@+id/tv_req"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

    <Button
        android:id="@+id/btn_req"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="发送请求"/>

    <TextView
        android:id="@+id/res_in_req"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

</LinearLayout>
package com.pshdhx.demo2;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.pshdhx.utils.DateUtil;

import java.util.Date;

public class RequestResActivity extends AppCompatActivity implements View.OnClickListener {

    TextView tv_req;
    TextView res_in_req;
    Button btn_req;
    private String req_text = "你睡了吗,我还没有睡";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_request_res);
        tv_req = findViewById(R.id.tv_req);
        tv_req.setText("待发送的消息为:" + req_text);

        btn_req = findViewById(R.id.btn_req);
        btn_req.setOnClickListener(this);

        res_in_req = findViewById(R.id.res_in_req);


    }

    @Override
    public void onClick(View v) {
        Intent intent = new Intent(this,ResponseReqActivity.class);
        Bundle bundle = new Bundle();
        bundle.putString("req_time:", DateUtil.getNowTime());
        bundle.putString("req_content:", req_text);
        intent.putExtras(bundle);
        startActivityForResult(intent, 8888);

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode == 8888){
            if (resultCode == Activity.RESULT_OK) {
                Bundle bundle = data.getExtras();
                String res_time = bundle.getString("res_time");
                String res_content = bundle.getString("res_content");
                String desc = String.format("收到返回消息:\n应答时间为%s\n返回内容为%s", res_time, res_content);
                res_in_req.setText(desc);
            }
        }

    }
}

接收方代码:

<?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"
    tools:context=".ResponseReqActivity">


    <TextView
        android:id="@+id/tv_res"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

    <Button
        android:id="@+id/btn_res"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="返回数据"/>

    <TextView
        android:id="@+id/req_in_res"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

</LinearLayout>
package com.pshdhx.demo2;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.pshdhx.utils.DateUtil;

import org.w3c.dom.Text;

public class ResponseReqActivity extends AppCompatActivity implements View.OnClickListener {

    TextView tv_res;
    Button btn_res;
    TextView req_in_res;
    private String res_text = "我还没有睡,我爸妈不在家";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_response_req);
        tv_res = findViewById(R.id.tv_res);
        btn_res = findViewById(R.id.btn_res);
        req_in_res = findViewById(R.id.req_in_res);
        req_in_res.setText("待响应的内容为:"+res_text);
        btn_res.setOnClickListener(this);

        Bundle bundle = getIntent().getExtras();
        String req_time = bundle.getString("req_time:");
        String req_content = bundle.getString("req_content:");
        String desc = String.format("收到请求消息:\n请求时间为%s\n请求内容为%s", req_time, req_content);
        tv_res.setText(desc);
    }

    @Override
    public void onClick(View v) {
        Intent intent = new Intent();
        Bundle bundle = new Bundle();
        bundle.putString("res_time", DateUtil.getNowTime());
        bundle.putString("res_content", res_text);
        intent.putExtras(bundle);
        setResult(Activity.RESULT_OK,intent);
        finish();
    }
}

效果图:

 

踩坑的地方:

startActivityForResult(intent, 8888);

一开始我设置的是RESULT_OK,发现他的值是-1,然后onActivityResult这个方法无法接收到返回数据,不触发该方法的回调。

为活动补充附加信息

1、利用资源文件配置字符串

String appName = getString(R.string.app_name);

2、利用元数据传递配置信息

        调用getPackageManager方法获取当前应用的包管理器

        调用包管理器的getActivityInfo方法获得当前活动的信息对象

        活动信息对象的metaData是Bundle包裹类型,调用包裹对象的getString即可获得指定名称的参数值。

PackageManager packageManager = getPackageManager();
        try {
            ActivityInfo activityInfo = packageManager.getActivityInfo(getComponentName(), PackageManager.GET_META_DATA);
            Bundle bundle = activityInfo.metaData;
            String weather = bundle.getString("weather");
            Toast.makeText(MetaDataActivity.this,weather,Toast.LENGTH_SHORT).show();
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }

3、给应用界面注册快捷方式

比如,在桌面,长按支付宝,会出现扫一扫,付钱之类的快捷方式,这是android7.0之后才有的功能。

代码:

<activity android:name=".shortCutActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>

            <!--            <meta-data-->
            <!--                android:name="weather"-->
            <!--                android:value="sunny" />-->
            <meta-data
                android:name="android.app.shortcuts"
                android:resource="@xml/shortcuts" />
        </activity>

res/xml/shortcuts.xml

<?xml version="1.0" encoding="utf-8"?>
<shortcuts xmlns:android="http://schemas.android.com/apk/res/android">
    <shortcut
        android:shortcutId="first"
        android:enabled="true"
        android:icon="@mipmap/ic_launcher"
        android:shortcutLongLabel="@string/first_long"
        android:shortcutShortLabel="@string/first_short">
        <intent
            android:action="android.intent.action.VIEW"
            android:targetPackage="com.pshdhx.demo2"
            android:targetClass="com.pshdhx.demo2.RequestResActivity"
            />
        <categories android:name="android.shortcut.conversation" />
    </shortcut>
</shortcuts>

 

<resources>
    <string name="app_name">安卓学习</string>
    <string name="first_short">first</string>
    <string name="first_long">启停活动</string>
</resources>
效果:

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/350438.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

鸿蒙:@Link装饰器-父子双向同步

子组件中被Link装饰的变量与其父组件中对应的数据源建立双向数据绑定。从API version 9开始&#xff0c;该装饰器支持在ArkTS卡片中使用。 需要注意&#xff1a;Link装饰的变量与其父组件中的数据源共享相同的值。Link装饰器不能在Entry装饰的自定义组件中使用。 一、装饰器使…

基于Python对二手车之家的数据采集与分析

1.1 用户需求 1.1.1 背景与现状 基于Python的二手车之家数据采集与分析的背景与现状分析 背景&#xff1a; 随着经济的发展和人们生活水平的提高&#xff0c;二手车市场逐渐兴起。二手车之家作为中国最大的二手车交易平台之一&#xff0c;提供了丰富的二手车信息&#xff0…

下载nacos 2.3 for arm64

客户组织安全测试&#xff0c;我们系统测出了好几个高危问题&#xff0c;其中大部分是关于nacos的。 原先的nacos版本太低了&#xff0c;是1.3的。现在&#xff08;2024.01&#xff09;已经是2.3了&#xff0c;应该装个新的。我们使用docker安装nacos&#xff0c;原本很简单的…

推荐几款便宜幻兽帕鲁游戏联机服务专用云服务器

随着互联网技术的发展&#xff0c;云服务器已经成为了许多游戏联机服务的首选。如果大家想要自行搭建幻兽帕鲁联机服务器&#xff0c;那么使用云服务器是一个很好的选择。下面&#xff0c;本文将推荐几款便宜幻兽帕鲁游戏联机服务专用云服务器。 幻兽帕鲁游戏对于服务器配置要求…

Windows 上面双网卡网络,配置为优先IPV4

多数网络游戏加速器是不支持IPV6的&#xff0c;即便支持IPV6也不好用&#xff0c;原因是IPV6在大陆并不是普及的状态&#xff0c;很多资源是没有的。 所以本文会教大家怎么让双IP栈的用户&#xff0c;怎么配置优先适用IPV4&#xff0c;并且IPV6也还可以用。 跟着我的步骤来&am…

互信息的简单理解

在介绍互信息之前&#xff0c;首先需要了解一下信息熵的概念&#xff1a;所谓信息熵&#xff0c;是指信息论中对一个随机变量不确定性的度量&#xff0c;对于随机变量x&#xff0c;信息熵的定义为&#xff1a; H ( x ) − ∑ x p ( x ) l o g p ( x ) H(x)-\sum_xp(x)logp(x) …

git安装步骤

安装环境&#xff1a;Windows10 64bit 下载 Git网址 &#xff1a;Git - Downloading Package 版本&#xff1a;Git-2.21.0-64-bit 第一步&#xff1a;双击下载后的Git-2.21.0-64-bit.exe&#xff0c;开始安装 安装开始 第二步&#xff1a;选择安装路径&#xff0c;点击[next]…

【云原生】Docker基于Dockerfile多级构建,实现缩小镜像体积

目录 一、基于上次的nginx的Dockerfile做多级构建 二、基于上次的php的Dockerfile修改做多级构建 三、基于上次的mysql的Dockerfile修改做多级构建 基于以上三个镜像构建 四、镜像体积是不是越小越好&#xff1f;为什么要缩减镜像体积&#xff1f; 五、缩小镜像体积的方法…

变相体罚的行为有哪些

你是否在无意中“伤害”了学生&#xff1f;每一位老师都如同辛勤的园丁&#xff0c;努力耕耘&#xff0c;期待花开。但在这个过程中&#xff0c;有些行为&#xff0c;虽不带皮肉之苦&#xff0c;却可能给学生的心灵留下难以愈合的创伤。今天&#xff0c;就让我们来谈谈这些隐蔽…

OpenHarmony—TypeScript到ArkTS约束说明

对象的属性名必须是合法的标识符 规则&#xff1a;arkts-identifiers-as-prop-names 级别&#xff1a;错误 在ArkTS中&#xff0c;对象的属性名不能为数字或字符串。通过属性名访问类的属性&#xff0c;通过数值索引访问数组元素。 TypeScript var x { name: x, 2: 3 };c…

2024最新幻兽帕鲁服务器多少钱一个?

幻兽帕鲁服务器多少钱&#xff1f;价格便宜&#xff0c;阿里云4核16G幻兽帕鲁专属服务器32元1个月、66元3个月&#xff0c;4核32G配置113元1个月、339元3个月&#xff1b;腾讯云4核16G14M服务器66元1个月、277元3个月、1584元一年。阿腾云atengyun.com分享阿里云和腾讯云palwor…

苹果笔记本MacBook电脑怎么卸载软件?三种方法快速卸载软件

苹果笔记本MacBook电脑是一款非常流行的电脑&#xff0c;但是有时候我们可能需要卸载一些不需要的软件。下面是一些简单的步骤&#xff0c;可以帮助您在MacBook电脑上卸载软件。 苹果笔记本MacBook电脑怎么卸载软件&#xff1f;三种实用方法快速卸载软件&#xff01; 方法一&a…

【开源】基于JAVA语言的公司货物订单管理系统

目录 一、摘要1.1 项目介绍1.2 项目录屏 二、功能模块2.1 客户管理模块2.2 商品维护模块2.3 供应商管理模块2.4 订单管理模块 三、系统展示四、核心代码4.1 查询供应商信息4.2 新增商品信息4.3 查询客户信息4.4 新增订单信息4.5 添加跟进子订单 五、免责说明 一、摘要 1.1 项目…

1. MySQL 数据库

本章内容 关系型数据库基础 安装 MySQL 管理数据库和表 用户和权限管理 函数&#xff0c;存储过程&#xff0c;触发器和事件 MySQL 架构 存储引擎 服务器选项&#xff0c;系统和状态变量 优化查询和索引管理 锁和事务管理 日志管理 备份还原 MySQL 集群 压力测试…

【星海随笔】unix 启动问题记录.

启动Ubuntu操作系统时&#xff0c;直接进入GRUB状态。 调试时候&#xff0c;曾显示 no bootable device no known filesystem detected 注意&#xff1a; 目前 GRUB 分成 GRUB legacy 和 GRUB 2。版本号是 0.9x 以及之前的版本都称为 GRUB Legacy &#xff0c;从 1.x 开始的就称…

软件功能测试如何确定测试需求?CMA、CNAS软件测试报告获取

软件功能测试是为了验证软件的功能是否按照设计要求正常工作的过程&#xff0c;可以确保软件的质量&#xff0c;提高用户体验&#xff0c;也是保证软件安全和可靠性的重要一环。我们需要从多个角度对软件的各个功能模块进行测试&#xff0c;确保每个功能都能正常运行&#xff0…

75 C++对象模型探索。C++关于 虚函数表指针位置分析。C++ 面向对象和基于对象的概念。

如果一个类中&#xff0c;有虚函数&#xff0c;针对这个类会产生一个虚函数表。 生成这个类对象的时候&#xff0c;会有一个虚函数表指针&#xff0c;这个指针会指向这个虚函数表的开始地址。 我们本节就研究这个vptr指针。注意&#xff0c;vptr指针在 类对象中的位置。 证明…

AI Agents系列—— 探究大模型的推理能力,关于Chain-of-Thought的那些事儿

一、写在前面&#xff1a;关于AI Agents与CoT 本文是2023.07.24发表在同名公众号「陌北有棵树」上的一篇文章&#xff0c;个人观点是基础理论的学习现在仍是有必要的&#xff0c;所以搬运过来。 今天要读的论文是《Chain-of-Thought Prompting Elicits Reasoning in Large La…

【C++】C++入门基础讲解(一)

&#x1f497;个人主页&#x1f497; ⭐个人专栏——C学习⭐ &#x1f4ab;点击关注&#x1f929;一起学习C语言&#x1f4af;&#x1f4ab; 导读 经过一段时间的C语言学习&#xff0c;我们以及基本掌握了C语言的知识&#xff0c;今天&#xff0c;我们就开始学习C&#xff0c;…