旋转屏幕后,我正试图按回按钮。我知道在屏幕旋转时,活动我想要做的是
由于savedInstanceState不为null,因此应显示已保存的内容。 Bu alas,null正在显示。这是代码
第1次活动: -
package dummy.pack;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity implements CommonHandler{
public final static String EXTRA_MESSAGE = "dummy.MESSAGE";
public static String DISPLAY_MESSAGE;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
**if (savedInstanceState != null)
{
DISPLAY_MESSAGE = savedInstanceState.getString("message");
DISPLAY_MESSAGE = DISPLAY_MESSAGE + " calling from create";
getMessage();
}**
else
{
setContentView(R.layout.activity_main);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
public void sendMessage(View view)
{
Intent intent = new Intent (this, DisplayMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.edit_message);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
@Override
public void onStart() {
super.onStart();
}
@Override
public void onStop(){
super.onStop();
System.gc();
}
**public void getMessage() {
TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setText(DISPLAY_MESSAGE);
setContentView(textView);
}**
@Override
public void onRestart (){
super.onRestart();
DISPLAY_MESSAGE = "Normal 1";
getMessage();
}
}
第二项活动: -
package dummy.pack;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.widget.TextView;
public class DisplayMessageActivity extends Activity implements CommonHandler{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Get the message from Intent
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
//Create the text view
TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setText(message);
setContentView(textView);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_display_message, menu);
return true;
}
@Override
**public void onSaveInstanceState(Bundle saveState) {
saveState.putString("message","Recreate");
super.onSaveInstanceState(saveState);
}**
}
任何帮助都会很明显。
答案 0 :(得分:0)
在清单文件
中的活动中添加以下内容<activity android:name="Activity name"
android:configChanges="keyboardHidden|orientation|">
并在您的活动中
if(newConfig.orientation==Configuration.ORIENTATION_LANDSCAPE){
Log.e("On Config Change","LANDSCAPE");
}else{
Log.e("On Config Change","PORTRAIT");
}
希望这能解决您的问题