1

我试图在旋转屏幕后按下返回按钮。我知道在屏幕旋转时,活动想要做的是

  1. 在第一个活动中输入消息
  2. 在第二个活动中显示消息
  3. 旋转屏幕并通过 onSaveInstanceState() 保存“ata”
  4. 重新创建第二个活动
  5. 在第二个活动上点击返回按钮(重新创建第一个活动 - onCreate() 调用)

由于 savedInstanceState 不为空,它应该显示保存的任何内容。唉,正在显示空值。这是代码

第一项活动:-

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);
}**
}

任何帮助都是不言而喻的。

4

1 回答 1

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");
}

希望这能解决你的问题

于 2013-02-20T08:40:38.393 回答