老师,我写的代码点进去第二种启动方式了,并点了第二界面中的按钮返回了第一界面,可当再点第一界面中的按钮时还是可以回到第二界面,,,这是不是说第二界面没被销毁,,,如果没被销毁该怎么做。。??
我的代码如下:
1.
package com.example.doemintent;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class FActivity extends Activity implements OnClickListener {
private Button btn1, btn2;
private TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView) findViewById(R.id.textView1);
btn1 = (Button) findViewById(R.id.button1_f);
btn2 = (Button) findViewById(R.id.button2_s);
btn1.setOnClickListener(this);
btn2.setOnClickListener(this);
}
@Override
public void onClick(View v) {
Intent intent = new Intent(FActivity.this, SActivity.class);
startActivityForResult(intent, 1);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1 && resultCode == 2) {
String content = data.getStringExtra("data");
tv.setText(content);
}
}
}
2.
package com.example.doemintent;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class SActivity extends Activity {
private Button bt;
private String content = "你最近还好吗?";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sactivity);
bt = (Button) findViewById(R.id.button1);
bt.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent data = new Intent();
data.putExtra("data", content);
setResult(2, data);
finish();
}
});
}
}