我已經編寫了以下代碼來設置片段的字符串。通過使用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);
}
}
}
當你調用一個新的活動舊其中包含您的片段被破壞,這意味着你的片段也被毀,其原因是該設備需要存儲資源(RAM),所以它可以自動銷燬背景活動和解決方案是覆蓋'onSaveInstanceState'然後在'onCreate'中讀取保存的值到它們各自的屬性 –
@Haidar你能否給我提供一些示例代碼以供參考 –
好吧,我會發佈一個答案 –