Android分步注册,Activity由B返回A修改再前往B,B中已填项不变
某日突然想到标题问题,一般来说返回上一个Activity,当前Activity应该自动销毁。要想保留值,便想到用bundle传递的方式
最后功能是实现了,但感觉方法很笨。
主要代码如下:
1 package com.example.valuepass; 2 3 import com.example.programdemo.R; 4 5 import android.app.Activity; 6 import android.content.Intent; 7 import android.os.Bundle; 8 import android.util.Log; 9 import android.view.View; 10 import android.view.View.OnClickListener; 11 import android.widget.Button; 12 import android.widget.EditText; 13 14 15 public class MainActivity extends Activity { 16 private EditText et_phonenumber; 17 private Button bt_next; 18 private int ACTIVITY1 = 1; 19 private int USERNAME = 2; 20 private Bundle bundle = new Bundle(); 21 private Intent intent = new Intent(); 22 private String TAG = "MainActivity"; 23 private String username; 24 @Override 25 protected void onCreate(Bundle savedInstanceState) { 26 super.onCreate(savedInstanceState); 27 setContentView(R.layout.activity_main); 28 et_phonenumber = (EditText) findViewById(R.id.et_phonenumber); 29 bt_next = (Button) findViewById(R.id.bt_next); 30 31 bt_next.setOnClickListener(new OnClickListener(){ 32 @Override 33 public void onClick(View v){ 34 bundle.putString("phonenumber", et_phonenumber.getText().toString()); 35 Log.v(TAG,et_phonenumber.getText().toString()); 36 bundle.putString("username", username); 37 intent.putExtras(bundle); 38 intent.setClass(MainActivity.this,Username.class); 39 startActivityForResult(intent,ACTIVITY1); 40 } 41 }); 42 } 43 44 @Override 45 protected void onActivityResult(int requestCode,int resultCode,Intent data){ 46 if(requestCode == ACTIVITY1){ 47 if(resultCode == USERNAME){ 48 Bundle getbundle; 49 getbundle = data.getExtras(); 50 username = getbundle.getString("username"); 51 Log.v(TAG,username); 52 } 53 } 54 super.onActivityResult(requestCode, resultCode, data); 55 } 56 57 }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 package com.example.valuepass; 2 3 import com.example.programdemo.R; 4 5 import android.app.Activity; 6 import android.content.Intent; 7 import android.os.Bundle; 8 import android.util.Log; 9 import android.view.View; 10 import android.view.View.OnClickListener; 11 import android.widget.Button; 12 import android.widget.EditText; 13 import android.widget.TextView; 14 15 public class Username extends Activity{ 16 private TextView et_phonenumber; 17 private EditText et_username; 18 private Button bt_next; 19 private Button bt_back; 20 private Bundle bundle = new Bundle(); 21 private Intent intent = new Intent(); 22 private int USERNAME = 2; 23 private String TAG = "Username"; 24 25 @Override 26 public void onCreate(Bundle savedInstanceState){ 27 super.onCreate(savedInstanceState); 28 setContentView(R.layout.username); 29 et_phonenumber = (TextView)findViewById(R.id.et_phonenumber); 30 et_username = (EditText)findViewById(R.id.et_username); 31 bt_next = (Button)findViewById(R.id.bt_next); 32 bt_back = (Button)findViewById(R.id.bt_back); 33 bundle = this.getIntent().getExtras(); 34 Log.v(TAG,bundle.getString("phonenumber")); 35 et_phonenumber.setText(bundle.getString("phonenumber")); 36 et_username.setText(bundle.getString("username")); 37 38 bt_next.setOnClickListener(new OnClickListener(){ 39 @Override 40 public void onClick(View v){ 41 bundle.putString("username", et_username.getText().toString()); 42 intent.putExtras(bundle); 43 intent.setClass(Username.this,Finish.class); 44 startActivity(intent); 45 } 46 }); 47 48 bt_back.setOnClickListener(new OnClickListener(){ 49 @Override 50 public void onClick(View v){ 51 Bundle bundle_back = new Bundle(); 52 bundle_back.putString("username", et_username.getText().toString()); 53 Log.v(TAG,bundle_back.getString("username")); 54 intent.putExtras(bundle_back); 55 setResult(USERNAME,intent); 56 onBackPressed(); 57 } 58 }); 59 } 60 61 }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 package com.example.valuepass; 2 3 import com.example.programdemo.R; 4 5 import android.app.Activity; 6 import android.os.Bundle; 7 import android.widget.TextView; 8 9 public class Finish extends Activity{ 10 private TextView et_phonenumber; 11 private TextView et_username; 12 private Bundle bundle = new Bundle(); 13 @Override 14 public void onCreate(Bundle savedInstanceState){ 15 super.onCreate(savedInstanceState); 16 setContentView(R.layout.finish); 17 18 bundle = this.getIntent().getExtras(); 19 et_phonenumber = (TextView)findViewById(R.id.et_phonenumber); 20 et_username = (TextView)findViewById(R.id.et_username); 21 et_phonenumber.setText(bundle.getString("phonenumber")); 22 et_username.setText(bundle.getString("username")); 23 } 24 25 }
PS:后来在网上看到类似问题,有人回答:
加flag启动activity
Intent.FLAG_ACTIVITY_REORDER_TO_FRONT这个标志表示:如果这个activity已经启动了,就不产生新的activity,而只是把这个activity实例加到栈顶来就可以了。
打算有时间试一下。不过似乎要改onBackPressed();
————————————————————————————————————————————————
来自企鹅娘的问候:
欢迎交流哦,如果有帮助转载的话,请务必注明出处"企鹅娘's 学习笔记",让我也小小的开心一下