Activity的模式,退出应用程序
来源:互联网 发布:身材好的女友知乎 编辑:程序博客网 时间:2024/06/05 15:20
1.Activity的四种模式:
运用launchMode();选择模式。
standard:标准模式。自己可调用自己的Activity。
singleTop:顶单例模式。不可以直接调用自己,通过间接的方式获取Activtity。两个一样Activity
但地址不同。
singleTask:内单例模式。不可以直接调用自己,每调用一个Activity,除掉此Activity上面的所有
Activity.
singleInstance:全局单例模式。不可以直接调用自己,调用的Activity的栈是不同,两个Activity相互
切换。
2.退出应用程序:
递归:
main.xml:
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="jump" android:text="跳B"/>
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="exit" android:text="退出"/>main_activity:
public void jump(View view){ //跳转页面 Intent intent=new Intent(this,BActivity.class); //为目的而跳转 startActivityForResult(intent,0x200); }
public void exit(View view){ finish(); }
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); exit(null); }b.xml:
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="jump" android:text="跳A"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="exit" android:text="退出"/>
b_activity
public void jump(View view){ Intent intent=new Intent(this,MainActivity.class); setResult(0x110,intent); } public void exit(View view){ finish(); }2.application 退出应用程序
package application;import android.app.Activity;import android.app.Application;import java.util.ArrayList;import java.util.List;/** * Created by Administrator on 2017/6/17 0017. */public class Myapplication extends Application{ public List<Activity> activities=new ArrayList<>(); public void addActivity(Activity activity){ activities.add(activity); } public void exitActivity(){ for (Activity a : activities) { a.finish(); } }}
<?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="com.zking.app13.MainActivity"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/main_text" android:text="这是A页面"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="jump" android:text="跳B"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="jumpMe" android:text="跳自己"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="exit" android:text="退出"/></LinearLayout>
package com.zking.app13;import android.content.Intent;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.TextView;import application.Myapplication;public class MainActivity extends AppCompatActivity { private TextView textView; private Myapplication myapplication; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textView = (TextView) findViewById(R.id.main_text); textView.setText("当前栈"+this.getTaskId()+"\n当前Activity:"+this); myapplication = (Myapplication) getApplication(); myapplication.addActivity(this); } public void jump(View view){ Intent intent=new Intent(this,BActivity.class); startActivity(intent); } public void jumpMe(View view){ Intent intent=new Intent(this,MainActivity.class); startActivity(intent); } public void exit(View view){ myapplication.exitActivity(); } }
<?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:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/b_text" android:text="B页面"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="jump" android:text="跳A"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="exit" android:text="退出"/></LinearLayout>
package com.zking.app13;import android.app.Application;import android.content.Intent;import android.os.Bundle;import android.support.annotation.Nullable;import android.support.v7.app.AppCompatActivity;import android.view.View;import android.widget.TextView;import application.Myapplication;/** * Created by Administrator on 2017/6/17 0017. */public class BActivity extends AppCompatActivity{ private TextView textView; private Myapplication myapplication; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.b); textView = (TextView) findViewById(R.id.b_text); textView.setText("当前栈"+this.getTaskId()+"\n当前Activity:"+this); myapplication = (Myapplication) getApplication(); myapplication.addActivity(this); } public void jump(View view){ Intent intent=new Intent(this,MainActivity.class); startActivity(intent); } public void exit(View view){ myapplication.exitActivity(); }}//注意要配置自己的application
阅读全文
0 0
- Activity的模式,退出应用程序
- Activity的退出应用程序
- Android activity 的退出应用程序
- Activity启动模式和栈退出应用程序
- Android 应用程序退出和Activity的管理
- 退出activity以及结束应用程序的总结
- Android-退出应用程序的所有activity
- Activity的LaunchMode和退出应用程序
- Android-Activity的LaunchMode和退出应用程序
- 安卓Activity的退出应用程序
- Activity的LaunchMode和退出应用程序
- Activity的LaunchMode和退出应用程序
- Activity完全退出应用程序
- Activity退出应用程序
- Activity退出应用程序
- Activity的启动模式和退出
- 在某个Activity上点击返回时,退出全部的Activity并退出应用程序
- 管理activity 完全退出应用程序
- 流量时代:直播 APP 变现的洪荒之力
- curl 使用举例详解(一)
- python学习
- hdu1722--cake
- Linux多线程编程
- Activity的模式,退出应用程序
- 从零开始构建部署上线Node.js+Express+Bootstrap+Jade响应式网站应用—RSPT丨留学生活第一站(三)
- CSS选择器“~”与“+”
- Intellij IDEA bug?
- SQL Server 2005 连接到服务器失败解决方案
- C与Python函数参数传递总结
- Vue绑定事件指令之表内修改与删除
- C语言应用题——如何确定跳水排名
- make(7)