<img height="1" width="1" style="display:none;" alt="" src="https://px.ads.linkedin.com/collect/?pid=299788&amp;fmt=gif">

Practical Android Example - Retaining Fragment Input

Software Development, Software Solutions, Java

If you are new to Android development, you're likely to be thrown for a loop when it comes to working with Fragments... more specifically, getting your fragments to retain user input when the state changes.
Android provides a few different options for retaining application data. Details of how this is supposed to be done can be found here. If you're like me, and need a little more detailed and practical example. Read on!We are going to look at retaining data through orientation or navigation drawer selection changes. In your fragment_user.xml, create the EditText control with an ID, so you can reference it in the activity and the fragment.
fragment_user.xml

<EditText
android:id="@+id/user_ID" android:layout_width="wrap_content" android:layout_height="wrap_content" android:hint="@string/user_hint"> </EditText>
In your activity class, "Override" the onSaveInstanceState method to instantiate the text field and put the entered text into the outState Bundle.  This saves the key-value pair in the savedInstanceState to be retrieved by the onCreate method.  When the onCreate is called again and again, the value is then added to a savedArgs Bundle that is passed back to the fragment when it is inflated within the activity.
MainActivity.java

Bundle savedArgs = new Bundle();
ExampleFragment fragment = new ExampleFragment();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState.getString(SAVED_USER) != null){
savedArgs.putString(fragment.USER, savedInstanceState.getString(SAVED_USER));
}
@Override
public void onNavigationDrawerItemSelected(int position) {
// update the main content by replacing fragments
FragmentManager fragmentManager = getSupportFragmentManager();
try {
switch (position) {
// Example Fragment
case 0:
if (savedArgs != null){
fragment.setArguments(savedArgs);
}
fragmentManager.beginTransaction().replace(R.id.container, fragment).commit();
break;
case 1:
. . .
break;
}
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
setTitle(mTitles[position]);
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
// Save the user entered username
EditText etUser = (EditText) findViewById(R.id.user_ID);
outState.putString(SAVED_USER, etUser.getText().toString());
}
In your fragment class, we first check to see if the savedInstanceState Bundle contains a USER key.  If it's there, the local user variable (mUser) is assigned the saved value and then set as the Text property.  If the local variable hasn't been set, the text field will show the hint we set in the fragment_user.xml.
ExampleFragment.java

public class ExampleFragment extends Fragment{
public final String USER = "saved_user";
private String mUser;
private EditText etUser;
/**
* Default Constructor
*/
public ExampleFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_user, container, false);
//User Name
etUser = (EditText) rootView.findViewById(R.id.user_ID);
if (getArguments().getString(USER) != null){
mUser=getArguments().getString(USER);
}
if (mUser != null){
>etUser.setText(mUser);
}
return rootView;
}
}

That's it!  You should now be able to take this and apply it to your specific situation.


This isn't the only way to accomplish this, it's just the way that made sense to me. And it works! So hopefully this saves others some time when trying to retain user input.
Managing JIRA at Scale White Paper

 

TAGS: Software Development, Software Solutions, Java

0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Subscribe to Our Newsletter

Recent Blog Posts