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;
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 two activities.One of them has Fragment.
From this fragment I can go to another activity, but by clicking the button "home" it goes to previous activity instead of going to the fragment of the previous activity.
Image
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_closet);
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
Refere below code :
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
You need to override below method :
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId()== android.R.id.home) {
finish();
return true;
}
return false;
}
getSupportActionBar().setNavigationOnClickListener(new OnClickListener {
public void onClick(View v){
onBackPressed()
}
})
Since you want same behavior as of back button you should use onBackPressed();
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
onBackPressed();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
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;
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.
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
}