多个activity跳转保留内存使用intent传递数据问题_新手
/////本来是做的activity跳转,普通那种,但是会在调回来会销毁原来的,重新调用onCreate方法,
后来参考【http://blog.csdn.net/qq_26918031/article/details/52749685】,给intent设置flag,,FLAG_ACTIVITY_REORDER_TO_FRONT,
状态是保留了,但bundle数据传递布料,‘本来想使用Application存储全局的,【参照http://blog.csdn.net/li12412414/article/details/51867400】
但是翻看到疯狂android讲义第三版286页,设置了intent的flag,,,,,FLAG_ACTIVITY_BROUGHT_TO_FRONT,,,
就可以在再次启动后的onStart,onResume中找到bundle了,,,,,

测试文件,
布局1:
<?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="match_parent"
android:layout_height="wrap_content"
android:text="数据展示1"
/>
<Button
android:id="@+id/btn_t1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="下"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="aaa"
/>
<TextView
android:id="@+id/tv_1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="show"
/>
</LinearLayout>
布局2:
<?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="match_parent"
android:layout_height="wrap_content"
android:text="数据展示2"
/>
<Button
android:id="@+id/btn_t2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="上"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="111"
/>
<TextView
android:id="@+id/tv_2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="show"
/>
</LinearLayout>
对应的activity1
package com.example.administrator.no1;
//配置日志过滤【参照:http://blog.csdn.net/hyr83960944/article/details/38268395】
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.Button;
import android.widget.TextView;
/**
* Created by Administrator on 2017/10/10 0010.
*/
public class TestActivityLife1 extends AppCompatActivity {
Button button;
TextView textView;
int j=1;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.e("zzf","oncreate"+j++);
setContentView(R.layout.test_a1);
button = (Button) findViewById(R.id.btn_t1);
textView = (TextView) findViewById(R.id.tv_1);
button.setOnClickListener(v -> {
Intent intent = new Intent(TestActivityLife1.this,TestActivityLife2.class);
Bundle bundle = new Bundle();
bundle.putString("a","123");
intent.putExtra("bun",bundle);
intent.setFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
startActivity(intent);
});
// getApplicationContext().put
}
@Override
protected void onStart() {
super.onStart();
// System.out.println("start"+j++);
Log.e("zzf","start"+j+++""+getIntent().getBundleExtra("bun"));
}
@Override
protected void onStop() {
super.onStop();
// System.out.println("stop"+j++);
Log.e("zzf","stop"+j++);
}
@Override
protected void onDestroy() {
super.onDestroy();
// System.out.println("destroy"+j++);
Log.e("zzf","destroy"+j++);
}
@Override
protected void onPause() {
super.onPause();
// System.out.println("zzf:pause"+j++);
Log.e("zzf","pause"+j++);
}
@Override
protected void onResume() {
super.onResume();
// System.out.println("resume"+j++);
Log.e("zzf","resume"+j+++""+getIntent().getBundleExtra("bun"));
if (getIntent().getBundleExtra("bun") != null){
Log.e("zzf","res111回馈数据suc:"+getIntent().getBundleExtra("bun").get("name"));
}
}
@Override
protected void onRestart() {
super.onRestart();
Log.e("zzf","restart"+j+++""+getIntent().getBundleExtra("bun"));
}
}
activity2
package com.example.administrator.no1;
///设置了FLAG_ACTIVITY_REORDER_TO_FRONT标志后,虽然保留内存,当时bundle数据传递就没用了
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.Button;
import android.widget.TextView;
/**
* Created by Administrator on 2017/10/10 0010.
*/
public class TestActivityLife2 extends AppCompatActivity {
Button button;
TextView textView;
Intent intent1;
int j;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.e("zzf","oncreate"+j++);
setContentView(R.layout.test_a2);
button = (Button) findViewById(R.id.btn_t2);
textView = (TextView) findViewById(R.id.tv_2);
intent1 = getIntent();
button.setOnClickListener(v -> {
Intent intent = new Intent(TestActivityLife2.this,TestActivityLife1.class);
Bundle bundle = new Bundle();
bundle.putString("name","zzf");
intent.putExtra("bun",bundle);
intent.setFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
startActivity(intent);
});
}
@Override
protected void onStart() {
super.onStart();
// System.out.println("start"+j++);
Log.e("zzf","start"+j++);
}
@Override
protected void onStop() {
super.onStop();
// System.out.println("stop"+j++);
Log.e("zzf","stop"+j++);
}
@Override
protected void onDestroy() {
super.onDestroy();
// System.out.println("destroy"+j++);
Log.e("zzf","destroy"+j++);
}
@Override
protected void onPause() {
super.onPause();
// System.out.println("zzf:pause"+j++);
Log.e("zzf","pause"+j++);
}
@Override
protected void onResume() {
super.onResume();
// System.out.println("resume"+j++);
Log.e("zzf","resume"+j++);
}
}
多个activity跳转保留内存使用intent传递数据问题_新手的更多相关文章
- Android Activity传递数据使用getIntent()接收不到,揭秘Intent传递数据与Activity启动模式singleTask的关系。
activity通过intent传递数据的时候,如果activity未启动,那么在这个刚启动的activity里通过getIntent()会获取到这个intent的数据.. 如果要启动的activit ...
- Android Intent传递数据
刚开始看郭大神的<>,实现以下里面的一些例子.Intent传递数据. 我们利用显示的方式进行Intent的启动. 1.启动intent并输入数据. Intent intent=new In ...
- 【转】Android 之最新最全的Intent传递数据方法
原文地址:https://www.jianshu.com/p/1169dba99261 intent传递数据 为什么要和intent单独拿出来讲,因为Intent传递数据也是非常重要的 一.简单的传递 ...
- Intent传递数据从一个Activity到另一个Activity
MainActivity package com.test.intentdemo; import android.app.Activity; import android.content.Intent ...
- Android学习之基础知识四-Activity活动4讲(Intent传递数据)
Intent除了可以开启一个活动,还能在启动活动的时候传递数据,此时Intent相当于一个保存数据的库,我们先把数据保存在Intent中,然后再根据各个activity的需要从其中取出数据. 一.使 ...
- Android 消息广播Intent传递数据
1.创建布局文件activity_broadcast.xml <RelativeLayout xmlns:android="http://schemas.android.com/apk ...
- Intent传递数据
方式比较多,先看看代码,一会儿再总结. activity_main.xml <RelativeLayout xmlns:android="http://schemas.android. ...
- Android 开发中使用Intent传递数据的方法
Activity之间通过Intent传递值,支持基本数据类型和String对象及 它们的数组对象byte.byte[].char.char[].boolean.boolean[].short.shor ...
- Android学习之Intent传递数据
Intent在Activity中的作用主要是有两个: 1.启动目标Activity 2.传递数据 Intent在传递数据时分两种情况:向下一个Activity传递数据和从下一个Activity返回数据 ...
随机推荐
- 关于Java的发展前景
各位看官觉得Java还能火几年?未来的发展方向是什么?
- 通过ssh协议实现用户key认证登录
author:JevonWei 版权声明:原创作品 用户实现key认证登录 主机A 192.168.198,134 主机B 192.168.198,131 主机C 192.168.198,136 创建 ...
- Java double和 float丢失精度问题
详见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcyt357 由于对float或double 的使用不当,可能会出现精度丢失的问题. ...
- C# 导出Excel的示例(转)
using System; using System.Collections.Generic; using System.Text; using System.Data; using System.W ...
- spring boot认识
Spring Boot的好处: 1.配置简化 2.配合各种starter使用,基本上可以做到自动化配置 3.上手速度快 4.提供运行时的应用监控 运用IDEA创建spring boot项目请查看: h ...
- unity3d在菜单栏,一键设置Player setting及自动打包并设置apk的存储位置
项目进行中,领导要求能够进行一键设置Player settings及自动打包并设置apk的位置,所以自己就上网搜索了很多大神的文章.最后是完成了领导需要的功能,在这里记录并分享一下(此项指针对安卓ap ...
- POJ 3463 最(次)短路条数
Sightseeing Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 9497 Accepted: 3340 Descr ...
- 串口数据缓存java版
接触串口很久了,一直以来将都是将串口读取出来的数组转换成字符串通过string.contains()查找是否包涵目标数组,自己感觉low到爆,所以写了一个byte-buffer,测试还是蛮好用的.希望 ...
- 201521123027 <java程序设计>第十二周作业总结
1.本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结多流与文件相关内容. 2.书面作业 将Student对象(属性:int id, String name,int age,double ...
- java进程/线程;堆和栈;多线程
一.进程和线程 进程:在内存中运行的应用程序,一个exe是一个进程. 如:ps -exf 可以查看各个应用的进程,其中ppid为父进程: ps aux | egrep '(cron|syslog)' ...