I have a screen where the user presses a button to bring up a DialogFragment
with an EditText
. They enter their information, and press Ok
. If they press the button again, I've made it so the EditText
will display the information they had just put in, and everything works fine.
However, if they enter the information, press Ok
, and then use the options menu to go look at another screen/fragment
, the information will not be displayed in the EditText
once they press the button again.
How can I save the information even if the user navigates away from that screen for a moment? I think it would require a newInstance
method in the fragment class, and then have the hosting activity call newInstance
instead of the constructor. But I'm unsure of how to implement it. Any help appreciated. Thanks!
SingleFragmentActivity
:
public abstract class SingleFragmentActivity extends FragmentActivity
{
protected abstract Fragment createFragment();
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fragment);
FragmentManager fm = getSupportFragmentManager();
Fragment fragment = fm.findFragmentById(R.id.fragmentContainer);
if (fragment == null)
{
fragment = createFragment();
fm.beginTransaction()
.add(R.id.fragmentContainer, fragment)
.commit();
}
}
}
Relevant code from hosting activity, AdviceActivity:
public class AdviceActivity extends SingleFragmentActivity
{
@Override
protected Fragment createFragment()
{
return new AdviceFragment();
}
}
Relevant code from the hosting fragment, AdviceFragment:
private boolean isTextButtonFirstClick = true;
...
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState)
{
mTextButton = (Button) v.findViewById(R.id.textButton);
mTextButton.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
if (isTextButtonFirstClick)
{
FragmentManager fm = getActivity().getSupportFragmentManager();
InputTextFragment dialog = InputTextFragment.newInstance("", isTextButtonFirstClick);
dialog.setTargetFragment(AdviceFragment.this, REQUEST_TEXT);
dialog.show(fm, DIALOG_TEXT);
isTextButtonFirstClick = false;
}
else
{
FragmentManager fm = getActivity().getSupportFragmentManager();
InputTextFragment dialog = InputTextFragment.newInstance(mAdvice.getText(), isTextButtonFirstClick);
dialog.setTargetFragment(AdviceFragment.this, REQUEST_TEXT);
dialog.show(fm, DIALOG_TEXT);
}
}
});
...
}
Relevant code from DialogFragment InputTextFragment:
public static InputTextFragment newInstance(String text, boolean isTextButtonFirstClick)
{
Bundle args = new Bundle();
args.putSerializable(EXTRA_TEXTBUTTON_FIRSTCLICK, isTextButtonFirstClick);
args.putSerializable(EXTRA_TEXT, text);
InputTextFragment fragment = new InputTextFragment();
fragment.setArguments(args);
return fragment;
}
...
@Override
public Dialog onCreateDialog(Bundle savedInstanceState)
{
...
boolean isTextButtonFirstClick = getArguments().getBoolean(EXTRA_TEXTBUTTON_FIRSTCLICK);
final EditText editText = (EditText) v.findViewById(R.id.dialogInputEditText);
if (!isTextButtonFirstClick)
{
editText.setText(text);
editText.setSelection(text.length());
}
...
}
XML activity_fragment:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fragmentContainer"
android:layout_width="match_parent"
android:layout_height="match_parent">
</RelativeLayout>