簡體   English   中英

Android:如何保存數據(意圖數據)

[英]Android : How to save data (Intent's Data)

在三個活動代碼下方,有一個示例代碼通過使用Intent傳遞了數據。

活動A

public class A extends AppCompatActivity {

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

public void onButton1Clicked(View v) {
    Intent intent = new Intent(getApplicationContext(), B.class);
    startActivity(intent);
}

活動B

public class B extends AppCompatActivity {

TextView tv;
ImageButton ib;

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

    tv = (TextView) findViewById(R.id.textView);
    ib = (ImageButton) findViewById(R.id.imageButton);
    ib.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent intent = new Intent(B.this, C.class);
            startActivityForResult(intent, 2);
        }
    });

}

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == 2) {
        if (data != null) {
            String message = data.getStringExtra("DATA");
            tv.setText(message);
        }
    }
}

public void onButton2Clicked(View v) {
    Intent intent = new Intent(getApplicationContext(), C.class);
    startActivity(intent);
}

活動C

public class C extends AppCompatActivity {

EditText et;
Button btn;


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

    et = (EditText) findViewById(R.id.editText);
    btn = (Button) findViewById(R.id.button);
    btn.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            String data = et.getText().toString();
            Intent intent = new Intent();
            intent.putExtra("DATA", data);
            setResult(2, intent);
            finish();
        }
    });
}

活動順序為A-B-C。

在A中,有一個可以移到B的按鈕,在B中有一個(可以移到C的)按鈕和TextView。 在C中,一個Button(可以再次移至B)和EditText。

例如,使用'startActivityForResult',我將輸出為TextView(B),該文本接收了C中EditText輸入的文本。但是將B移到A(Back)后,再次進入B時,消失了TextView的插入。 另外,即使進入C,也不會插入EditText。

我真的很需要並且知道在C中按下按鈕時通過輸入EditText作為輸入來將“保存代碼”保存到變量中。

在這種情況下,我該如何添加“代碼”作為插入值的剩余值,或者通過退出應用程序保留插入值,或者移至接收到像A這樣的DATA的Activity?

感謝您的合作和關注。

要維持活動狀態,您必須重寫onSaveInstanceState方法。 使用此方法存儲TextViews和EditText的值。 例如,讓我們談談活動B。在活動B中,您將執行以下操作:

@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
    // Save the user's current game state
    String text = tv.getText().toString();
    savedInstanceState.putString("mytext", text);

    // Always call the superclass so it can save the view hierarchy state
    super.onSaveInstanceState(savedInstanceState);
}

然后在您的onCreate執行以下操作:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState); // Always call the superclass first
   // Set Content View and initialize the views
    // Check whether we're recreating a previously destroyed instance
    if (savedInstanceState != null) {
        // Restore value of members from saved state
        String mytext = savedInstanceState.getString("mytext");
        tv.setText(mytext);
    } else {
        // Probably initialize members with default values for a new instance
    }
    ...
}

您可以閱讀有關重新創建活動的更多信息

您可以使用以下方式存儲Intent的數據。您的示例在如何傳遞數據方面似乎令人困惑,並且在ActivityB中有兩種方法來處理Intent。 (onButtonClicked和StartActivityForResult())

這是簡單的例子

定義唯一的字符串以將您的Intent數據保存在-ActivityA

public final static String EXTRA_MESSAGE ="myMessage";

下面的方法將打開ShowMessage活動,顯示您在EditText視圖中鍵入的消息。

 public void sendMessage(){
 Intent intent = new Intent(this, ShowMessage.class);
    EditText editText = (EditText) findViewById(R.id.edit_message);
    String message = editText.getText().toString();
    intent.putExtra(EXTRA_MESSAGE, message);
    startActivity(intent);
}

這是您如何在ShowMessage活動中檢索該數據的方法。 -ActivityB。

Intent intent = getIntent();
    String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
 //EXTRA_MESSAGE is a unique string that holds the message typed in TextView
  TextView receiveMessage = findViewById(R.id.sentMessage);
  receiveMessage.setText(message); //would set the message typed in ActivityA

現在,當您返回時,如果仍然希望您的EditText輸入先前的字符串,則可以作為Eric B給出的答案。

//Saving the value
@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);

    String myMessage = editText.getText().toString();
    outState.putString("myMessage", myMessage);

}

在onCreate()中,您可以按以下方式檢索

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

   if (savedInstanceState != null){

        String myMessage = savedInstanceState.getString("myMessage","");
        editText.setText(myMessage);
    }

推薦的一件事是resultCode == RESULT_OK以驗證意圖。

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);

if (requestCode == 2 && resultCode == RESULT_OK) {
    if (data != null) {
        String message = data.getStringExtra("DATA");
        tv.setText(message);
    }
}

暫無
暫無

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

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