2017-04-23 6 views
-2

このアクティビティで返却した後、編集テキストのアクティビティにデータを保存します。どうやってするの? OnPauseOnResumeを使用する必要がありますか?返却後にデータをアクティビティに保存する

+0

活性 –

+2

間【保存インスタンスの状態を使用してAndroidの活動状態を保存する]の可能な重複を切り替えながらデータを永続化するために使用saveinstancestate(http://stackoverflow.com/questions/151777/saving-android-activity-state-using-save -instance-state) – lnman

+0

この質問は少し不明です。より具体的にしてください。 –

答えて

0

Bundle savedInstanceStateを使用すると、アプリケーションの状態を保存できます。 デバイスの向きを変更すると、データが失われます。我々は値を保存し、以下の例で

XMLレイアウト
int value = 1;  

@Override 
protected void onSaveInstanceState(Bundle outState) { 
    super.onSaveInstanceState(outState); 

    /* EditText editText = 
      (EditText) findViewById(R.id.editText); 
    CharSequence text = editText.getText();// getting text in edit text 
    outState.putCharSequence("savedText", text);// saved text in bundle object (outState) 
    */ 

    outState.putInt("value", value); 

} 

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

    /* CharSequence savedText=savedInstanceState.getCharSequence("savedText"); 
    Toast.makeText(this, ""+savedText, Toast.LENGTH_SHORT).show();*/ 

    value = savedInstanceState.getInt("value"); 
    Toast.makeText(this, ""+value, Toast.LENGTH_SHORT).show(); 
} 

public void buttonClicked(View view) { 
    value++; 
} 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 

    <EditText 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:inputType="text" 
     android:id="@+id/editText"/> 
    <EditText 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:inputType="text"/> 
<Button 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Click Me" 
    android:onClick="buttonClicked"/> 

</LinearLayout> 

注:テキストの編集では、任意の情報、ボタンなど は、限り、あなたは割り当てるようアンドロイドによって自動的に保持されますID。 2番目の編集テキストはその情報を消去しますが、上の例で画面を回転させると、1番目のテキスト編集IDのテキストを編集することはできません。

関連する問題