sanjana
sanjana

Reputation: 230

Show/hide menu based on condition for fragment

i have one fragment that displays list with help of adapter.in list there is a link click in which user calls another activity that calls an adapter to get data .After getting data we can add ata and save in that adapter this directs user to fragment 1 to save data .

.initially I don't want menu as save in fragment1. Once the user add data in onclick of the list which opens another list to add data and returns to fragment1 then I need to display menu save.....I have tried.

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setHasOptionsMenu(true);
}


@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.action_save, menu);
    shareItem = menu.findItem(R.id.action_save1);
    shareItem.setVisible(false);

}

Later based on condition I am doing: shareItem.setVisible(true);

Upvotes: 2

Views: 1864

Answers (4)

androidcodehunter
androidcodehunter

Reputation: 21937

As you are starting another activity for result, you can use startActivityForResult(). After getting the data, you can enable the menu. Here you can get more information How to manage startActivityForResult on Android?

For an example, when you start activity set the result code for example 1. Then after getting the activity data, finish the second activity like this way

Intent returnIntent = new Intent();
setResult(Activity.RESULT_OK,returnIntent);
finish();

And in your onActivityResult you can check if the result ok then enable your menu.

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
     if (requestCode == 1) {
            if(resultCode == Activity.RESULT_OK){
               //data is updated. 
             myitem.setvisiblity(true)
            }        
        }
    }

Upvotes: 1

S Haque
S Haque

Reputation: 7271

before opening another list you can set a boolean value to true and when you get back to the fragment1 again setUserVisibleHint() method will be called and you can check that boolean value and isVisibleToUser variable value. If both of them are true then you can visible the menu item.

@Override  
public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
}

Or If you are opening another activity for getting data you can use startActivityForResult() method. When you sends back the data to Activity. It's onActivityResult() will be called. From where you can check the requestCode and all your fragments startActivityForResult() method. From where you can set the menu item.

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1001) {
            Fragment fragment1 = (Fragment) getSupportFragmentManager().findFragmentByTag(FRAGMENT_TAG);
            if (fragment1 != null) {
                fragment1 .onActivityResult(requestCode, resultCode, data);
            }
        }
    }

Upvotes: 0

rafsanahmad007
rafsanahmad007

Reputation: 23881

In your fragment override the onPrepareOptionsMenu() and hide the item based on your condition

 @Override
public void onPrepareOptionsMenu(Menu menu) {
    MenuItem item=menu.findItem(R.id.action_save1);
    item.setVisible(false);
    super.onPrepareOptionsMenu(menu);
}

Also make sure you call setHasOptionsMenu(true); in onCreate()

From Documentation

boolean onPrepareOptionsMenu (Menu menu) Prepare the Screen's standard options menu to be displayed. This is called right before the menu is shown, every time it is shown. You can use this method to efficiently enable/disable items or otherwise dynamically modify the contents.

Upvotes: 0

Sergio
Sergio

Reputation: 30595

Add boolean variable as field in activity isShareItemVisible = true, then when conditions are changed set it to false and call invalidateOptionsMenu();. In the onCreateOptionsMenu method use this variable, i.e.:

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.action_save, menu);
    shareItem = menu.findItem(R.id.action_save1);
    shareItem.setVisible(isShareItemVisible);
}

Upvotes: 0

Related Questions