我有一个FrameLayout并通过单击按钮放置一些片段,下一次单击应该从FrameLayout中删除片段,我通过removeAllViews()
执行此操作(FrameLayout在另一个片段中,因此translaction方法在Activity中)。
我需要在removeAllViews()
启动时执行一些操作,并且必须在Fragment类中执行此操作,但出现问题。
我试过了:
OnDestroy()
OnDestroyView()
OnPause()
在Fragment类
但它的作用如下:
removeAllViews()
(来自活动)OnDestroy()
)都可以工作(可能是destroy
旧片段的实时)当用户不存在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();
}
我在另一个片段中使用此方法:
((MainActivity) getContext()).showProductsList("carb", carbContainer.getId());
出现错误:
Error:(560, 21) error: cannot find symbol method setOnSystemUiVisibilityChangeListener(<anonymous OnSystemUiVisibilityChangeListener>)
答案 0 :(得分: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;
}
}