Android – action bar item Click handler in fragment

I have a simple 2 activity application. The main activity fills in listfragment and the second activity fills in fragments with fields to add custom objects (list items) to the main activity

In the second activity, I have a "save" icon in the action bar. I'm trying to figure out how to click this button in the clip, so I can package textfields and pass them back to the activity through the interface

I tried to overwrite optionitemselected, but it didn't hit. What should I do?

resolvent:

OK, so the trick is in the fragment oncreate method, you have to call

setHasOptionsMenu(true);

Then all you have to do is overwrite the options itemselected in the clip and handle the action bar and click there!!

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.action_save : {
            Log.i(TAG, "Save from fragment");
            return true;
        }
    }
    return super.onOptionsItemSelected(item);
}

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>