我的应用中有两个活动:主要和搜索。
在主要用户通过单击搜索键入原点和目的地将搜索显示地图的活动。但是,如果我点击Android的背面它回到主要状态我需要再次调用onCreate。
研究onResume()
方法,但不知道如何在我的应用程序中使用它。
我应该将onCreate()
放在onResume()
吗?
public void onCreate(Bundle savedInstanceState) {
db = new DB(getBaseContext());
db.getWritableDatabase();
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final ActionBar actionBar = getActionBar();
actionBar.setSubtitle("http://www.eptc.com.br");
actionBar.setDisplayShowTitleEnabled(true);
actionBar.setDisplayShowHomeEnabled(true);
intent = new Intent(Intent.ACTION_MAIN);
date=(TextView)findViewById(R.id.date);
date.setText(today);
time=(TextView)findViewById(R.id.time);
time.setText(horaHoje);
imageViewDatepicker=(ImageView)findViewById(R.id.imageViewDatepicker);
imageViewDatepicker.setOnClickListener(this);
imageViewTimepicker=(ImageView)findViewById(R.id.imageViewTimepicker);
imageViewTimepicker.setOnClickListener(this);
/*
* Prepara para receber os eventos dos botões
*/
Button btnSearch = (Button) findViewById(R.id.btnSearch);
/*
* Prepara para receber os valores dos texts
*/
final EditText txtOrigem = (EditText) findViewById(R.id.txtOrigem);
final EditText txtDestino = (EditText) findViewById(R.id.txtDestino);
/*
* Suporte ao onclick do botão pesquisar
*/
OnClickListener btnSearchOnClickListener = new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
};
btnSearch.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
BandeiraDAO dao = new BandeiraDAO(getBaseContext());
getDayOfWeek();
checkDateHour();
Intent intent = new Intent(Main.this, Search.class);
intent.putExtra("diaMes", diaMes);
intent.putExtra("data", selecteddate);
intent.putExtra("hora", selectedHour );
intent.putExtra("diaDaSemana", diaDaSemana);
intent.putExtra("origin",txtOrigem.getText().toString());
intent.putExtra("destination", txtDestino.getText().toString());
startActivity(intent);
}
});
}
答案 0 :(得分:3)
但是,如果我点击Android的背面,它返回主要状态我需要再次调用onCreate
所以,您可以使用recreate()
,让我解释一下。
startActivityForResult
代替startActivity
。在完成后启动您想要结果的活动。当此活动退出时(通过按Back或调用finish()),将使用给定的requestCode调用onActivityResult()方法。
Intent intent = new Intent(Main.this, Search.class);
intent.putExtra("diaMes", diaMes);
intent.putExtra("data", selecteddate);
intent.putExtra("hora", selectedHour );
intent.putExtra("diaDaSemana", diaDaSemana);
intent.putExtra("origin",txtOrigem.getText().toString());
intent.putExtra("destination", txtDestino.getText().toString());
startActivityForResult(intent, 1234); // 1234 will be requestCode on callback
onActivityResult
并重新创建它。当您的活动重新开始时,您将在onResume()之前立即收到此电话。
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (requestCode == 1234) { // this code was used on startActiviyForResults
if (android.os.Build.VERSION.SDK_INT >= 11)
{
recreate(); // since API11
}
else
{
// workaround for pre-API11
startActivity(getIntent());
finish();
}
}
}
此代码重新创建活动,当前实例将经历其生命周期到onDestroy()以及之后创建的新实例。