Activity的跳转及Activity之间的数据传递(作业)
首先呢,看到这个作业第一感觉并不难,因为只是涉及到多activity之间的跳转以及简单的数据传递而已。然后,做的时候也没花费多长的时间,做好之后,发现了一个BUG,那就是activity跳转的时候造成activity的残留问题。这次的作业只有两个activity,但是在反复进行跳转操作的时候之前的activity并不会被结束掉,因为跳转的时候是开启一个新的activity,而不是跳转回原来的那个activity。就是因为这样,在两个activity之间进行反复跳转操作的时候,之前的未结束,又开启了一个新的activity,比如说有activity A和B,反复操作的时候就会造成ABABABABABA........这样就使得整个程序用起来很不爽!当然,这个问题被我顺利的解决了,哈哈。然后还遇到了一个小小的问题,就是activityB向activityA跳转的时候要抓取B传送的数据,所以要在A的onCreate里写,但是当没有B的时候,就会抛出异常,造成程序闪退,然后我还是用了一个try-catch的方法给避免掉了,好了,话就说这么多,下面看代码!
首先是布局文件:activity_main和activiyt_test
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.a15083.hwork4_7.MainActivity">
<EditText
android:id="@+id/et_name"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="center_horizontal|bottom"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/bt_test"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_weight="1"
android:onClick="onClick"
android:text="@string/bt_test"/>
<Button
android:id="@+id/bt_exit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:layout_weight="1"
android:onClick="onClick"
android:text="@string/bt_exit"
/>
</LinearLayout>
<TextView
android:id="@+id/tv_result"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="center_horizontal|top"
/>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.a15083.hwork4_7.TestActivity">
<TextView
android:id="@+id/tv_qua"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="center_horizontal|bottom"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal">
<Button
android:id="@+id/bt_YES"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_weight="1"
android:onClick="onClick"
android:text="@string/bt_yes" />
<Button
android:id="@+id/bt_NO"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:layout_weight="1"
android:onClick="onClick"
android:text="@string/bt_no" />
</LinearLayout>
</LinearLayout>
界面效果:
首先是MainActivity的功能实现代码:
package com.example.a15083.hwork4_7;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private EditText et_name;
private TextView tv_result;
public static MainActivity mainActivity = null; //创建一个ManiActivity的类变量用来在TestActivity中结束这个Activity
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et_name=(EditText) findViewById(R.id.et_name);
tv_result=(TextView) findViewById(R.id.tv_result);
mainActivity =this; //将类变量的值赋值为这个activity
try {
TestActivity.testActivity.finish();
Intent intent = getIntent();
String result = intent.getStringExtra("result");
String name=intent.getStringExtra("name");
et_name.setText(name);
tv_result.setText(result);
}catch (Exception e){
}
}
public void onClick(View view){
switch (view.getId()){
case R.id.bt_test:
String name = et_name.getText().toString();
Intent intent = new Intent(MainActivity.this,TestActivity.class);
Bundle bundle = new Bundle();
bundle.putString("qua",name);
intent.putExtras(bundle);
startActivity(intent);
break;
case R.id.bt_exit: //点击退出时将两个activity全都结束
finish();
TestActivity.testActivity.finish();
break;
}
}
}
package com.example.a15083.hwork4_7;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private EditText et_name;
private TextView tv_result;
public static MainActivity mainActivity = null; //创建一个ManiActivity的类变量用来在TestActivity中结束这个Activity
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et_name=(EditText) findViewById(R.id.et_name);
tv_result=(TextView) findViewById(R.id.tv_result);
mainActivity =this; //将类变量的值赋值为这个activity
try {
TestActivity.testActivity.finish();
Intent intent = getIntent();
String result = intent.getStringExtra("result");
String name=intent.getStringExtra("name");
et_name.setText(name);
tv_result.setText(result);
}catch (Exception e){
}
}
public void onClick(View view){
switch (view.getId()){
case R.id.bt_test:
String name = et_name.getText().toString();
Intent intent = new Intent(MainActivity.this,TestActivity.class);
Bundle bundle = new Bundle();
bundle.putString("qua",name);
intent.putExtras(bundle);
startActivity(intent);
break;
case R.id.bt_exit: //点击退出时将两个activity全都结束
finish();
TestActivity.testActivity.finish();
break;
}
}
}
然后是TestActivity里的功能实现代码:
package com.example.a15083.hwork4_7;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
public class TestActivity extends AppCompatActivity {
private TextView tv_qua;
private String name;
public static TestActivity testActivity = null; //创建一个TestActivity的类变量用来在MainActivity中结束这个Activity
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
tv_qua = (TextView) findViewById(R.id.tv_qua);
testActivity =this; //将类变量的值赋值为这个activity
MainActivity.mainActivity.finish();
Bundle bundle = getIntent().getExtras();
name = bundle.getString("qua");
tv_qua.setText(name+"同学学习android有没有信心?");
}
public void onClick(View view){
Intent intent = new Intent(TestActivity.this,MainActivity.class);
switch (view.getId()){
case R.id.bt_YES:
intent.putExtra("name",name);
intent.putExtra("result","评估内容返回为:有");
startActivity(intent);
break;
case R.id.bt_NO:
intent.putExtra("name",name);
intent.putExtra("result","评估内容返回为:没有");
startActivity(intent);
break;
}
}
}
package com.example.a15083.hwork4_7;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
public class TestActivity extends AppCompatActivity {
private TextView tv_qua;
private String name;
public static TestActivity testActivity = null; //创建一个TestActivity的类变量用来在MainActivity中结束这个Activity
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
tv_qua = (TextView) findViewById(R.id.tv_qua);
testActivity =this; //将类变量的值赋值为这个activity
MainActivity.mainActivity.finish();
Bundle bundle = getIntent().getExtras();
name = bundle.getString("qua");
tv_qua.setText(name+"同学学习android有没有信心?");
}
public void onClick(View view){
Intent intent = new Intent(TestActivity.this,MainActivity.class);
switch (view.getId()){
case R.id.bt_YES:
intent.putExtra("name",name);
intent.putExtra("result","评估内容返回为:有");
startActivity(intent);
break;
case R.id.bt_NO:
intent.putExtra("name",name);
intent.putExtra("result","评估内容返回为:没有");
startActivity(intent);
break;
}
}
}
在解决残留的activity时候我是分别给每个activity设置了一个该activity类型的类变量,然后在其各自的onCreate方法里赋值为自己本身这个activity。
public static MainActivity mainActivity = null;
mainActivity =this;
public static TestActivity testActivity = null;
testActivity =this;
public static MainActivity mainActivity = null;
mainActivity =this;
public static TestActivity testActivity = null;
testActivity =this;
这样设置好之后,在相应的位置就能结束掉跳转之前存在的那个activity了。
2017年4月16日第二次编辑
上面是第一次编辑的,结束activity的方法太麻烦了。
测试了一下,发现直接在startActivity后面加上finish()就可以了解决activity残留的问题,真是尴尬啊......
public void onClick(View view){
switch (view.getId()){
case R.id.bt_test:
String name = et_name.getText().toString();
Intent intent = new Intent(MainActivity.this,TestActivity.class);
Bundle bundle = new Bundle();
bundle.putString("qua",name);
intent.putExtras(bundle);
startActivity(intent);
finish();
break;
public void onClick(View view){
switch (view.getId()){
case R.id.bt_test:
String name = et_name.getText().toString();
Intent intent = new Intent(MainActivity.this,TestActivity.class);
Bundle bundle = new Bundle();
bundle.putString("qua",name);
intent.putExtras(bundle);
startActivity(intent);
finish();
break;
上过周四的课之后又重新写的一个小例子
上面写的第一个例子,有一个很不好的地方,就是由于每次都是开启新的activity,所以原有的数据会被清除掉,所以我还写了代码让新开启的activity显示之前输入的名字,但是上过周四的课之后才发现我之前用的方法真的是太笨了下面是这周四学到的方法,activity不是一直开启新的而是进行返回,这样原来的数据还会被保存。
输入名字进行测试的代码
package com.example.a15083.fragmentandactivity;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
public class SendActivity extends AppCompatActivity implements View.OnClickListener{
private EditText et_name;
private TextView tv_result;
private static final int REQUEST_CODE = 1000;
public static final int RESULT_CODE= 1001;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_send);
et_name = (EditText) findViewById(R.id.et_name);
tv_result =(TextView) findViewById(R.id.tv_result);
}
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.bt_test:
Intent intent = new Intent(this,ReceiveActivity.class);
String name = et_name.getText().toString();
intent.putExtra("name",name);
startActivityForResult(intent,REQUEST_CODE);
break;
case R.id.bt_exit:
finish();
break;
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode==REQUEST_CODE && resultCode==RESULT_CODE){
String result_value = data.getStringExtra("result");
tv_result.setText(result_value);
}
}
}
package com.example.a15083.fragmentandactivity;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
public class SendActivity extends AppCompatActivity implements View.OnClickListener{
private EditText et_name;
private TextView tv_result;
private static final int REQUEST_CODE = 1000;
public static final int RESULT_CODE= 1001;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_send);
et_name = (EditText) findViewById(R.id.et_name);
tv_result =(TextView) findViewById(R.id.tv_result);
}
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.bt_test:
Intent intent = new Intent(this,ReceiveActivity.class);
String name = et_name.getText().toString();
intent.putExtra("name",name);
startActivityForResult(intent,REQUEST_CODE);
break;
case R.id.bt_exit:
finish();
break;
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode==REQUEST_CODE && resultCode==RESULT_CODE){
String result_value = data.getStringExtra("result");
tv_result.setText(result_value);
}
}
}
结果返回的代码:
package com.example.a15083.fragmentandactivity;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
public class ReceiveActivity extends AppCompatActivity implements View.OnClickListener {
private TextView tv_qua;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_receive);
tv_qua=(TextView) findViewById(R.id.tv_qua);
Intent intent = getIntent();
String name = intent.getStringExtra("name");
tv_qua.setText(name+"同学:学习Android有没有信心?");
}
@Override
public void onClick(View view) {
Intent intent = new Intent();
switch (view.getId()) {
case R.id.bt_YES:
intent.putExtra("result", "评估返回:有信心!");
setResult(SendActivity.RESULT_CODE, intent);
finish();
break;
case R.id.bt_NO:
intent.putExtra("result", "评估返回:没有信心!");
setResult(SendActivity.RESULT_CODE, intent);
finish();
break;
}
}
}
package com.example.a15083.fragmentandactivity;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
public class ReceiveActivity extends AppCompatActivity implements View.OnClickListener {
private TextView tv_qua;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_receive);
tv_qua=(TextView) findViewById(R.id.tv_qua);
Intent intent = getIntent();
String name = intent.getStringExtra("name");
tv_qua.setText(name+"同学:学习Android有没有信心?");
}
@Override
public void onClick(View view) {
Intent intent = new Intent();
switch (view.getId()) {
case R.id.bt_YES:
intent.putExtra("result", "评估返回:有信心!");
setResult(SendActivity.RESULT_CODE, intent);
finish();
break;
case R.id.bt_NO:
intent.putExtra("result", "评估返回:没有信心!");
setResult(SendActivity.RESULT_CODE, intent);
finish();
break;
}
}
}