onOptionsItemSelected ActionBarActivity doesn't respect false return - android

I have some code that looks like this in an ActionBarActivity:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
Intent returnIntent = new Intent();
if (isNameNull()) {
namePicker.setError(getString(R.string.warning_name_should_not_be_empty));
Log.d(TAG, "child name is empty");
return false;
}
}
return super.onOptionsItemSelected(item);
}
This code is just supposed to display an error in the EditText. It worked fine when I was using FragmentActivty. But when I switched to ActionBarActivity with ToolBar this code displays the error and navigates back to parent activity. This could be a bug in ActionBarActivity. Any workarounds? Overriding onMenuItemSelected(int featureId, MenuItem item) doesn't work because it was made final in ActionBarActivity.

For ActionBarActivity, you can override onSupportNavigateUp() to do custom behavior when the Up button is pressed, rather than handling it in onOptionsItemSelected():
#Override
public boolean onSupportNavigateUp() {
if (isNameNull()) {
namePicker.setError(getString(R.string.warning_name_should_not_be_empty));
Log.d(TAG, "child name is empty");
return false;
}
return super.onSupportNavigateUp();
}
Per the Javadoc on the return value of that method:
returns true if Up navigation completed successfully and this Activity was finished, false otherwise.

Related

Android: using toolbar with homeAsUpButton does call onCreate instead of onResume

I use toolbar in activity like this:
Toolbar myToolbar = (Toolbar) findViewById(R.id.my_toolbar);
setSupportActionBar(myToolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
And this code in AppCombatActivity:
#Override
public boolean onSupportNavigateUp() {
onBackPressed();
return true;
}
In Manifest I defined parent activity.
It works. But everytime I press the button 'onCreate' is called on parent activity. But thats not what I want. Whenn I press the back-button on the device it goes back to previous activity and just calls onResume. That is what it also should do when I press the back button in the toolbar.
Any Ideas?
Thanks!
In your manifest file add
launchmode="singleTop"
in the parent activity's declaration.
Check out LorenzCK's answer:
https://stackoverflow.com/a/15933890/5987223
Add this for your requirement...
#Override
public boolean onCreateOptionsMenu(Menu menu) {
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
//Start activity which you want... to call onCreateMethod
return true;
default:
return super.onOptionsItemSelected(item);
}
}
This code works for me:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
**onBackPressed();**
return true;
default:
return super.onOptionsItemSelected(item);
}
}

Toolbar back button does not close current Fragment

I have an activity that loads a Fragment onCreate. And I have a toolbar on that activity that has a button that when clicked will add a new Fragment on top of the one that got created first.
Now on that toolbar, when clicked, I want to remove the newer fragment so that it just displaces the fragment that is in the bottom (older one). I've searched and I kinda have figured it out except for one thing.
I did this so that it tries to see if the toolbar button is pressed:
#Override
public boolean onKeyUp(int keyCode, KeyEvent objEvent) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
onBackPressed();
return true;
}
return super.onKeyUp(keyCode, objEvent);
}
#Override
public void onBackPressed() {
FragmentManager mFM = getSupportFragmentManager();
if(mFM.findFragmentByTag("NewFragment") != null){
mFM.beginTransaction().remove(mFM.findFragmentByTag("NewFragment")).commit();
}
}
The problem is, "keyCode" is only equals to KEYCODE_BACK when you click the "hardware's back button" and not the back button of the toolbar. What it gives me is keycode 58 and not 4 (KEYCODE_BACK).
I believe what your looking for is the NavigationListener for the Toolbar Widget:
mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onBackPressed();
}
});
but if your using a Theme with an ActionBar and setting a supportActionBar then do something :
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()){
case android.R.id.home:
onBackPressed();
break;
}
return true;
}
It's not clear from your question whether you're asking a Fragment management question (FragmentTransaction remove vs. replace), or a Toolbar button question. I assume the latter...
An Android Toolbar or ActionBar should have actions handled using:
#Override
public Boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case MY_BACK_BUTTON:
myBackPressedHandler();
return (true);
}
return (false);
}
this assumes that your Back button is added as a MenuItem and was given an ID of MY_BACK_BUTTON.
For a Toolbar, you can use a MenuItemClickListener to handle the click events:
mToolbar.setOnMenuItemClickListener(MenuItem menuItem)
{
#Override
public Boolean onMenuItemClick(MenuItem menuItem)
{
// call onOptionsItemSelected, or handle the click here directly
return (thisFragment.onOptionsItemSelected(menuItem));
}
});
If you're referring to the "Up" button on the Toolbar/ActionBar, i.e. setDisplayHomeAsUpEnabled(true), this will have a getItemId() of android.R.id.home and can be handled in a similar fashion:
case android.R.id.home:
myBackPressedHandler();
return (true);

How to check if the back button of the ActionBarSherlock was clicked?

I'm using the library ActionBarSherlock, and put a item with the property android:showAsAction="ifRoom|collapseActionView". How to check if the back button of the ActionBarSherlock was clicked? thanks!
you have to override
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
}
return true;
}
from the doc:
his hook is called whenever an item in your options menu is selected.
The default implementation simply returns false to have the normal
processing happen (calling the item's Runnable or sending a message to
its Handler as appropriate). You can use this method for any items for
which you would like to do processing without those other facilities.
the answer above works (Thanks). But for my code, this solution works best ...
#Override
public boolean onOptionsItemSelected(
com.actionbarsherlock.view.MenuItem item) {
item.setOnActionExpandListener(new OnActionExpandListener() {
#Override
public boolean onMenuItemActionExpand(MenuItem item) {
// running changes ...
return true;
}
#Override
public boolean onMenuItemActionCollapse(MenuItem item) {
// running changes ...
return true;
}
});
return super.onOptionsItemSelected(item);
};

Don't repeat menu code

I'd like to have only one menu for all my activities. I don't want to repeat my menu code (below) in all my activities.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.referent, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.action_settings:
System.out.println("set");
return true;
case R.id.action_alert:
System.out.println("alert");
return true;
default:
return super.onOptionsItemSelected(item);
}
}
I read some topics, but I found just one solution witch is to extends a parent class who declare the menu. I can't use this solution because all my activities are not extending Activity, I have also have FragmentActivity and ListActivity.
Is there a solution to have the same menu on each activity writing a minimum of code on each activity?
Depending on what the menu handling code needs access to from the current activity, you could create a class whose only responsability is to handle the selected menu items. Possibly even with just a static method that receives the MenuItem.
For example, modify activities such that the onOptionsItemSelected is:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
boolean handled = MenuHandler.onOptionsItemSelected(item);
if (!handled) {
handled = super.onOptionsItemSelected(item);
}
return handled;
}
and create the MenuHandler class:
public class MenuHandler {
public static boolean onOptionsItemSelected(MenuItem item)
{
// Handle item selection
switch (item.getItemId()) {
case 1: //R.id.action_settings:
System.out.println("set");
return true;
case 2: //R.id.action_alert:
System.out.println("alert");
return true;
default:
return false; //allow default processing
}
}
}
all what you need is to extend from a main class
public abstract class main extends activity(){
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// put your common menu code
super.onOptionsItemSelected(item);
}
}
public class HelloActivity extends main{
#Override
public boolean onOptionsItemSelected(MenuItem item) {
super.onOptionsItemSelected(item);
}
}

How to implement a menuitem click listener inside a fragment in android

I have MenuItems in the ActionBar and I am using Fragments inside ViewPager. Now I would like to handle onMenuItemClickListener event inside my fragment. It works fine inside Main Activity. But not inside Fragments. And also it doesn't fetch any error.
Here is the methods that I tried. Both works fine inside Activity.
First method:
public boolean onCreateOptionsMenu(Menu menu) {
MenuItem item = menu.findItem(R.id.grid_view);
item.setOnMenuItemClickListener(new OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
Log.v("test","dfsfdsfasd");
return true;
}
});
return true;
}
Second Method:
public boolean onOptionsItemSelected(MenuItem item)
{
switch(item.getItemId())
{
case R.id.grid_view:
{
Log.v("Log:","grid_view item pressed");
return true;
}
case R.id.list_view:
{
Log.v("Log:","list_view item pressed");
return true;
}
default:
return true;
}
}
Any help on how to achieve this will be appreciated.
Solved by using onPrepareOptionsMenu method.

Categories

Resources