某日突然想到标题问题,一般来说返回上一个Activity,当前Activity应该自动销毁。要想保留值,便想到用bundle传递的方式

最后功能是实现了,但感觉方法很笨。

主要代码如下:

 package com.example.valuepass;

 import com.example.programdemo.R;

 import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText; public class MainActivity extends Activity {
private EditText et_phonenumber;
private Button bt_next;
private int ACTIVITY1 = 1;
private int USERNAME = 2;
private Bundle bundle = new Bundle();
private Intent intent = new Intent();
private String TAG = "MainActivity";
private String username;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et_phonenumber = (EditText) findViewById(R.id.et_phonenumber);
bt_next = (Button) findViewById(R.id.bt_next); bt_next.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v){
bundle.putString("phonenumber", et_phonenumber.getText().toString());
Log.v(TAG,et_phonenumber.getText().toString());
bundle.putString("username", username);
intent.putExtras(bundle);
intent.setClass(MainActivity.this,Username.class);
startActivityForResult(intent,ACTIVITY1);
}
});
} @Override
protected void onActivityResult(int requestCode,int resultCode,Intent data){
if(requestCode == ACTIVITY1){
if(resultCode == USERNAME){
Bundle getbundle;
getbundle = data.getExtras();
username = getbundle.getString("username");
Log.v(TAG,username);
}
}
super.onActivityResult(requestCode, resultCode, data);
} }
 package com.example.valuepass;

 import com.example.programdemo.R;

 import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView; public class Username extends Activity{
private TextView et_phonenumber;
private EditText et_username;
private Button bt_next;
private Button bt_back;
private Bundle bundle = new Bundle();
private Intent intent = new Intent();
private int USERNAME = 2;
private String TAG = "Username"; @Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.username);
et_phonenumber = (TextView)findViewById(R.id.et_phonenumber);
et_username = (EditText)findViewById(R.id.et_username);
bt_next = (Button)findViewById(R.id.bt_next);
bt_back = (Button)findViewById(R.id.bt_back);
bundle = this.getIntent().getExtras();
Log.v(TAG,bundle.getString("phonenumber"));
et_phonenumber.setText(bundle.getString("phonenumber"));
et_username.setText(bundle.getString("username")); bt_next.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v){
bundle.putString("username", et_username.getText().toString());
intent.putExtras(bundle);
intent.setClass(Username.this,Finish.class);
startActivity(intent);
}
}); bt_back.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v){
Bundle bundle_back = new Bundle();
bundle_back.putString("username", et_username.getText().toString());
Log.v(TAG,bundle_back.getString("username"));
intent.putExtras(bundle_back);
setResult(USERNAME,intent);
onBackPressed();
}
});
} }

Username.java

 package com.example.valuepass;

 import com.example.programdemo.R;

 import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView; public class Finish extends Activity{
private TextView et_phonenumber;
private TextView et_username;
private Bundle bundle = new Bundle();
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.finish); bundle = this.getIntent().getExtras();
et_phonenumber = (TextView)findViewById(R.id.et_phonenumber);
et_username = (TextView)findViewById(R.id.et_username);
et_phonenumber.setText(bundle.getString("phonenumber"));
et_username.setText(bundle.getString("username"));
} }

Finish.java

PS:后来在网上看到类似问题,有人回答:

加flag启动activity
Intent.FLAG_ACTIVITY_REORDER_TO_FRONT这个标志表示:如果这个activity已经启动了,就不产生新的activity,而只是把这个activity实例加到栈顶来就可以了。 打算有时间试一下。不过似乎要改onBackPressed();

代码下载

最新文章

  1. matlab初学之roundn和round
  2. (function($, window, document) {}) jQuery 调用解决与其他javascript库冲突的写法
  3. nginx后的tomcat获取真实用户ip
  4. 洛谷P1461 海明码 Hamming Codes
  5. Extjs load和reload的区别
  6. Django数据库配置
  7. cisco上的RIP V2加上MD5认证配置测试成功
  8. Microsoft Visual Studio Professional 2012 专业版 下载
  9. 原生 javascript 学习之 js变量
  10. String的使用
  11. 消息队列mq的原理及实现方法
  12. Linux之软链接与硬链接
  13. 关于期权池Option Pools与Vesting:码农创业防身必备法器
  14. LeetCode100.相同的树
  15. vue的router-link传参问题
  16. SPOJ.TLE - Time Limit Exceeded(DP 高维前缀和)
  17. ABAP性能和优化
  18. 3.git、TortoiseGit的安装、仓库的配置教程
  19. CountDownLatch的简单理解
  20. postman—集成到jenkins

热门文章

  1. xv6课本翻译之——第0章 操作系统接口
  2. Anaconda 用于科学计算的 Python 发行版
  3. Visual Studio 2015 和 Apache Cordova 跨平台开发入门(一)
  4. Python基本数据类型
  5. jsp 以及javabean内省技术
  6. Andrion错误解决:cannot be resolved or is not a field
  7. javascript显示实时时间
  8. 几次接触Collection排序使用总结
  9. JVM工作原理
  10. 服务器中判断客户端socket断开连接的方法
  11. netbeans 7安装xdebug调试php程序
  12. DLL模块:C++在VS下创建、调用dll
  13. 001---Hibernate简介( 开源O/R映射框架)
  14. IE8兼容border-radius.
  15. 配置国内PIP源方法
  16. nodejs 使用CAS 实现 单点登录(SSO) 【开源库实现,简单】
  17. 【朝花夕拾】四大组件之(二)Service篇
  18. 【转载】 “强化学习之父”萨顿:预测学习马上要火,AI将帮我们理解人类意识
  19. JAVA(三)JAVA常用类库/JAVA IO
  20. Validate常用校验