On back press go back to previous view - android

I have an Activity in which i have 3 views.On click of a button i have hidden one view and displayed other one.But my doubt is that i cannot go back to the previous view.How should i do this.
For eg: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.
Code
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();
}
}
Please do suggest something

I don't understand your problem exactly. Do you mean the back button of the device or the a custom designed button in your application? In the latter case you can simply call the finish()-Method of your Activity when the button is pressed.
case R.id.bt_back:
// finish current view and go back to last view
finish();

It sounds like you might want to make view A and view B into fragments instead of hiding/showing their specific data. If you still want to use your paradigm you could override the onBackPressed to show view A when it is pressed.
#Override
public void onBackPressed() {
Log.d("YOUR_APP", "onBackPressed Called");
//Show/Hide stuff here
}

The back button works on activities, not on views.
The default back logic is (well, more or less) 'close current activity and return to
the one before'.
if you want to override this behavior add code like below to your activity:
#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();
}
}
or whatever other back-button logic your application requires.

Do you mean that you want to navigate from activity 3 to activity 1? Dependend on your use case you could override the standard task and backstack behavior. http://developer.android.com/guide/components/tasks-and-back-stack.html

Related

How to use onBackPress in android

In my application I am using navigation drawer menu. I need to close this menu and get back to previous activity, when user clicks on back button.
I wrote below code :
#Override
public void onBackPressed() {
if (toNightSearchView.isSearchOpen()) {
toNightSearchView.closeSearch();
} else if (toNightDrawerLayout.isShown()) {
toNightDrawerLayout.closeDrawer(Gravity.END);
} else {
super.onBackPressed();
}
}
The problem is when menu is closed app does not back to previous activity.
How can I fix this?
// use `isOpen()` to check the drawer state
if (toNightSearchView.isSearchOpen() || toNightDrawerLayout.isOpen()) {
//close if any of the views are open
if (toNightSearchView.isSearchOpen())
toNightSearchView.closeSearch();
if (toNightDrawerLayout.isOpen())
toNightDrawerLayout.closeDrawer(Gravity.END);
// Gravity.END is used for closing right drawer and start for left one
} else{
super.onBackPressed(); // go back to previous activity
}
or better use isDrawerOpen
if (toNightSearchView.isSearchOpen() || toNightDrawerLayout.isDrawerOpen(Gravity.START)) {
if (toNightSearchView.isSearchOpen())
toNightSearchView.closeSearch();
if (toNightDrawerLayout.isDrawerOpen(Gravity.START))
toNightDrawerLayout.closeDrawer(Gravity.START);
} else{
super.onBackPressed(); // go back to previous activity
}

How to close activity in android?

I have a menu button on the bottom and I have display menu icons from another activity like MenuTask.
But how to close that MenuTask activity if the user clicks again in the menu?
imgMenu.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
if (key == 0) {
// I want to show menu here
key = 1;
Intent intent = new Intent(MainActivity.this, MenuAction.class);
startActivityForResult(intent, 2);
setResult(0);
} else if (key == 1) {
// I want to Delete menu here
key = 0;
//onBackPressed();
finish();
}
}
});
Note: I think if i click second time , button could not fire. it means else part not execute.
Problem:
I have Clicked, right top menu button and open new activity menu action. but if i want to click again same menu button, i want to disappers that menu action. but menu button could not clicked.
How to make MenuButton Clickable? any idea? any other menthod? But when i click mobile backbutton menuaction layout disappeared.
Thanks in advance.
I thing this is not best way. You must use Fragment
If you want only kill activity MenuAction.this.finish();
#Override
public void onBackPressed() {
if (doubleBackToExitPressedOnce) {
super.onBackPressed();
return;
}
this.doubleBackToExitPressedOnce = true;
Toast.makeText(this, "Please click BACK again to exit", Toast.LENGTH_SHORT).show();
// handler to delay
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
doubleBackToExitPressedOnce=false;
}
}, 2000);
}
you can go though this link Clicking the back button twice to exit an activity
same question

Is it possible for the Back button to take the user to a specific Activity given certain conditions, and behave like normal in other conditions?

I'd like the back button to have behaviour similar to e.g.
// if I have an Item count > 1 and I'm not on ItemsListActivity
// back button takes me to ItemsListActivity
// else
// back button behaves like normal
Is this possible? If so, what do I need to do?
Yes.
Override onBackPressed()
(http://developer.android.com/reference/android/app/Activity.html#onBackPressed())
Called when the activity has detected the user's press of the back key. The default implementation simply finishes the current activity, but you can override this to do whatever you want.
So you could do something like:
#Override
public void onBackPressed(){
if (count > 1) && (!(this instanceof ItemsListActivity)) {
// Launch ItemsListActivity / do whatever you want
}
else {
super.onBackPressed(); // Do the normal back press functionality
}
}
There is probably a better way to check what activity you are in!
Is it possible: you have to override onKeyDown:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// Check if the key event was the back button
if ((keyCode == KeyEvent.KEYCODE_BACK) && itemCount > 1) {
// Back button takes you to ItemsListActivity
return true;
}
// Otherwise execute requested action
return super.onKeyDown(keyCode, event);
}
By the way, when user hit back, normally they expect to return on previous activity or exit app. IMHO it is better to make another way (button or menu item) to access on your ItemsListActivity.

Android:ViewFlipper with onBackPressed

Ia m using ViewFlipper in my app, and to move inside the pages, I use a Button for next and capturing the onBackPressing to return back.
the behavior is the following:
1) I click on button and move to 2 page.
2) click back and code work
3) click again on the button next
4) click back and now wont work anymore
on the step 4, I can feel the vibration, so the event fire, but my viewflipper wont to go back.
Any suggestion?
Thank's
public void onBackPressed() {
if (flipView.getDisplayedChild() == 1) {
flipView.setDisplayedChild(0);
} else if (flipView.getDisplayedChild() == 0) {
flipView.setDisplayedChild(1);
}
}
This works perfectly for me. Change onBackPressed to what ever method is calling the back button.
Just to add something to the other answer.
Let's say we have only two views that we are flipping through, doing:
public void onBackPressed() {
if (mViewFlipper.getDisplayedChild() == 1) {
mViewFlipper.setDisplayedChild(0);
} else if (mViewFlipper.getDisplayedChild() == 0) {
flipView.setDisplayedChild(1);
}
}
isn't enough. As a matter of fact, it creates another problem for you.
If the view is in 0 (the first), and you then press the back button, NOTHING happens. The activity doesn't exit. This is because you haven't called super.onBackPressed(). Now, adding super.onBackPressed() to the code above also creates another problem. When you flip from 1 (the second view) it goes to the first view (0) and then exits the activity, which is wrong if not for anything but for the weird animation of skipping a view while transitioning from one activity to another.
The best way to implement onBackPressed() for your activity containing a ViewFlipper is this:
public void onBackPressed() {
int displayedChildId = mViewFlipper.getDisplayedChild(); //get current view's number
if (displayedChildId > 0) { //if this number is greater than 0(let's say 5)
mViewFlipper.setDisplayedChild(displayedChildId - 1);//We then go down that number by 1. That is 5 - 1, which is 4. This happens until displayedChildId isn't greater than 0 anymore, which is then the first view. if we press back from here, we exit the activity.
} else {
super.onBackPressed();
}
}
I hope this makes sense

Back button and layout id detection

I'm trying to have my app display a toast message when "Back" is pressed when R.layout.main.xml is displayed, but not display the message from any other layout.xml . Also, when any other layout is displayed, I want it to return to the previous screen (like a back button should). If I left the back button default, whenever you pressed it, it would exit the application from any layout screen.
Here is my current code..... it keeps force closing and I cant figure out why:
private Toast toast;
private long lastBackPressTime = 0;
#Override
public void onBackPressed()
{
View thisView = getCurrentFocus();
int screen = thisView.getId();
if (screen == R.layout.main)
{
if (this.lastBackPressTime < System.currentTimeMillis() - 4000)
{
toast = Toast.makeText(this, "Press BACK once more to close this application", 4000);
toast.show();
this.lastBackPressTime = System.currentTimeMillis();
}
else
{
if (toast != null)
{
toast.cancel();
}
super.onBackPressed();
}
}
if (screen != R.layout.main)
super.onBackPressed();
}
In my apps I'm using constants for each layout and a currentView variable. Each time I change the content view I reassign this variable, so I'm always aware of where I'm currently being. Hope this helps you.

Categories

Resources