Pramod Setlur Pramod Setlur - 26 days ago 16
Android Question

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?

Answer

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

Comments