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();
}
Related
I'm new to Android development, even to the androidOS, this is a more of a experience question on how do I approach on a OK or Cancel for a list of choices, Let's say a list of languages with a name and a checkbox next to it.
There's a back arrow button at the bottom of screen for each app, does that mean Cancel or that means I'm OK with the selection? I also noticed a toolbar back button, does that mean OK or Cancel? (attached screenshot).
What's the expectation of user for each of these back buttons so I can program my app accordingly?
I see hitting back as neither OK nor Cancel but a mechanism to navigate back to a previous Activity or Fragment. I usually refer to the back button to the left of the toolbar as the "in-app" back button, where as the system back button as the "system" back button, or "hardware" back button depending on the device.
You can also change the default behavier of the toolbar back arrow button with this code
public boolean onOptionsItemSelected(#NonNull MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
try {
int id = item.getItemId();
switch (id) {
case android.R.id.home: {
//anything you want here
return true;
}
case R.id.settings: {
Intent intent = new Intent(this, Settings.class);
startActivity(intent);
return true;
}
}
} catch (Exception e) {
e.printStackTrace();
}
return super.onOptionsItemSelected(item);
}
It normally means cancel, you can change that with this piece of code:
#Override
public void onBackPressed{
//anything you want here
}
I have a progress dialog, I have set progressDialog.setCancelable(false);,but i want to give user the option to use back button and go to previous activity.
Is it possible to enable back button when setCancableis set to false
Just finish your activity when the user presses the back button. Then they should return to the previous activity
#Override
public void onBackPressed() {
finish();
}
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
}
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
In my app..there are 2 activities i.e.
page1 and page2.
flow is page1-->page2
On page1, i have used many checkboxes. After selection of one checkbox, when i presses the "next" button, it goes to page2 activity showing the result but when i presses back button and select another checkbox, it is still showing the same result on page2 i.e. of previous selection.
I m new to android.please help me out.Thanks in advance.
i thik you should mentain page1 using
#Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
// here you checkyour checked checkbox items and make it unchecked
}
this mayhelps you
I thought you were having your own back button..if you're using the phone's back button, try this code to finish the current activity and go to the previous activity [this is to be added to page2 activity]:
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
finish();
return true;
}
return super.onKeyDown(keyCode, event);
}
If you want to implement your own back button, under the listener for the button, use an intent to start an activity to go back to the previous activity..
Intent intent = new Intent(Page2ActivityFileName.this, Page1ActivityFileName.class);
startActivity(intent);
finish();
See you're probably using startactivity(intent); whereas in this case you should use startactivityforresult(intent); Hope you get it, if not drop a comment I'll elaborate my answer with some code.