我有一个活动,其中我有3个视图。点击一个按钮我隐藏了一个视图并显示另一个视图。但我怀疑是我不能回到上一个视图。我该怎么做。< / p>
例如:I am on view A, and i click a button so now view A is hidden and now B is displayed.Now if press back button A should be displayed again but this is not happening,y app is directly closing.
代码
switch (view.getId()) {
case R.id.bt_continue_1:
if (str_first_name.equals("") || str_last_name.equals("") || str_gender.equals("Select Gender") || str_religion.equals("Select Religion") || str_age.equals("Select Age")) {
commonFunctions.showAlert(this, "INVALID", "Please fill all the mandatory details");
personal_info.setVisibility(view.VISIBLE);
professional_info.setVisibility(view.GONE);
contact_info.setVisibility(view.GONE);
} else {
header.setText("Contact Info");
personal_info.setAnimation(commonFunctions.animateLeft());
contact_info.setAnimation(commonFunctions.animateRight());
personal_info.setVisibility(view.GONE);
professional_info.setVisibility(view.GONE);
contact_info.setVisibility(view.VISIBLE);
}
break;
case R.id.bt_continue_2:
if (str_mobile.equals("") || str_state.equals("Select State") || strCity.equals("") || str_area.equals("") || str_address.equals("")) {
commonFunctions.showAlert(this, "INVALID", "Please fill all the mandatory details");
personal_info.setVisibility(view.GONE);
professional_info.setVisibility(view.GONE);
contact_info.setVisibility(view.VISIBLE);
} else {
header.setText("Professional Info");
contact_info.setAnimation(commonFunctions.animateLeft());
professional_info.setAnimation(commonFunctions.animateRight());
personal_info.setVisibility(view.GONE);
contact_info.setVisibility(view.GONE);
professional_info.setVisibility(view.VISIBLE);
}
break;
//
// if( code== KeyEvent.KEYCODE_BACK){
//
// }
case R.id.bt_save:
// AddUpdateJobResource async = new AddUpdateJobResource (personal_info.this, "0", "34588A34-E969-4723-84FE-E5409B66A5B7", "", str_first_name, str_last_name, gender_id, age_substring, nationality_id, "IND", "INDIA", state_code, str_state, str_city, str_area, str_address, str_mobile, profession_id, religion_id, "2", months_substring, yrs_substring, "0.00", "0.00", "hssuraksha");
// async.execute ();
AddResources addResources = new AddResources();
addResources.execute();
}
}
请提出建议
答案 0 :(得分:1)
我完全不了解你的问题。您的意思是设备的后退按钮或应用程序中的自定义设计按钮?在后一种情况下,您只需调用finish() - 按下按钮时的Activity方法。
case R.id.bt_back:
// finish current view and go back to last view
finish();
答案 1 :(得分:0)
听起来您可能希望将视图A和视图B分成片段而不是隐藏/显示其特定数据。如果您仍想使用您的范例,则可以覆盖onBackPressed以在按下时显示视图A.
@Override
public void onBackPressed() {
Log.d("YOUR_APP", "onBackPressed Called");
//Show/Hide stuff here
}
答案 2 :(得分:0)
后退按钮适用于活动,而不适用于视图。
默认的后退逻辑是(好吧,或多或少)&#39;关闭当前活动并返回 之前的那个&#39;。
如果您想覆盖此行为,请将以下代码添加到您的活动中:
@Override
public void onBackPressed() {
if (v1.getVisibility() == view.VISIBLE) {
// hide v1, show v2, v3
v1.setVisibility(view.GONE);
v2.setVisibility(view.VISIBLE);
v3.setVisibility(view.VISIBLE);
}
else if (v2.getVisibility() == view.VISIBLE) {
// hide v2, show v1, v3
v2.setVisibility(view.GONE);
v1.setVisibility(view.VISIBLE);
v3.setVisibility(view.VISIBLE);
}
else if (v2.getVisibility() == view.VISIBLE) {
// close activity
finish();
}
}
或您的应用程序需要的任何其他后退按钮逻辑。
答案 3 :(得分:0)
你的意思是你想从活动3导航到活动1吗?根据您的用例,您可以覆盖标准任务和Backstack行为。 http://developer.android.com/guide/components/tasks-and-back-stack.html