Android开发案例 - 注册登录
本文只涉及UI方面的内容, 如果您是希望了解非UI方面的访客, 请跳过此文. 在微博, 微信等App的注册登录过程中有这样的交互场景(如下图):
- 打开登录界面
- 在登录界面中, 点击注册, 跳转到注册界面
- 如果取消注册, 则回退到登录界面, 如果完成注册,跳转到主页面
在整个交互场景里有个问题, 就是如何在完成注册之后关闭之前已经打开过的界面. 那如果层级更深的话, 应该如何处理?

注: 按此文分割线以下的方式处理, 存在一些问题, 如下:
> 假设注册登录之前的界面是欢迎界面, 并注册登录成功
> 这时, 以Intent.FLAG_ACTIVITY_CLEAR_TASK & Intent.FLAG_ACTIVITY_NEW_TASK方式启动主界面
> 进入应用管理强制停止应用, 再从最近使用过应用的列表进入应用
> 这时, 问题就来了, 应用没有进入欢迎界面, 而是直接进入了主界面 建议使用Intent.FLAG_ACTIVITY_CLEAR_TOP来实现注册登录的界面跳转, 大体思路, 如下:
> 设置欢迎界面的activity.launchMode属性为singleTop, 并接管onNewIntent(Intent), 若满足跳转主界面的条件, 则直接进入主界面
> 注册登录成功后, 使用Intent.FLAG_ACTIVITY_CLEAR_TOP方式跳转到欢迎界面
> 这时, 程序会运行到欢迎界面的onNewIntent(Intent), 这是因为欢迎界面现在是BackStack的栈顶activity了. 最后将直接进入主界面
知识要点
- Intent.FLAG_ACTIVITY_CLEAR_TASK
- Intent.FLAG_ACTIVITY_NEW_TASK
P.S. Intent.FLAG_ACTIVITY_CLEAR_TASK与 Intent.FLAG_ACTIVITY_CLEAR_TOP不同, 它是在API-Level-11时引入的, 使用它会清空当前task, 并让新activity成为一个空task的root activity, 另外它要求与Intent.FLAG_ACTIVITY_NEW_TASK一起使用. 而Intent.FLAG_ACTIVITY_CLEAR_TOP的调用效果同activity的singTask启动模式.
官方文档如下:
public static final int FLAG_ACTIVITY_CLEAR_TASK
Added in API level 11
If set in an Intent passed to Context.startActivity(), this flag will cause any existing task that would be associated with the activity to be cleared before the activity is started. That is, the activity becomes the new root of an otherwise empty task, and any old activities are finished. This can only be used in conjunction with FLAG_ACTIVITY_NEW_TASK.
public static final int FLAG_ACTIVITY_NEW_TASK
Added in API level 1
If set, this activity will become the start of a new task on this history stack. A task (from the activity that started it to the next task activity) defines an atomic group of activities that the user can move to. Tasks can be moved to the foreground and background; all of the activities inside of a particular task always remain in the same order. See Tasks and Back Stack for more information about tasks.
This flag is generally used by activities that want to present a "launcher" style behavior: they give the user a list of separate things that can be done, which otherwise run completely independently of the activity launching them.
When using this flag, if a task is already running for the activity you are now starting, then a new activity will not be started; instead, the current task will simply be brought to the front of the screen with the state it was last in. See FLAG_ACTIVITY_MULTIPLE_TASK for a flag to disable this behavior.
This flag can not be used when the caller is requesting a result from the activity being launched.
实现代码
> 定义
- LoginActivity - 登录界面
- RegisterActivity - 注册界面
- MainActivity - 主界面
> LoginActivity.java
略. 常规代码实现, 即直接调用startActivity来启动注册界面, 因此, 在注册界面按回退键后, 还是会回到该界面.
> RegisterActivity.java
import ...
public class RegisterActivity extends Activity {
...
/**
* 注册成功, 跳转到主界面
*/
private void finishWhenRegisterSucceeded() {
ComponentName component = new ComponentName(this, MainActivity.class);
Intent intent = IntentCompat.makeRestartActivityTask(component);
startActivity(intent);
}
}
在上述代码中, 使用了android-support-v4中的IntentCompat类, 该类封装了上述两个Intent-Flag.
到这里, 注册成功后, 进入了MainActivity后, MainActivity就是一个新task的root activity了, 而其他activity则都被清理了. 需要说明的是, finishWhenRegisterSucceeded() 没有对启动RegisterActivity的LoginActivity做任何处理或者Intent回传.
END.
Android开发案例 - 注册登录的更多相关文章
- Android开发之注册登录
昨天给大家介绍了一下关于Android端向服务器端发送数据的方法,不过貌似有一点瑕疵,今天经过调试已经解决,在这里给大家介绍一下 貌似Android4.0以后版本的对于网络权限要求变得严格,导致昨天编 ...
- AllJoyn+Android开发案例-android跨设备调用方法
AllJoyn+Android开发案例-android跨设备调用方法 项目须要涉及AllJoyn开源物联网框架.前面主要了解了一些AllJoyn主要的概念.像总线,总线附件,总线对象,总线接口这种概念 ...
- 微信小程序-----校园头条详细开发之注册登录
1.注册登录功能的实现 1.1结构 1.2 代码实现 1.2.1 为了通信的安全着想,在此我是通过小程序端获得code,然后传递给后端,在后端向微信后台发送api请求,解密,从而得到用户的唯一标示o ...
- .Net程序猿乐Android开发---(4)注册页面布局
接下来我们介绍的登陆页面布局,在本节中,我们看一下注册页面布局,页面布局大同小异,来一起熟悉下基本控件的使用方法. 效果图: 1.加入注冊页面 右键选中layout目录,加入注冊页面.例如以下图 点击 ...
- Android开发实现QQ三方登录 标签: android开发qq三方登录
本文分为两个部分:一是QQ的授权部分:二是获取用户的基本信息部分 一.授权部分 1.首先,先去腾讯开放平台获取APP ID和APP KEY(未注册腾讯开发者账号的可能需要先注册账号),获取的过程还是还 ...
- Android开发案例 设置背景图片轮播
点击按钮实现图片轮播效果 实践案例: xml <?xml version="1.0" encoding="utf-8"?> <LinearLa ...
- Android开发案例 – 在AbsListView中使用倒计时
在App中, 有多种多样的倒计时需求, 比如: 在单View上, 使用倒计时, 如(如图-1) 在ListView(或者GridView)的ItemView上, 使用倒计时(如图-2) 图-1 图-2 ...
- Android开发案例 - 淘宝商品详情
所有电商APP的商品详情页面几乎都是和淘宝的一模一样(见下图): 采用上下分页的模式 商品基本参数 & 选购参数在上页展示 商品图文详情等其他信息放在下页展示 知识要点 垂直方向的ViewPa ...
- Android开发案例 - 自定义虚拟键盘
所有包含IM功能的App(如微信, 微博, QQ, 支付宝等)都提供了Emoji表情之类的虚拟键盘, 如下图: 本文只着重介绍如何实现输入法键盘和自定义虚拟键盘的流畅切换, 而不介绍如何实现虚 ...
随机推荐
- DBCP 配置备注
<property name="initialSize" value="5"></property> <property name ...
- Phaser-游戏之旅
虽然这个小游戏逻辑不是很复杂,但为了熟悉Phaser这个游戏框架的使用方法所以就选择了它. 另外第一次在项目中尝试使用ES6,之后利用babel进行转换. 自动化构建:gulp(其他文件复制和解析) ...
- DataTable转换成IList<T>的简单实现
DataTable的无奈 很多时候,我们需要去操作DataTable.但DataTable的操作,实在是太不方便了.Linq?lambda表达式?统统没有... 特别是对现有结果集做进一步筛选,这样的 ...
- useful commands for docker beginner
You may want to add my wechat public account or add my technical blog's RSS feed This list is meant ...
- ABP源码分析二十八:ABP.MemoryDB
这个模块简单,且无实际作用.一般实际项目中都有用数据库做持久化,用了数据库就无法用这个MemoryDB 模块了.原因在于ABP限制了UnitOfWork的类型只能有一个(前文以作介绍),一般用了数据库 ...
- C++ std::deque
std::deque template < class T, class Alloc = allocator > class deque; Double ended queue deque ...
- Swift -- 对AFN框架的封装
Swift -- 对AFN框架的封装 一.封装AFN的目的 简单的说: 解耦 日常工作中,我们一般都不会去直接使用AFNetWorking来直接发送网络请求,因为耦合性太强,假设有多个控制器都使用AF ...
- express全局安装后无法通过require使用
今天入门了一下express,首先安装依赖. npm install express -g; npm install body-parser -g; npm install cookie-parser ...
- WCF学习之旅—第三个示例之三(二十九)
上接WCF学习之旅—第三个示例之一(二十七) WCF学习之旅—第三个示例之二(二十八) 在上一篇文章中我们创建了实体对象与接口协定,在这一篇文章中我们来学习如何创建WCF的服务端代码.具体步骤见下面. ...
- 在Linux Mint上安装node.js和npm
1.安装Node.js 前端开发过程中,很多项目使用npm的http-server的模块来运行一个静态的服务器,我个人在Dell的笔记本上安装的是Linux Mint最新版本,所以想尝试一下在Linu ...