简体   繁体   English

我想从片段中隐藏片段容器视图(在 MainActivity 布局内),但是当我单击任务按钮然后重新打开应用程序时它不起作用

[英]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.

相关问题 我想在 MainActivity 内部的一个片段中获取 EditText(来自 MainActivity),我得到一个 Nullpointer 错误 - I want to get the EditText (from the MainActivity) in a fragment inside off the MainActivity and i get a Nullpointer error 启动片段时如何隐藏MainActivity布局? - How to Hide MainActivity layout when fragment is launched? 当我尝试从mainactivity获取Fragment中的textview值时,我的应用无法正常工作 - when i tried to get the textview value in Fragment from mainactivity, my app is not working 如何通过在片段内单击按钮多次膨胀视图/容器? - How do I inflate a view/container multiple times by button click inside a Fragment? 我可以通过捆绑从适配器发送数据(当单击适配器中自定义布局的视图按钮时)到另一个片段? - can I send data from adapter by bundle( when click on view button from custom layout in adapter) to another fragment? 在android Studio中,我想要当我单击按钮时,下一个活动/片段应该来自右侧 - In android Studio, I want when i click on button , next activity/fragment should come from right side 我想使用回收者视图将数据从Firebase获取到内部片段 - I want to get data from firebase to inside fragment with recycler view 我想在 mainactivity java 类中使用片段 UI 元素 id 来完成 mainactivity 类而不是片段类上的单击侦听器事件 - I Want to use fragment UI element id in mainactivity java class to accomplish on click listener event on mainactivity class instead of fragment class 我想添加片段按钮,但它不起作用 - I want to add fragment button but it is not working 我无法使用片段从我的活动中访问片段布局内的文本视图 - I am not being able to access the text view inside my fragment layout from my Activity using the fragment
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM