Pramod Setlur
Pramod Setlur

Reputation: 811

calling onOptionsItemSelected explicitly

I have a Save MenuItem. This has a onOptionsItemSelected(MenuItem item) function. It's triggered when I click on the Save menu. However, I want to call this function explicitely when the user tries to navidate to another activity without saving. So basically how can I call this onOptionsItemSelected(MenuItem item) from another function?

Upvotes: 0

Views: 2181

Answers (3)

Amit Jayaswal
Amit Jayaswal

Reputation: 1723

just call invalidateOptionsMenu().

Upvotes: 0

Pratik
Pratik

Reputation: 30855

Do one thing all the code you written in this method within for save just copy and paste in you created method for e.g.

onOptionsItemSelected(MenuItem item){

    switch(item.getItemId()){
    case R.id.save:
       saveMe();
    break;
    }
}

private void saveMe(){
    // write your save code here
}

now you can call this method when user navigate to another activity

Upvotes: 3

Amit
Amit

Reputation: 13364

Have a function named showSaveMenu() and inside it show the save menu if user has not saved already. Call this function from each possible exit point from the activity i.e. onBackPressed() or from any where you start another activity and also from onOptionsItemselected()....

Upvotes: 1

Related Questions