简体   繁体   English

从另一个活动返回时,片段变为空白

[英]Fragment goes blank when returning from another activity

There are two activities in my app: 我的应用程序中有两项活动:

MainActivity (containing 3 fragments) MainActivity(包含3个片段)

  • FragmentHome 片段首页
  • FragmentOrders 片段订单
  • FragmentAccount 片段帐户

AccountEditActivity AccountEditActivity

The code to set fragments in MainActivity is this: 在MainActivity中设置片段的代码是这样的:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
    navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);

    // set Home as the default fragment
    setFragment(FragmentMainHome.getInstance());
}

private void setFragment(Fragment fragment){
    FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
    transaction.replace(R.id.frame_layout, fragment);
    transaction.commit();
}

The code for FragmentAccount is: FragmentAccount的代码为:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    rootView = inflater.inflate(R.layout.fragment_activity_main_account, container, false);
    layout = rootView.findViewById(R.id.layout_fragment_main_account);

    etName = (EditText) layout.findViewById(R.id.etNameLabelValue);
    etEmail = (EditText) layout.findViewById(R.id.etEmailLabelValue);
    etGender = (EditText) layout.findViewById(R.id.etGenderLabelValue);
    etPhoneNumber = (EditText) layout.findViewById(R.id.etPhoneNumberLabelValue);

    btnEditAccount = (ImageButton) layout.findViewById(R.id.btnEditAccount);
    btnManageAddresses = (ImageButton) layout.findViewById(R.id.btnAccountManageAddresses);

    btnManageAddresses.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
        }
    });

    btnEditAccount.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
        }
    });

    return rootView;
}

The code for FragmentOrders is: FragmentOrders的代码为:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    rootView = inflater.inflate(R.layout.fragment_activity_main_order_history, container, false);
    layout = rootView.findViewById(R.id.layout_fragment_main_order_history);

    lvOrders = (ListView) layout.findViewById(R.id.lvOrders);
    tvNoOrdersFound = (TextView) layout.findViewById(R.id.tvNoOrdersFound);

    final SwipeRefreshLayout pullToRefresh = rootView.findViewById(R.id.swipe_refresh_layout_order_history);
    pullToRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
        @Override
        public void onRefresh() {
            // load orders
            pullToRefresh.setRefreshing(false);
        }
    });

    lvOrders.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                                int position, long id) {
        }
    });

    return rootView;
}

and similar kind of code goes for FragmentHome FragmentHome也使用类似的代码

The Problem 问题

I can move between fragments and the view works fine. 我可以在片段之间移动,并且视图工作正常。 Now from Account fragment, I move to AccountEditActivity I do some update there and come back to MainActivity by pressing hardware back button 现在从Account片段中,我移到AccountEditActivity,在那里进行一些更新,然后按硬件后退按钮回到MainActivity。

FragmentHome is displayed fine but when I click on FragmentAccount, screen goes blank FragmentHome显示正常,但是当我单击FragmentAccount时,屏幕变为空白

Now, if I click on Home fragment and click FragmentAccount again, it displays fine. 现在,如果我单击“家庭”片段,然后再次单击“ FragmentAccount”,它会显示正常。

What is wrong here? 怎么了

better use viewpager in your activity to show the fragments it's easy to manage while using viewpagers 最好在活动中使用viewpager,以显示使用viewpagers时易于管理的片段

have a look at the link Fragments in viewpager 看一下viewpager中的链接Fragments

and in your case just move this code from onCreate to onResume 然后将您的代码从onCreate移到onResume

setFragment(FragmentMainHome.getInstance());

for understanding better have a look at the lifecycle of activity in android 为了更好地了解Android中的活动生命周期

Activity life cycle 活动生命周期

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从另一个片段返回时,主活动中的映射片段不可见 - Map fragment in main activity not visible when returning from another fragment 添加另一个片段时,片段活动为空 - Fragment Activity goes null when another fragment is added 片段在初始启动时或从其他活动返回后具有空白内容 - Fragment having blank content at initial startup or after returning from another activity 当按下另一个活动的排序按钮时,Android 中的 RecyclerView 变为空白 - RecyclerView in Android goes blank when pressing sort button from another activity Android-从Backstack返回时的空白片段 - Android - Blank fragment when returning from backstack ViewPager从另一个片段返回后具有空白页面的片段 - Fragment with ViewPager having blank pages after returning to it from another fragment 将数据从一个活动返回到另一个活动的片段? - Returning data from an activity to another activity's fragment? 从Fragment类打开时,活动显示为空白 - The activity is showing blank when it is opened from the Fragment Class 从另一个 Activity 返回后返回到 MainActivity 的片段? - Returning to MainActivity's fragment after return back from another Activity? 当程序从一种活动转到另一种活动时,应用程序崩溃 - Application crashes when program goes from one activity to another
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM