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
Related
i want to recreate (reload) a fragment when i click on a OptionsItem from a Drawer Navigation Activity
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch (id) {
case R.id.fr:
LocaleHelper.setLocale(getBaseContext(), "Fr");
if(getVisibleFragment().isAdded()){
getVisibleFragment().getActivity().recreate();
}
return true;
case R.id.ar:
LocaleHelper.setLocale(getBaseContext(), "Ar");
if(getVisibleFragment().isAdded()){
getVisibleFragment().getActivity().recreate();
}
return true;
case R.id.en:
LocaleHelper.setLocale(getBaseContext(), "En");
if(getVisibleFragment().isAdded()){
getVisibleFragment().getActivity().recreate();
}
return true;
case R.id.es:
LocaleHelper.setLocale(getBaseContext(), "Es");
if(getVisibleFragment().isAdded()){
getVisibleFragment().getActivity().recreate();
}
return true;
}
return super.onOptionsItemSelected(item);
}
when i click on an Option i get this error :
java.lang.IllegalStateException: Fragment NewsMainFragment{9a39a2f} not attached to a context
how can i achieve this properly ?
In this code:
getVisibleFragment().getActivity().recreate();
you recreate the whole activity, not the fragment. So, if the fragment you want recreate is in the activity where the method onOptionsItemSelected(MenuItem item) placed, you just recreate current activity, so you can call just:
recreate();
instead of your code.
But if you want just to recreate the fragment without recreating whole activity, you can put the code, where you initialize all views of your fragment, in separate public method and call them, like:
#Override
public void onResume() {
super.onResume();
updateUi();
}
public void updateUi() {
// Your code
}
and then:
case R.id.fr:
LocaleHelper.setLocale(getBaseContext(), "Fr");
if(getVisibleFragment().isAdded()){
((YourFragment)getVisibleFragment()).updateUi();
}
return true;
i am new at android.I am using three dots menu in my android app but when i click on item on on three dots menu..my app crashes.can someone help me here is my code:
enter code here#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.my_menu,menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id=item.getItemId();
switch (id){
case R.id.account:
Intent acc=new Intent(this,Account.class);
startActivity(acc);
break;
case R.id.setting:
Intent seting=new Intent(this,setting.class);
startActivity(seting);
break;
case R.id.feedback:
Intent feedback=new Intent(this,feedback.class);
startActivity(feedback);
break;
case R.id.help:
Intent help=new Intent(this,help.class);
startActivity(help);
break;
case R.id.faq:
Intent FAQ=new Intent(this,FAQ.class);
startActivity(FAQ);
break;
}
return true;
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.my_menu,menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.account:
Intent acc=new Intent(this,Account.class);
startActivity(acc);
return true;
case R.id.setting:
Intent seting=new Intent(this,setting.class);
startActivity(seting);
break;
case R.id.feedback:
Intent feedback=new Intent(this,feedback.class);
startActivity(feedback);
return true;
case R.id.help:
Intent help=new Intent(this,help.class);
startActivity(help);
return true;
case R.id.faq:
Intent FAQ=new Intent(this,FAQ.class);
startActivity(FAQ);
return true;
default:
return super.onOptionsItemSelected(item);
}
return true;
}
Seems there is nothing wrong with the code.
This could be issue with declration in manifest.
Make sure the activities account, help are declared in manifest.
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.
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);
}
Can you please tell me what is wrong with my code ? (I bet it's something stupid but i can't find it..)
My code :
public boolean onOptionsItemSelected(MenuItem item) {
return super.onOptionsItemSelected(item);
switch (item.getItemId()){
case R.id.action_go_home:
//go home action
Intent i = new Intent(this, UserProfileActivity.class);
startActivity(i);
return true;
break;
case R.id.action_select_categories:
//select categories
return true;
case R.id.action_refresh:
//refresh timeline
return true;
default :
return super.onOptionsItemSelected(item);
}
}
You have a return statement before your switch.
You should fix it like:
public boolean onOptionsItemSelected(MenuItem item) {
super.onOptionsItemSelected(item);
switch (item.getItemId()){
....