我是android的初学者。我有一个简单的代码,我从我的第一个活动中调用一个活动,在第二个活动中我有一个按钮,按下该按钮第二个活动完成并且第一个活动出现。有没有办法在第一个活动中执行 onSaveInstanceState。我必须在清单文件中编辑一些东西吗?
以下是我的代码
public class Activity1 extends Activity {
/** Called when the activity is first created. */
TextView mTextView ;
Button b1;
static int count=0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mTextView = (TextView) findViewById(R.id.textView2);
if (savedInstanceState == null) {
mTextView.setText("Welcome to HelloAndroid!");
} else {
mTextView.setText("Welcome Back!");
System.out.println("count------>"+ count);
}
final Intent i = new Intent(this,activity2.class);
b1 = (Button) findViewById(R.id.button1);
b1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
startActivity(i);
finish();
}
});
}
@Override
public void onResume()
{
super.onResume();
System.out.println("inside Resume");
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Checks the orientation of the screen
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
savedInstanceState.putBoolean("MyBoolean", true);
savedInstanceState.putDouble("myDouble", 1.9);
savedInstanceState.putInt("MyInt", 1);
savedInstanceState.putString("MyString", "Welcome back to Android");
count++;
}
@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
boolean myBoolean = savedInstanceState.getBoolean("MyBoolean");
double myDouble = savedInstanceState.getDouble("myDouble");
System.out.println("MyBoolean"+ myBoolean);
System.out.println("myDouble"+ myDouble);
}
}
这是我的第二个活动,由第一个活动调用
public class activity2 extends Activity{
TextView textview;
Button b1;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main2);
textview = (TextView) findViewById(R.id.textView1);
textview.setText("in activity2");
final Intent i = new Intent(this,Activity1.class);
b1 = (Button) findViewById(R.id.button1);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(i);
finish();
}
});
}
}
有人请帮我谢谢!