【问题标题】:Android when fragment is removed删除片段时的Android
【发布时间】:2017-03-19 13:17:00
【问题描述】:

我有一个 FrameLayout 并通过单击按钮将一些片段放在那里,下一次单击应该从 FrameLayout 中删除片段,我通过removeAllViews() 执行此操作(FrameLayout 在另一个片段中,因此翻译方法在 Activity 中)。 我需要在removeAllViews() 启动时执行一些操作,并且必须在 Fragment 类中执行此操作但出现问题。

我试过了: OnDestroy() OnDestroyView() OnPause() 在 Fragment 类中

但它的工作原理是:

  1. 将 Fragment 放入 FrameLayout(来自 Activity)
  2. 使用removeAllViews()(来自活动)
  3. FrameLayout 中没有 Fragment(很清楚),但没有其他反应,方法也不起作用
  4. 将新 Fragment 放入 FrameLayout(来自 Activity) - 现在所有方法(来自 Fragment 类的OnDestroy())都可以正常工作(可能是实时到destroy 旧片段)

当用户不存在 Fragment 时,如何“获取时刻”?如果用户隐藏 Fragment,我想向服务器发送一些信息。

@Edit3 来自我要进行翻译的 Activity 方法的代码

public void showProductsList(String productType,int containerID){

        List<String> prodNames = new ArrayList<String>();
        List<Long> prodIds = new ArrayList<Long>();

            DatabaseDAOProdProtein dao = new DatabaseDAOProdProtein(getApplicationContext());
            dao.open();
            List<DatabaseProduct> productList = dao.getAllProducts();
            for(int i=0;i<productList.size();i++){
                prodNames.add(productList.get(i).getName());
                prodIds.add(productList.get(i).getId());
            }
            dao.close();

        ProductsList productsList = new ProductsList(productType,prodNames,prodIds);

        productsList.setOnSystemUiVisibilityChangeListener
                (new View.OnSystemUiVisibilityChangeListener() {
                    @Override
                    public void onSystemUiVisibilityChange(int visibility) {
                        // Note that system bars will only be "visible" if none of the
                        // LOW_PROFILE, HIDE_NAVIGATION, or FULLSCREEN flags are set.
                        Toast.makeText(getApplicationContext(),"action1 " ,Toast.LENGTH_LONG).show();
                        if ((visibility & View.SYSTEM_UI_FLAG_FULLSCREEN) == 0) {
                            // TODO: The system bars are visible. Make any desired
                            // adjustments to your UI, such as showing the action bar or
                            // other navigational controls.
                            Toast.makeText(getApplicationContext(),"action2 " ,Toast.LENGTH_LONG).show();
                        } else {
                            // TODO: The system bars are NOT visible. Make any desired
                            // adjustments to your UI, such as hiding the action bar or
                            // other navigational controls.
                            Toast.makeText(getApplicationContext(),"action3 " ,Toast.LENGTH_LONG).show();
                        }
                    }
                });

        FragmentManager manager = getSupportFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();
        transaction.replace(containerID, productsList).commit();
    }

我在另一个 Fragment 中使用了这个方法:

((MainActivity) getContext()).showProductsList("carb", carbContainer.getId());

有一个错误:

Error:(560, 21) error: cannot find symbol method setOnSystemUiVisibilityChangeListener(&lt;anonymous OnSystemUiVisibilityChangeListener&gt;)

【问题讨论】:

    标签: java android android-studio android-fragments


    【解决方案1】:

    你说:

    “当 Fragment 不存在时,如何'获得时刻' 用户?如果用户隐藏,我想向服务器发送一些信息 片段。”

    我现在知道您的意思不是“隐藏”,所以只需使用 OnDestroy() 方法。

    试试这个来触发“隐藏”

    View topLevelLayout = findViewById(R.id.top_layout);
    topLevelLayout.setVisibility(View.INVISIBLE);
    

    片段(活动)可见时,您不能进入停止状态。 Android destroying activities, killing processes

    确保通过视图运行的最佳方法是通过帖子运行它:

    topLevelLayout.post(new Runnable()
    {
    @Override
    public void run()
    {
            topLevelLayout.removeAllViews();
        }
    }
    

    要获得系统 UI 可见性更改的通知,请将 View.OnSystemUiVisibilityChangeListener 注册到您的 view(片段)。

    https://developer.android.com/training/system-ui/visibility.html

    @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            Toast.makeText(getContext(),"action0 " ,Toast.LENGTH_LONG).show();
        Fragment your_frag = new ProductsList(productType,prodNames,prodIds);
    getSupportFragmentManager().beginTransaction().replace(containerID,your_frag).commit();
        getSupportFragmentManager().executePendingTransactions();//make sure onCreateView has executed
            your_frag.getRootView().setOnSystemUiVisibilityChangeListener
                    (new View.OnSystemUiVisibilityChangeListener() {
                        @Override
                        public void onSystemUiVisibilityChange(int visibility) {
                            // Note that system bars will only be "visible" if none of the
                            // LOW_PROFILE, HIDE_NAVIGATION, or FULLSCREEN flags are set.
                            Toast.makeText(getContext(),"action1 " ,Toast.LENGTH_LONG).show();
                            if ((visibility & View.SYSTEM_UI_FLAG_FULLSCREEN) == 0) {
                                // TODO: The system bars are visible. Make any desired
                                // adjustments to your UI, such as showing the action bar or
                                // other navigational controls.
                                Toast.makeText(getContext(),"action2 " ,Toast.LENGTH_LONG).show();
                            } else {
                                // TODO: The system bars are NOT visible. Make any desired
                                // adjustments to your UI, such as hiding the action bar or
                                // other navigational controls.
                                Toast.makeText(getContext(),"action3 " ,Toast.LENGTH_LONG).show();
                            }
                        }
                    });
        }    
    

    一个典型的片段如下所示:

    public class HomeFragment extends Fragment {
        View                        mRootView     = null;
        public HomeFragment(){}//null constructor
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
    
            mRootView = inflater.inflate(R.layout.fragment_home, container, false);
    
            return mRootView ;
        }
    public View getRootView ()
    {
        return mRootView;
    }
    }
    

    【讨论】:

    • 在创建片段时将监听器放在片段上(rootView)。
    • 我在活动中创建它(再次编辑我的帖子)。你能解释一下如何放置听众吗?
    • 这(我添加的代码)对你有意义吗?
    • 有没有办法在OnCreate()之外使用这个方法? (我不想在活动开始时加载片段,只需在另一个片段中通过((MainActivity) getContext()).showProductsList("protein", proteinContainer.getId());
    • 是的,您可以随心所欲。
    猜你喜欢
    • 2019-03-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多