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
}
Related
I'm writing a library that starts a flow of activities when the user does some action. If back is never pressed, the flow should be:
Application using library detects some action on ParentAppScreen1 >
LibraryActivity1: user taps next >
LibraryActivity2: user taps send >
user is returned to ParentAppScreen1
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
// Respond to the action bar's Up/Home button
case android.R.id.home:
this.finish();
return true;
case R.id.action_send:
sendReport();
// TODO go back to where the user started in parent app
}
return super.onOptionsItemSelected(item);
}
Is there any kind of standard practice for this? I'm not finding much when browsing documentation, and I'm not really sure where to start.
Can i make my Action Bar app icon clickable without displaying the back icon?
This is my code, it works, I have only layout problem:
actionBar.setDisplayHomeAsUpEnabled(true);
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
//Do stuff
return true;
default:
return super.onOptionsItemSelected(item);
}
}
This is my activity layout, what I want is remove back icon, is it possible?
Try to use setHomeButtonEnabled(boolean enabled) instead of setDisplayHomeAsUpEnabled(boolean enabled). The latter do exactly the same thing as former which is enabling home button but additionally put up affordance sign which you want to get rid off.
I am using the new Android L transition, in particular shared element transitions along with a Slide(). When I press the back button the transitions work perfectly, it slides and transitions the shared ImageView to the correct spot but when I press the home-up button in the ActionBar it ignores the new transitions.
I set this block of code in the receiving activity:
getWindow().requestFeature(Window.FEATURE_CONTENT_TRANSITIONS);
getWindow().setExitTransition(new Slide());
getWindow().setEnterTransition(new Slide());
And this block of code in my 'Main' Activity:
getWindow().requestFeature(Window.FEATURE_CONTENT_TRANSITIONS);
Transition transition = new Slide();
getWindow().setSharedElementEnterTransition(transition);
getWindow().setSharedElementExitTransition(transition);
Make sure you call finishAfterTransition() when the action bar's up button is clicked:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
finishAfterTransition();
return true;
}
return super.onOptionsItemSelected(item);
}
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();
}
How I can change working of home button in android? I want to when I click on home button I do some actions and after that application should go to background. How I can do that.
Write your own home screen. When the user presses HOME, they will get a choice of running your app or the built-in home screen. They can elect to choose one just this one time, or set either app as being their default from now on. Many will wonder why on Earth you decided to decided to implement a home screen.
Most developers care about any case where their activity moves to the background, in which case you can either use a lifecycle method (e.g., onPause(), onStop()) or try onUserLeaveHint().
I do it this way:
/* Handles item selections */
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
//You can do whatever you want here
Intent homeInt = new Intent(this, SomeActivity.class);
startActivity(homeInt);
return true;
}
return false;
}
Have you tried overriding onKeyDown()
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_HOME) {
// ENTER CODE HERE
return true;
}
return super.onKeyDown(keyCode, event);
}