调用多次startActivityForResult时,片段数据会丢失

时间:2016-10-05 06:28:46

标签: android android-fragments

我编写了以下代码来设置片段的字符串。通过使用startActivityForResult我得到的值并设置为片段字符串,即(firstString,secondString)

但问题是每当我调用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);
        }

    }

}

2 个答案:

答案 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",""));


        }
    }


}

答案 1 :(得分:0)

经过一些调试,我已经解决了,谢谢Haider。

@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);
    }
}