1

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();

    }
Daniel
  • 66
  • 1
  • 9

2 Answers2

1

You need to add the Bundle to the Intent that you're starting the Activity from. onSaveInstanceState() won't work here. And inside the other Activity, use getIntent().getExtras() to get the Bundle to extract the values you've added.

onSaveInstanceState() is used to restore Activity state during recreation or configuration changes of the Activity that you're in, not across Activity to Activity.

That is:

Remove your override to onSaveInstanceState().

When starting new Activity, pass in the data you need into the Intent:

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);
             advancedOptions.putExtra("subject", selectedSubject);
             advancedOptions.putExtra("priceBand", priceBand);
             advancedOptions.putExtra("rating", starRating);
             advancedOptions.putExtra("isCheck", checked);
             startActivity(advancedOptions);
         }
     });
}

Inside your new Activity, get the data from the Bundle inside the Intent:

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toast.makeText(searchParam.this, "onCreate called", Toast.LENGTH_LONG).show();

    //Get your precious data
    Bundle bundle = getIntent().getExtras();
    selectedSubject = bundle.getString("subject");
    priceBand = bundle.getString("priceBand");
    checked = bundle.getBoolean("isCheck");
    starRating = bundle.getFloat("rating");

    // Call all listeners
    listenerForRatingBar();
    listenerForSubject();
    listenerForSearch();
    listenerForAdvSearch();
    listenerForGeoCheck();
    listenerForPrice();

}
razzledazzle
  • 6,900
  • 4
  • 26
  • 37
1

It looks like you want to pass data between two activities. You can achieve that adding data to Intent.

In the first activity add data using putExtra(...):

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

            advancedOptions.putExtra("subject", selectedSubject);
            advancedOptions.putExtra("priceBand", priceBand);
            advancedOptions.putExtra("isCheck", checked);
            advancedOptions.putExtra("rating", starRating);

            startActivity(advancedOptions);
        }
    });
}

And retrieve the values in the second activity:

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);

    selectedSubject = getIntent().getStringExtra("subject");
    priceBand = getIntent().getStringExtra("priceBand");
    checked = getIntent().getBooleanExtra("isCheck");
    starRating = getIntent().getFloatExtra("rating");
}
Vasily Kabunov
  • 6,511
  • 13
  • 49
  • 53