0

I am try to user one activity in my project。 in main fragment i user viewpage and tablayout,the ViewPager contain five fragment, in first fragment contain two fragment i form one fragment navigation to new fragment, when back i lose all data in the ViewPager's fragment.

just like this enter link description here

as @Ail tell me. i fix this bug.

Code before:

protected View mView;

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    mView = inflater.inflate(getRootView(), container, false);
    initView();
    initData();
    return mView;
}

Code after:

public View mView;

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    if (mView == null) {
        mView = inflater.inflate(getRootView(), container, false);
        initView();
        initData();
    }
    return mView;
}
Ven Ren
  • 1,584
  • 1
  • 13
  • 24

1 Answers1

1

You are having one Activity which means you are using architecture Navigation component.

When you navigate back to main screen, your callback methods like onViewCreated recalled. You should keep a reference to the View you created the first time in main screen and return it again, as suggested by Ian Lake :

https://twitter.com/ianhlake/status/1103522856535638016

Also look at Fragments destroyed / recreated with Jetpack's Android Navigation components

Ali
  • 9,800
  • 19
  • 72
  • 152