2016-10-05 76 views
0

我已經編寫了以下代碼來設置片段的字符串。通過使用startActivityForResult我得到的值,並設置爲片段字符串即((firstString,secondString)當調用多個時間startActivityForResult時,片段數據丟失

但問題是,無論何時我調用startActivityForResult片段被重新創建和以前的數據丟失。例如,當我設置secondString比firstString的值丟失。

我已經提到了幾個答案來保存和恢復片段數據,但無法做到這一點。

boolean isEditing = true; 
LinearLayout first_layout, second_layout; 
TextView first_textview, second_textview; 
String firstString, secondString; 

@Override 
public void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    //setContentView(R.layout.activity_profile); 
    if(savedInstanceState!=null) 
    { 
     if(savedInstanceState.getBoolean("isEditing",false)) 
     { 
      Log.e("onSaveInstanceState","Restored"); 
      isEditing=true; 
      firstString = savedInstanceState.getString(firstString); 
      secondString = savedInstanceState.getString(secondString); 
     } 
    } 
} 


@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 

    View view = inflater.inflate(R.layout.mf_postload, container, false); 

    // Linearlayout 
    first_layout = (LinearLayout) view.findViewById(R.id.payment_layout_MF_PostLoad); 
    second_layout = (LinearLayout) view.findViewById(R.id.remark_layout_MF_PostLoad); 

    // TextView 
    first_textview = (TextView) view.findViewById(R.id.payment_MF_PostLoad); 
    second_textview = (TextView) view.findViewById(R.id.remark_MF_PostLoad); 

    first_textview.setText(firstString); 
    second_textview.setText(firstString); 


    // Listner 
    first_layout.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      Intent intent = new Intent(getActivity().getApplicationContext(), Remark.class); 
      Integer requestCode = 1; 
      intent.putExtra("requestCode", requestCode); 
      startActivityForResult(intent, requestCode); 
     } 
    }); 

    second_layout.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      Intent intent = new Intent(getActivity().getApplicationContext(), Remark.class); 
      Integer requestCode = 2; 
      intent.putExtra("requestCode", requestCode); 
      startActivityForResult(intent, requestCode); 
     } 
    }); 

    return view; 
} 

@Override 
public void onSaveInstanceState(Bundle bundle) 
{ 
    super.onSaveInstanceState(bundle); 
    Log.e("onSaveInstanceState","Called"); 
    try 
    { 
     if(isEditing) 
     { 
      bundle.putBoolean("isEditing",true); 
      bundle.putString("firstString",firstString); 
      bundle.putString("secondString",secondString); 
      Log.e("onSaveInstanceState","Called and Saved"); 
     } 
     else 
     { 
      bundle.putBoolean("isEditing",false); 
      Log.e("onSaveInstanceState","Called and not Saved"); 
     } 
    } 
    catch (Exception e) 
    { 
     e.printStackTrace(); 
    } 
} 

// Call Back method to get the Message form other Activity 
@Override 
public void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 
    // First 
    if (requestCode == 1) { 
     if (resultCode == Activity.RESULT_OK && data != null) { 
      firstString = data.getStringExtra("remark_string"); 
      first_textview.setText(firstString); 
     } 

    } 

    // Second 
    if (requestCode == 2) { 
     if (resultCode == Activity.RESULT_OK && data != null) { 
      secondString = data.getStringExtra("remark_string"); 
      second_textview.setText(secondString); 
     } 

    } 

} 
+0

當你調用一個新的活動舊其中包含您的片段被破壞,這意味着你的片段也被毀,其原因是該設備需要存儲資源(RAM),所以它可以自動銷燬背景活動和解決方案是覆蓋'onSaveInstanceState'然後在'onCreate'中讀取保存的值到它們各自的屬性 –

+0

@Haidar你能否給我提供一些示例代碼以供參考 –

+0

好吧,我會發佈一個答案 –

回答

0

這裏是onSaveInstance

//Like you see here am using a boolean to determine if i need to save the variable of not 
//because if i am finishing the activity normally and its not the case where system kills it 
//so i dont need to save these and if case that this boolean is true than the system is 
//killing my activity and i need to save the values 
@Override 
public void onSaveInstanceState(Bundle bundle) 
{ 
    super.onSaveInstanceState(bundle); 

    Log.e("onSaveInstanceState","Called"); 

    try 
    { 
     if(isEditing) 
     { 
      bundle.putBoolean("isEditing",true); 
      bundle.putString("photoFile", photoFile.getAbsoluteFile()+""); 
      bundle.putString("birthday",birthdayText.getText().toString()); 
      bundle.putString("phone",phoneText.getText().toString()); 
      bundle.putString("gender",genderText.getText().toString()); 
      bundle.putString("martial",martialText.getText().toString()); 
      bundle.putString("email",emailText.getText().toString()); 
      bundle.putString("first",editFirstName.getText().toString()); 
      bundle.putString("last",editLastName.getText().toString()); 
      Log.e("onSaveInstanceState","Called and Saved"); 
     } 
     else 
     { 
      bundle.putBoolean("isEditing",false); 
      Log.e("onSaveInstanceState","Called and not Saved"); 
     } 
    } 
    catch (Exception e) 
    { 
     e.printStackTrace(); 
    } 
} 

壓倒一切這裏是onCreate

//In onCreate am checking if i need to restore these values or not according 
//to what i saved and in this way its like my activity never got killed 
@Override 
public void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_profile); 

    if(savedInstanceState!=null) 
    { 
     if(savedInstanceState.getBoolean("isEditing",false)) 
     { 
      Log.e("onSaveInstanceState","Restored"); 

      isEditing=true; 
      photoFile =new File(savedInstanceState.getString("photoFile"),"") ; 
      birthdayText.setText(savedInstanceState.getString("birthday","1/1/1980")); 
      phoneText.setText(savedInstanceState.getString("phone","")); 
      genderText.setText(savedInstanceState.getString("gender","Please specify gender")); 
      martialText.setText(savedInstanceState.getString("martial","Please specify marital")); 
      emailText.setText(savedInstanceState.getString("email","")); 
      editFirstName.setText(savedInstanceState.getString("first","")); 
      editLastName.setText(savedInstanceState.getString("last","")); 


     } 
    } 


} 
0

壓倒一切的一些調試我已經解決了之後,由於海德爾。

@Override 
public void onSaveInstanceState(Bundle bundle) { 
    super.onSaveInstanceState(bundle); 
    Log.e("onSaveInstanceState", "Called"); 
    bundle.putString("firstString", firstString); 
    bundle.putString("secondString", secondString); 
    Log.e("onSaveInstanceState", "Called and Saved" + firstString + " " + secondString); 
} 


@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    Log.e("onSaveInstanceState", "Restored Called"); 
    if (savedInstanceState != null) { 
     firstString = savedInstanceState.getString("firstString"); 
     secondString = savedInstanceState.getString("secondString"); 
     Log.e("onSaveInstanceState", "Restored" + firstString + " " + secondString); 
    } 
}