I am using the following method to save the user inputted variables between activities:
// Save instance between activities
@Override
public void onSaveInstanceState(Bundle savedInstanceState)
{
Toast.makeText(searchParam.this, "onSaveInstanceState", Toast.LENGTH_SHORT).show();
savedInstanceState.putString("subject", selectedSubject);
savedInstanceState.putString("priceBand", priceBand);
savedInstanceState.putFloat("rating", starRating);
savedInstanceState.putBoolean("isCheck", checked);
super.onSaveInstanceState(savedInstanceState);
}
When the user selected the advanced options he/she is sent to the advancedSearch.class
to perform other things via this method:
public void listenerForAdvSearch()
{
// Initialize advanced options button
Button advancedOptionsBtn = (Button) findViewById(R.id.advancedButton);
advancedOptionsBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent advancedOptions = new Intent(searchParam.this, advancedSearch.class);
startActivity(advancedOptions);
}
});
}
On return my onCreate method is called as follows:
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toast.makeText(searchParam.this, "onCreate called", Toast.LENGTH_LONG).show();
if (savedInstanceState != null)
{
Toast.makeText(searchParam.this, "if savedInstanceState != NULL", Toast.LENGTH_LONG).show();
selectedSubject = savedInstanceState.getString("subject");
priceBand = savedInstanceState.getString("priceBand");
checked = savedInstanceState.getBoolean("isCheck");
starRating = savedInstanceState.getFloat("rating");
}
else
Toast.makeText(searchParam.this, "if savedInstanceState = NULL", Toast.LENGTH_SHORT).show();
// Call all listeners
listenerForRatingBar();
listenerForSubject();
listenerForSearch();
listenerForAdvSearch();
listenerForGeoCheck();
listenerForPrice();
}
I have a couple toast messages in there for my personal debugging. My issue is in the if statement in the onCreate method; the savedInstanceState bundle is coming back as NULL and not restoring the values on return from the advanced options class. Why is this happening?
NOTE: here is the relevant code for advancedSearch class:
public class advancedSearch extends AppCompatActivity {
// initialize variables
int minute_x, hour_x;
int year_x, month_x, day;
static final int dialogid = 0;
static final int tdialogid = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_advanced_search);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
// Call picker dialogs on button click
showDialogOnButtonClick();
showTimeDialogOnButtonClick();
}