通过Bundle保存活动之间的值

时间:2016-04-12 02:11:46

标签: java android android-intent android-bundle

我使用以下方法在活动之间保存用户输入的变量:

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

当用户选择高级选项时,他/她将被发送到advancedSearch.class以通过此方法执行其他操作:

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

返回时,我的onCreate方法调用如下:

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

    }

我在那里有几个Toast消息供我个人调试。我的问题出在onCreate方法的if语句中; savedInstanceState包返回为NULL,而不是从高级选项类返回时恢复的值。为什么会这样?

注意:这是advancedSearch类的相关代码:

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

    }

2 个答案:

答案 0 :(得分:1)

您需要将Bundle添加到您从Intent开始的ActivityonSaveInstanceState()在这里不起作用。在其他Activity内,使用getIntent().getExtras()Bundle提取您添加的值。

onSaveInstanceState()用于在您所在的Activity的重新设置或配置更改期间恢复Activity状态,而不是ActivityActivity

那是:

将您的覆盖移至onSaveInstanceState()

开始新的Activity时,将您需要的数据传入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);
         }
     });
}

在新Activity内,从Bundle内的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();

}

答案 1 :(得分:1)

看起来您想要在两个活动之间传递数据。您可以实现向Intent添加数据。

在第一个活动中使用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);
        }
    });
}

并检索第二个活动中的值:

@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");
}