【发布时间】:2017-03-19 13:17:00
【问题描述】:
我有一个 FrameLayout 并通过单击按钮将一些片段放在那里,下一次单击应该从 FrameLayout 中删除片段,我通过removeAllViews() 执行此操作(FrameLayout 在另一个片段中,因此翻译方法在 Activity 中)。
我需要在removeAllViews() 启动时执行一些操作,并且必须在 Fragment 类中执行此操作但出现问题。
我试过了:
OnDestroy()
OnDestroyView()
OnPause()
在 Fragment 类中
但它的工作原理是:
- 将 Fragment 放入 FrameLayout(来自 Activity)
- 使用
removeAllViews()(来自活动) - FrameLayout 中没有 Fragment(很清楚),但没有其他反应,方法也不起作用
- 将新 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(<anonymous OnSystemUiVisibilityChangeListener>)
【问题讨论】:
标签: java android android-studio android-fragments