通过 Bundle 在活动之间保留值

Preserving values between activities via 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);
    }

当用户选择高级选项时 he/she 被发送到 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);
            }
        });
    }

在return我的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();

    }

我有几条消息供我个人调试。我的问题出在 onCreate 方法中的 if 语句中; savedInstanceState 包返回为 NULL,并且不会从高级选项 class 恢复 return 上的值。为什么会这样?

注意:这里是高级搜索的相关代码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();

    }

您需要将 Bundle 添加到您从中开始 ActivityIntentonSaveInstanceState() 在这里不起作用。在另一个 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 中,从 Intent 中的 Bundle 获取数据:

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

}

您似乎想在两个活动之间传递数据。您可以实现将数据添加到 Intent.

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

并检索第二个 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");
}