[英]Bundle savedInstanceState is null on onCreate() although savedInstanceState is already set data
I have a problem when getting state of activity. 获取活动状态时出现问题。 I have 2 activities: MainActivity and Activity2.
我有2个活动:MainActivity和Activity2。 In MainActivity I have put an
editText
and a button
name GO. 在MainActivity中,我放置了
editText
和button
名称GO。 In Activity2, I have a button
name BackMainActivity
. 在Activity2中,我有一个
button
名称BackMainActivity
。 What I want is that: I put a text, for example:"abc" into EditText then click button GO. 我想要的是:我将一个文本(例如:“ abc”)放入EditText,然后单击“执行”按钮。 App will navigate to Activity2.
应用程序将导航到Activity2。 After that, I click button BackMainActivity, app will navigate to MainActivity and data in EditText is restore as "abc".
之后,我单击按钮BackMainActivity,应用程序将导航到MainActivity,EditText中的数据将还原为“ abc”。 I already used onSaveInstanceState and onRestoreInstanceState.
我已经使用过onSaveInstanceState和onRestoreInstanceState。 App run through onSaveInstanceState before going to Activity2.
在进入Activity2之前,应用程序通过onSaveInstanceState运行。 But if I go back to MainActivity, onCreate(), savedInstanceState is null.
但是,如果我回到MainActivity,则onCreate()的savedInstanceState为null。 Can you show me the reason ?
你能告诉我原因吗? I want to store data of mainActivity in bundle.
我想将mainActivity的数据存储在包中。 So how can I do ?
那我该怎么办? Thank you very much !
非常感谢你 !
MainActivity 主要活动
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();
}
}
Activity2 活性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 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 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>
Just do something like: 只需执行以下操作:
1: Fetch data from onSavedInstanceState
in your oncreate()
and put it in your edittext
. 1:在
oncreate()
从onSavedInstanceState
中获取数据,并将其放入edittext
。 Something like: 就像是:
@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);
/*Just fetch data from savedInstanceState */
if(savedInstanceState != null){
t.setText(savedInstanceState.getString("text"));
}
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, Activity2.class);
startActivity(intent);
}
});
}
And in your Activity2
set a FLAG_ACTIVITY_REORDER_TO_FRONT
flag with your intent.like: 而在你的
Activity2
设置FLAG_ACTIVITY_REORDER_TO_FRONT
与intent.like标志:
Intent intent = new Intent(Activity2.this, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(intent);
For a better idea just check this conversation . 为了更好的主意,请检查此对话 。
In the onClick function of your Activity2 you don't go back to the previous MainActivity but you open a new MainActivity. 在Activity2的onClick函数中,您不会返回上一个MainActivity,而是打开一个新的MainActivity。
Just finish Activity2 by calling finish() then your first MainActivity will come up again. 只需通过调用finish()完成Activity2,您的第一个MainActivity将再次出现。 It should be working with the hardware back button of your device.
它应该与设备的硬件后退按钮一起使用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.