I'm having an issue with preserving the data entered by a user on my android activity classes. I have identified I need to make use of the onSavedInstanceState(Bundle outState)
method but the way my program is written makes this difficult.
A user enters a variety of data in DataEntry.java
class and the information they submit is displayed on DataSummary.java
. This works fine.
But when a user navigates away from DataSummary.java
say, to fill in the rest of the information on DataEntry.java
the original submitted data is lost if you go back to DS.java
to see what you've written already. Below is the code for DataSummary.java
.
public class DataSummary extends Activity {
ImageView resultImage;
TextView resultName;
TextView resultDescription;
TextView resultType;
TextView resultProject;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_data_summary);
//Check if there is anything in the 'bundle' and if not produce message - AVOIDS NULLPOINTEREXCEPTION when navigating to Activity
Bundle bundle = this.getIntent().getExtras();
if (bundle != null){
int image = bundle.getInt("image");
String name = bundle.getString("key");
String description = bundle.getString("key1"); //gets data from DataEntry activity
String type = bundle.getString("key2");
String project = bundle.getString("key3");
resultImage=(ImageView)findViewById(R.id.resultImage);
resultName=(TextView)findViewById(R.id.resultName); //adds the TextViews to the activity
resultType=(TextView)findViewById(R.id.resultType);
resultDescription=(TextView)findViewById(R.id.resultDesc);
resultProject=(TextView)findViewById(R.id.resultProject);
resultImage.setImageResource(image);
resultName.setText(name); // Fills the textviews with imported data
resultType.setText(type);
resultDescription.setText(description);
resultProject.setText(project);
}
else
{
Toast.makeText(DataSummary.this,"Received no data yet!", Toast.LENGTH_LONG).show();
}
}
/* MANAGES ACTIVITY LIFESTYLE */
public void onSavedInstanceState(Bundle outState){
super.onSaveInstanceState(outState);
}
How can I expand on the onSavedInstanceState
method to get the imported data which is received when the Activity is created and preserved it if a user navigates away from this activity? Hope that's well explained enough?
It is difficult to figure out how to make use of the variables in onCreate as well as I cannot access them from another method (I think if I knew how to do this I could complete the method).