【发布时间】:2025-12-21 10:50:10
【问题描述】:
我在获取活动状态时遇到问题。我有 2 个活动:MainActivity 和 Activity2。在 MainActivity 我放了一个 editText 和一个 button 名称 GO。在 Activity2 中,我有一个 button 名称 BackMainActivity。我想要的是:我将文本,例如:“abc”放入EditText,然后单击按钮GO。应用程序将导航到 Activity2。之后,我单击 BackMainActivity 按钮,应用程序将导航到 MainActivity,并且 EditText 中的数据恢复为“abc”。
我已经使用过 onSaveInstanceState 和 onRestoreInstanceState。应用程序在进入 Activity2 之前通过 onSaveInstanceState 运行。但如果我回到 MainActivity,onCreate(),savedInstanceState 为空。你能告诉我原因吗?我想将 mainActivity 的数据存储在 bundle 中。那我该怎么办?非常感谢!
主活动
package com.example.demosavedataactivity;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity {
EditText t;
Button b;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
t = (EditText) findViewById(R.id.editText1);
b = (Button) findViewById(R.id.button1);
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(MainActivity.this, Activity2.class);
startActivity(intent);
}
});
}
@Override
protected void onSaveInstanceState(Bundle outState) {
// TODO Auto-generated method stub
String a = t.getText().toString();
outState.putString("text",a);
super.onSaveInstanceState(outState);
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
// TODO Auto-generated method stub
t.setText(savedInstanceState.getString("text"));
super.onRestoreInstanceState(savedInstanceState);
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
Log.w("<<<<<<<<<<<<<<<<<<<", "onDestroy");
super.onDestroy();
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
Log.w("<<<<<<<<<<<<<<<<<<<", "onPause");
super.onPause();
}
@Override
protected void onRestart() {
// TODO Auto-generated method stub
Log.w("<<<<<<<<<<<<<<<<<<<", "onRestart");
super.onRestart();
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
Log.w("<<<<<<<<<<<<<<<<<<<", "onResume");
super.onResume();
}
@Override
protected void onStart() {
// TODO Auto-generated method stub
Log.w("<<<<<<<<<<<<<<<<<<<", "onStart");
super.onStart();
}
@Override
protected void onStop() {
// TODO Auto-generated method stub
Log.w("<<<<<<<<<<<<<<<<<<<", "onStop");
super.onStop();
}
}
活动2
package com.example.demosavedataactivity;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
public class Activity2 extends Activity {
Button back;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity2);
back = (Button) findViewById(R.id.button1);
back.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(Activity2.this, MainActivity.class);
startActivity(intent);
}
});
}
}
activity2.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CombackA1" />
</LinearLayout>
activity_main.xml
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="14dp"
android:layout_marginTop="18dp"
android:ems="10" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editText1"
android:layout_below="@+id/editText1"
android:layout_marginLeft="50dp"
android:layout_marginTop="63dp"
android:text="Button" />
</RelativeLayout>
【问题讨论】:
-
有什么问题..我测试了你的代码..效果很好。
-
我想要的是:在mainActivity的editText中输入像“abc”这样的字符串。然后单击 GO 按钮转到 Activity2。在 Activity2 中,单击 BackMainActivity 按钮返回 MainActivity。而在 MainActivity 中,它仍然在 editText 中保留了文本“ABC”。事实上,editText 是 null :((。你知道为什么吗?我已经在上面的问题中解释了我的问题。谢谢你。
-
我也有同样的问题,其实这个问题的所有答案都解决不了。我认为这是一个基本问题,但为什么会混淆?