How to use onBackPress in android - 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
}

Related

Handle back press with ShowcaseView

How can I handle a user pressing the back button, whilst a ShowcaseView is showing? I want to be able to hide the ShowcaseView when they press back.
As linked from https://github.com/amlcurran/ShowcaseView/issues/376
ShowcaseView allows you to query if it is showing or not. So, if a user presses back and it is showing, simply hide it using Activity#onBackPressed():
#Override
public void onBackPressed() {
if (sv.isShowing()) {
sv.hide();
} else {
super.onBackPressed();
}
}
Try this :
#Override
public void onBackPressed() {
// in if condition check showcaseview is show or hide
if(isShowcaseViewShow==true) {
// here hide your showcase view
}
else{
// here perform back action if view already hide
super.onBackPressed();
}
}

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

On back press go back to previous view

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

Android: Finish activity when SlidingMenu (jfeinstein) is showing

I'm trying to implement the sliding menu created by jfeinstein in my app. The behavior I want to achieve is that when I press the back button and the menu is not showing, it will show up. This works great.
However when the menu is showing pressing the back button should finish the current activity. What happens instead is that the sliding menu is just closed again.
To achieve the described behavior I have overwritten onBackPressed:
#Override
public void onBackPressed()
{
SlidingMenu sm = getSlidingMenu();
if(!sm.isMenuShowing())
{
sm.showMenu();
}
else
{
finish();
}
}
But as soon as the sliding menu is showing onBackPressed isn't called anymore. I suspect as I have to use a SlidingFragmentActivity pressing the back button triggers the fragment history stack to pops.
Does anybody know how to solve this problem?
After browsing the SlidingMenu code a little further I found that onKeyUp is overwritten in SlidingFragmentActivity. So the way to go is to override onKeyUp in your activity.
#Override
public boolean onKeyUp(int keyCode, KeyEvent event)
{
if(keyCode == KeyEvent.KEYCODE_BACK)
{
SlidingMenu sm = getSlidingMenu();
if(sm.isMenuShowing())
{
finish();
return true;
}
}
return super.onKeyUp(keyCode, event);
}

Android on back press user should remain in the same page

In my activity page i have two option- normal screen and edit mode screen.Both screen are in the same activity.
When the user enters the activity the normal page is shown.when he clicks on a button the edit mode is shown.if normal screen is there then on back press user should go back to the previous activity.But if edit mode is on and the user clicks on back button then instead of going back to the previous activity,user should be shown the normal screen.
my onbackpressed method is
if(inputSearch.getVisibility()== View.VISIBLE){
Intent intentList = new Intent(getApplicationContext(),listsActivity.class);
intentList.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intentList);
}
else{
imgEditList.setSelected(false);
editMode.setVisibility(View.GONE);
inputSearch.setVisibility(View.VISIBLE);
}
super.onBackPressed();
What is the use of finish().Does it close the present activity or...
Try this
#Override
public void onBackPressed() {
if(inputSearch.getVisibility()== View.VISIBLE){
Intent intentList = new Intent(getApplicationContext(),listsActivity.class);
intentList.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intentList);
}
else{
imgEditList.setSelected(false);
editMode.setVisibility(View.GONE);
inputSearch.setVisibility(View.VISIBLE);
return;
}
super.onBackPressed();
}

Categories

Resources