Actionbar menu item action

Asked 2 years ago, Updated 2 years ago, 65 views

Nice to meet you.Please give me some advice.

This is about how it works when you click menuItem on ActionBar on Android.
I often look at things like Toast, but I can't find any screen transitions (from Actionbar menu to ListFragment).

in onOptionsItemSelected I would appreciate it if you could tell me how to specify the screen transition.
Currently, we are trying as follows.However, if you press menu_home, it will fall off.
How should I rewrite it?
The transition destination is ListFragment.

@Override
public boolean onOptionsItemSelected(MenuItem){
    FragmentManager mFragmentManager=getFragmentManager();

    switch(item.getItemId()){

        case R.id.menu_home:
            Fragment mEvent = new Event();
            mFragmentManager.beginTransaction().replace(R.id.menu_home,mEvent).commit();
            break;
    }
    return true;
}

android android-fragments

2022-09-30 19:35

2 Answers

replace(R.id.menu_home,mEvent)

The reason is that I am trying to replace the menu button in the mEvent fragment


2022-09-30 19:35

If you look at each feature, you'll see

beginTransaction()Start changing the fragment

commit()Confirm the fragment changes

replace(A,B)Displays the fragments specified in B in the View id specified in A.
Delete any fragments there.

R.id.menu_homeThis is the id assigned to each option menu

show()Displays fragments assigned to View

View.

Does the log show the following when it drops?

No view found for id~

As you can see, R.id.menu_home is not the View id.
What I will give to replace(A,B) is the View id that I want to display.


2022-09-30 19:35

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.