主要活动:
问题是在三个fragment中的几次transition之后(大约3个transition之后),fragment的view一直在后台,也就是没有变化,应用没有crash,内存没有增长(used,check in the工作室)。
使用 cicerone 在碎片中移动
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()){
case R.id.certificationItem:
router.replaceScreen(FragmentScreenKeys.FRAGMENT_CERTIFICATION);
return true;
case R.id.planItem:
router.replaceScreen(FragmentScreenKeys.FRAGMENT_PLAN);
return true;
case R.id.chatItem:
router.replaceScreen(FragmentScreenKeys.FRAGMENT_CHAT);
return true;
default:
throw new RuntimeException("Unknown item.getId");
}
}
片段1:
private View view;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
((MainActivity)getActivity()).getComponent().inject(this);
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_certification, container, false);
return view;
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
ButterKnife.bind(this, view);
}
@Override
public void onResume() {
super.onResume();
holder.setNavigator(navigator);
}
@Override
public void onPause() {
holder.removeNavigator();
super.onPause();
}
@Override
public void onDestroyView() {
super.onDestroyView();
view = null;
}
@Override
public Fragment newInstance() {
return new FragmentCertification();
}
片段2:
private View view;
private LearningAdapter adapter;
@Inject
ApiService apiService;
@BindView(R.id.recyclerPlan) RecyclerView recyclerPlan;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
((MainActivity)getActivity()).getComponent().inject(this);
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_plan, container, false);
return view;
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
ButterKnife.bind(this, view);
initRecycler(view);
}
@Override
public void onResume() {
super.onResume();
showLearning();
}
private void initRecycler(View view){
recyclerPlan.setLayoutManager(new LinearLayoutManager(view.getContext(),
OrientationHelper.VERTICAL, false));
}
private void showLearning(){
adapter = new LearningAdapter(new ArrayList<>());
recyclerPlan.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
@Override
public void onDestroyView() {
super.onDestroyView();
view = null;
}
@Override
public Fragment newInstance() {
return new FragmentPlan();
}
片段3:
private View view;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_chat, container, false);
return view;
}
@Override
public void onDestroyView() {
super.onDestroyView();
view = null;
}
@Override
public Fragment newInstance() {
return new FragmentChat();
}
和导航器:
public MainNavigator(FragmentActivity activity, int containerId) {
super(activity, containerId);
}
@Override
protected Intent createActivityIntent(Context context, String screenKey, Object data) {
return null;
}
@Override
protected Fragment createFragment(String screenKey, Object data) {
switch (screenKey){
case FragmentScreenKeys.FRAGMENT_CERTIFICATION:
return new FragmentCertification().newInstance();
case FragmentScreenKeys.FRAGMENT_PLAN:
return new FragmentPlan().newInstance();
case FragmentScreenKeys.FRAGMENT_CHAT:
return new FragmentChat().newInstance();
default:
throw new RuntimeException("Unknown screen key");
}
}
删除了方法 o
nPause()和onDestroyView()在第一个片段中,我使用了
cicerone,尽管在那里不需要它,它在方法中连接了我的导航器并在方法中onResume()取消订阅onPause()- 删除它们,在其他片段中也是如此。解决了这个问题。但是该项目还有三个使用它的片段,它的导航器和
cicerone方法都存在,但它们没有问题。onResume()onPause()