[英]i want to hide fragment container view (Inside MainActivity Layout) from fragment but its not working when i click task button and then reopen app
@Override
public void onResume() {
super.onResume();
Activity mainActivity = getActivity();
mFragmentViewContainer_MainActivity = mainActivity.findViewById(R.id.nav_host_fragment_containerView);
mFragmentViewContainer_MainActivity.setVisibility(View.GONE);
}
it works when i open app first time but when i click on task button of android and then reopen app fragment container view became visible again even它在我第一次打开应用程序时有效,但是当我单击 android 的任务按钮然后重新打开应用程序片段容器视图时再次可见
i set visibility GONE inside OnResume我在 OnResume 中设置了可见性 GONE
And don't really know if this work but try this !而且真的不知道这是否有效,但试试这个!
In your fragment:在您的片段中:
public class MyFragment extends Fragment {
private MyFragmentListener mCallback;
public interface MyFragmentListener{
public actionFromParent(int visibility);
}
@Override
public void onAttach(@NotNull Context context) {
super.onAttach(context);
mCallback = (MainActivity) context;
}
@Override
public void onResume() {
super.onResume();
mCallback.actionFromParent(View.INVISIBLE);
}
}
And your MainActivity:还有你的 MainActivity:
public class MainActivity extends AppCompatActivity implements MyFragment.MyFragmentListener {
private View view;
@Override
protected void onCreate(Bundle savedInstanceState) {
// ...
view = findViewById(R.id.nav_host_fragment_containerView);
// ...
}
@Override
public void actionFromParent(int visibility){
view.setVisibility(visibility);
}
}
I don't know if this is a good think to use set visibility from onResume.我不知道使用 onResume 中的设置可见性是否是一个好主意。 Better to use it with a button except if you know what are you doing.
除非您知道自己在做什么,否则最好将其与按钮一起使用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.