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

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);
}
}

Related

Option menu option not directing to another activity

I want to navigate to another Activity when click on the option menu item 'settings' from the menu bar. Nothing actual happens.I have checked similar issues posted here, but i can understand why this is not working for option menu.
see the code below:
Can't go to a new activity from selected option from option menu
<item
android:id="#+id/mySettings"
android:title="#string/action_settings" />
<item
android:id="#+id/logout"
android:title="log out" />
code:
public class Dashboard extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dashboard);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.app_bar_menu,menu);
return super.onCreateOptionsMenu(menu);
}
public void openConfigure(){
Intent intent = new Intent(this,Configure.class);
this.startActivity(intent);
}
public boolean onContextItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.mySettings:
openConfigure();
break;
default:
return super.onOptionsItemSelected(item);
}
return true;
}
Use onOptionsItemSelected instead of onContextItemSelected cause you are using OptionMenu not ContextMenu.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.mySettings:
openConfigure();
break;
}
return super.onOptionsItemSelected(item);
}
to select an options menu item, you have to override onOptionItemSelected() :
try below code
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.mySettings:
openConfigure();
break;
}
return true;
}

BaseActivity with common toolbar and menuItems activities can not catch onMenuItemClick

I've the base activity where all toolbar initializations and options menu are done, the activities extending the base can not fire onitemclick
In the base i have
public class BaseActivity extends AppCompatActivity {
private MenuItem refresh;
public Toolbar getToolbar() {
return toolbar;
}
public MenuItem getRefresh() {
return refresh;
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
refresh = menu.findItem(R.id.action_refresh);
refresh.setActionView(R.layout.menu_item_view);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
onBackPressed();
break;
}
return false;
}
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
switch (mState) {
case Const.STATE_IDLE:
refresh.setVisible(true);
break;
case STATE_WORKING:
refresh.setVisible(false);
break;
default:
refresh.setVisible(true);
break;
}
return super.onPrepareOptionsMenu(menu);
}
}
In one of the activites I handle it like
public class CommentsActivity extends BaseToolbarActivity
{
#Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
super.onOptionsItemSelected(item);
if(item.getItemId() == R.id.action_refresh){
setState(Const.STATE_WORKING);
showMsg(contentRoot,"oops");
return true;
}
return super.onOptionsItemSelected(item);
}
}
but the click items won't fire
After a while I came to realize that since I was setting a custom layout for my items (useful for animations) an option menu with a custom view that is either set in the xml or dynamically using
item.setActionView(R.layout.menu_lay);
Just like my problem above the menu item can never be invoked by the normal onOptionsItemSelected listener so the way to make it work is to implement onClickListener on the item's custom View so in my case the way to make it invoke is by
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getRefresh.getActionView().setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
//refresh some data
}
});
return true;
}

onOptionsItemSelected ActionBarActivity doesn't respect false return

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.

simulate onOptionsItemSelected inside method call

I have an ImageView inside a xml layout. The ImageView has an onClick method.
android:onClick="onHomeClicked"
When the user clicks my image, I want that to in turn call onOptionsItemSelected inside the activity. How would I do that?
The following code gives null for homeMenuItem:
MenuItem homeMenuItem;
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
homeMenuItem = menu.findItem(android.R.id.home);
super.onPrepareOptionsMenu(menu);
return true;
}
public void onHomeClicked(View view) {
onOptionsItemSelected(homeMenuItem);
}
You have no MenuItem, whatever logic onOptionsItemSelected() would contain would fail (assuming there are multiple options).
Since you seem to have a need for shared code, move that piece of logic to its own method, then call that method from both onHomeClicked() and onOptionsItemSelected().
Eg
private void mySharedMethod (){
//implementation
}
public void onHomeClicked (View v){
mySharedMethod();
}
public boolean onOptionsItemSelected (MenuItem item){
switch (item.getItemId ()){
case android.R.id.home:
mySharedMethod();
return true;
default:
return super.onOptionsItemSelected (item);
}
}
Possible you have to declare method and call it from onOptionsItemSelected and from onHomeClicked
onOptionsItemSelected(MenuItem item){
switch(item.getItemId()) {
case R.id.btn:
youNewMethod();
break;
}
}
public void onHomeClicked() {
youNewMethod();
}
private void youNewMethod(){
// write your code here
}

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