簡體   English   中英

捆綁的saveInstanceState在onCreate()上為null,盡管saveInstanceState已經設置了數據

[英]Bundle savedInstanceState is null on onCreate() although savedInstanceState is already set data

獲取活動狀態時出現問題。 我有2個活動:MainActivity和Activity2。 在MainActivity中,我放置了editTextbutton名稱GO。 在Activity2中,我有一個button名稱BackMainActivity 我想要的是:我將一個文本(例如:“ abc”)放入EditText,然后單擊“執行”按鈕。 應用程序將導航到Activity2。 之后,我單擊按鈕BackMainActivity,應用程序將導航到MainActivity,EditText中的數據將還原為“ abc”。 我已經使用過onSaveInstanceState和onRestoreInstanceState。 在進入Activity2之前,應用程序通過onSaveInstanceState運行。 但是,如果我回到MainActivity,則onCreate()的savedInstanceState為null。 你能告訴我原因嗎? 我想將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();
    }
}

活性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設置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);

為了更好的主意,請檢查此對話

在Activity2的onClick函數中,您不會返回上一個MainActivity,而是打開一個新的MainActivity。

只需通過調用finish()完成Activity2,您的第一個MainActivity將再次出現。 它應該與設備的硬件后退按鈕一起使用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM