Java – check the currently open clip position when using drawerlayout

I'm using Android drawerlayout in the support library to display the slide menu I use a single activity and 5-6 clips to display them in the drawerlayout menu But I have a small question, "how do I check which clip is currently visible if the user selects the menu item corresponding to the opened clip. At present, it creates the clip again and displays bad. The function triggered when clicking the menu item is:

private void selectItem(int position) {

        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        // Locate Position
        switch (position) {
        case 0:
            ft.replace(R.id.content_frame,fragment1);
            break;
        case 1:
            ft.replace(R.id.content_frame,fragment2);
            break;
        case 2:
            ft.replace(R.id.content_frame,fragment3);
            break;
        }
        ft.commit();
        mDrawerList.setItemChecked(position,true);
        // Close drawer
        mDrawerLayout.closeDrawer(mDrawerList);
    }

How do I check that the requested fragment is open so that it is no longer created? Is any of their methods checked through the fragmentmanager?

Solution

I'll add @ Plato's answer

The answer is that when you add fragments to a transaction, you can use tags to represent specific fragments It's like:

ft.replace(android.R.id.content,fragment,"MY_FRAGMENT");

If you want to check whether the clip is visible later, you can do the following:

RequestedFragment fragment = (RequestedFragment)getFragmentManager().findFragmentByTag("MY_FRAGMENT"); //"My_FRAGMENT" is its tag
if (fragment.isVisible()) {
   // add your code here 
}

I hope this will help

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
分享
二维码
< <上一篇
下一篇>>