I have an activity menu and on one fragment I want to replace an item inside that menu. The item when pressed launches an activity. My fragment menu item works, but it also calls the activity menu item intent. I need to remove the duplicate main activity menu item being selected too.
Main Activity menu:
public boolean onOptionsItemSelected(MenuItem item) {
else if(itemId == R.id.action_settings)
startActivity (new Intent(getApplicationContext(),
PreferencesActivity.class));
return super.onOptionsItemSelected(item);
}
Fragment menu:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_settings:
startActivity(new Intent(getActivity(),
PreferencesFragment.class));
return false;
default:
break;
}
return false;
}
It is only if the activity doesn't use the action that it is passed to the fragment. You may be able to add in the activity a boolean that causes the switch to be bypassed for that item.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (!isMyFragmentPresent()) {
switch (item.getItemId()) {
case R.id.action_settings:
startActivity(new Intent(getApplicationContext(),
PreferencesActivity.class));
return true;
default:
break;
}
}
return false;
}
public void setMyFragmentIsPresent(boolean isMyFragmentPresent) {
this.isMyFragmentPresent = isMyFragmentPresent;
}
UPDATE: I realise this doesn't work if you have more items than the one you want to change. Maybe you would be better having a default Fragment that loads in the activity that does nothing but load the R.id.action_settings settings button and what it does. This fragment would then be replaced by your new Fragment.
Related
I'm converting some activities to several fragments , and now when I press back button it doesn't work.
what changes should I do in this fragment when its returning back to Previous activity and when returning to Previous fragment?
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.returnHome:
Intent i= new Intent(getActivity().getApplicationContext(), WoundNavigation.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP| Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(i);
return true;
case android.R.id.home:
getActivity().onBackPressed();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
Try like this
write this in onCreateView() method
setHasOptionsMenu(true)
and make these changes
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.returnHome:
Intent i= new Intent(getActivity(), WoundNavigation.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP| Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(i);
return true;
case android.R.id.home:
getActivity().onBackPressed();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
It may help. If this also doesn't works then you have to manage fragment backstack in activity from where you are calling your fragment
I have the following in my code and I want to switch to this new activity when I select it from the menu, but the app just keeps closing:
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.mi_baas:
startActivity(new Intent("com.my.project.BAAS"));
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
Please help, 2 days so far.....
You have to pass context and class which is to be opened.
Your code should be like this.
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.mi_baas:
startActivity(new Intent(getContext(),BAAS.class));
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
Hope it helps:)
so i have Activity A,B, and C. Activity A & B both go to activity C. When i am on activity C and I press the back home button on mySupportActionBar, I want to return to the state of activity (from the state i left it in) I came from. How would i accomplish this?
Here is my onOptionsItemSelected(). So currently, it goes back to the designated parent activity i assigned in manifest to avoid my app from crashing. Because the parent activites require strings from intents.
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
int id = item.getItemId();
if(id == android.R.id.home)
{
NavUtils.navigateUpFromSameTask(this);
}
return super.onOptionsItemSelected(item);
}
I would also love to accomplish this onBackPressed().
The back arrow button is the "home" button when you're in a inner activity so you could finish the inner activity or maybe just call the back button
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
onBackPressed();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
Use android.R.id.home
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
You also can use setNavigationOnClickListener on toolbar to trigger back button.
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_filter_category);
setSupportActionBar(toolbar);
if (getSupportActionBar() != null) {
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowTitleEnabled(false);
//back button action
toolbar.setNavigationOnClickListener(view -> finish());
}
}
I have android.support.v7.widget.Toolbar in my fragment.
Also I declared in my Fragment onViewCreated this:
((MainActivity) mCtx).setSupportActionBar(toolbar);
((MainActivity) mCtx).getSupportActionBar().setTitle(null);
((MainActivity) mCtx).getSupportActionBar().setDisplayHomeAsUpEnabled(true);
But I cant make that Toolbar button back to work no way.
How to do that?
Tried to:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
onBackAction();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
Instead of onBackAction(); use:
onBackPressed();
break;
how can I link my setting buttons in the action bar with a another activity.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()){
case R.id.about:
about(); // shows a dialogbox
break;
case R.id.settings:
// I want to link this with my setting screen
break;
}
return super.onOptionsItemSelected(item);
}
I linked up my about action bar button with a dialog box but for my settings, I want it to go to another activity. How can I do this? The activity I want to link to is called settings.java
How about this?
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()){
case R.id.about:
about(); // shows a dialogbox
break;
case R.id.settings:
startActivity(this, settings.class);
// This doesn't work?
break;
}
return super.onOptionsItemSelected(item);
}