gpt4 book ai didi

java - 通过 Bundle 在 Activity 之间保留值

转载 作者:行者123 更新时间:2023-11-29 19:57:45 25 4
gpt4 key购买 nike

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

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

}

最佳答案

您需要将 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();

}

关于java - 通过 Bundle 在 Activity 之间保留值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36562457/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com