2016-10-05 47 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); 
    } 
} 
相关问题