简体   繁体   English

Android OnCreate 输入包始终 null

[英]Android OnCreate input bundle always null

I am undertaking project but I have been having some issues with activities and saving state.我正在进行项目,但我在活动和保存 state 方面遇到了一些问题。 So I have gone back to basics to try and understand what I was missing.所以我回到了基础,试图了解我错过了什么。 However, even with a very simple set up I am having the same issues.但是,即使设置非常简单,我也遇到了同样的问题。

So I started with an empty project and placed 2 textViews and 1 button on the activity_main.xml and 1 button on the activity_2.xml.所以我从一个空项目开始,在activity_main.xml上放置了2个textViews和1个按钮,在activity_2.xml上放置了1个按钮。 The user will enter some text in a text box, press the button to advance to the Activity2 Activity and then press the button (in the Activity2 layout) to return back to the MainActivity.用户将在文本框中输入一些文本,按下按钮前进到 Activity2 Activity,然后按下按钮(在 Activity2 布局中)返回到 MainActivity。

My hope is that the text entered into the text box has been saved by the overriden onSaveInstanceState method and then the bundle argument in the OnCreate will hold this value.我希望输入到文本框中的文本已被覆盖的 onSaveInstanceState 方法保存,然后 OnCreate 中的 bundle 参数将保存此值。 However, when MainActivity is hit, the bundle is alywas null despite onSaveInstanceState being called when button to start Activity2 is pressed.但是,当 MainActivity 被击中时,尽管在按下启动 Activity2 的按钮时调用了 onSaveInstanceState,但捆绑包是 alywas null。

Am I missing something?我错过了什么吗? The onSaveInstanceState method is definitely initiated and savedInstanceState is populated within it, so why when the MainActivity is started again and onCreate is called is the Bundle savedInstanceState null? onSaveInstanceState 方法肯定是启动的,并在其中填充了 savedInstanceState,那么为什么当再次启动 MainActivity 并调用 onCreate 时,Bundle savedInstanceState 是 null? Is it the way I am setting up starting other Activities?这是我设置开始其他活动的方式吗?

Thanks for your help!谢谢你的帮助!

For reference, here is my setup:作为参考,这是我的设置:

My MainActivity:我的主要活动:

public class MainActivity extends AppCompatActivity {

    Button forwardButton;
    TextView editText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        forwardButton = findViewById(R.id.buttonForward);
        editText = findViewById(R.id.editText);
        forwardButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                // Code here executes on main thread after user presses button
                Intent activity2 = new Intent(MainActivity.this,Activity2.class);
                startActivity(activity2);
            }
        });
    }

    @Override
    public void onSaveInstanceState(Bundle savedInstanceState) {
        String textToSave = editText.getText().toString();
        savedInstanceState.putString("editText",textToSave);
        super.onSaveInstanceState(savedInstanceState);
    }
}``

My Activity2 class:我的活动 2 class:

public class Activity2 extends AppCompatActivity {

    Button goBack;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_2);

        goBack = findViewById(R.id.goBack);
        goBack.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                // Code here executes on main thread after user presses button
                Intent activity2 = new Intent(Activity2.this,MainActivity.class);
                startActivity(activity2);
            }
        });
    }
}

Activity_main.xml: Activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/buttonForward"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="162dp"
        android:layout_marginTop="232dp"
        android:layout_marginEnd="162dp"
        android:layout_marginBottom="76dp"
        android:text="Forward"
        app:layout_constraintBottom_toTopOf="@+id/textView"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <EditText
        android:id="@+id/editText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="96dp"
        android:layout_marginTop="43dp"
        android:layout_marginEnd="102dp"
        android:layout_marginBottom="268dp"
        android:ems="10"
        android:inputType="textPersonName"
        android:text="Enter"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView" />

</androidx.constraintlayout.widget.ConstraintLayout>

activity_2.xml:活动_2.xml:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Activity2">

    <Button
        android:id="@+id/goBack"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="162dp"
        android:layout_marginTop="232dp"
        android:layout_marginEnd="162dp"
        android:layout_marginBottom="76dp"
        android:text="Go back"
        app:layout_constraintBottom_toTopOf="@+id/textView"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

In your Activity2在你的Activity2

    goBack.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            Activity2.this.finish();
        }
    });

And get your saved instance in MainActivity from onRestoreInstanceState并从onRestoreInstanceState获取您在MainActivity中保存的实例

//this will trigger when the same instance of activity is restored
//by the back press that finishes the Activity2
@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
  super.onRestoreInstanceState(savedInstanceState);

  String data = savedInstanceState.getString("editText");

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM