gpt4 book ai didi

android - 尽管 savedInstanceState 已设置数据,但 onCreate() 上的 Bundle savedInstanceState 为 null

转载 作者:行者123 更新时间:2023-11-30 03:01:41 26 4
gpt4 key购买 nike

我在获取 Activity 状态时遇到问题。我有 2 个 Activity :MainActivity 和 Activity2。在 MainActivity 中,我放了一个 editText 和一个 button name GO。在 Activity2 中,我有一个 button 名称 BackMainActivity。我想要的是:我将文本,例如:“abc”放入 EditText,然后单击按钮 GO。应用程序将导航到 Activity2。之后,我单击按钮 BackMainActivity,应用程序将导航到 MainActivity,EditText 中的数据将恢复为“abc”。 我已经使用了 onSaveInstanceState 和 onRestoreInstanceState。应用程序在转到 Activity2 之前通过 onSaveInstanceState 运行。但是如果我回到 MainActivity,onCreate(),savedInstanceState 为空。你能告诉我原因吗?我想将 mainActivity 的数据存储在 bundle 中。那我该怎么办?非常感谢!

主要 Activity

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

Activity 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>

最佳答案

只需执行以下操作:

1: 在您的oncreate() 中从onSavedInstanceState 获取数据并将其放入您的edittext。像这样的东西:

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

然后在您的 Activity2 中设置一个带有您的 Intent 的 FLAG_ACTIVITY_REORDER_TO_FRONT 标志。例如:

 Intent intent = new Intent(Activity2.this, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(intent);

要获得更好的主意,请查看此 conversation .

关于android - 尽管 savedInstanceState 已设置数据,但 onCreate() 上的 Bundle savedInstanceState 为 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22422430/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com