Y
Y
YungSherman2018-07-02 12:52:04
Android
YungSherman, 2018-07-02 12:52:04

Why doesn't the fragment save its state?

I'm trying to create an application with MVP (Moxy) architecture. The fact is that the fragment does not save its state after the device is rotated. Here is the onCreate method of the activity in which the fragment is launched:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_comments);
    String link = getIntent().getStringExtra(EXTRA_LINK_COMMENTS);
    FragmentManager fragmentManager = getSupportFragmentManager();
    CommentFragment fragment = CommentFragment.newInstance(link);
    fragmentManager.beginTransaction().add(R.id.comments_fragment_host, fragment).commitNowAllowingStateLoss();
}

Presenter code for fragment:
@InjectViewState
public class CommentsPresenter extends MvpPresenter<CommentsView> {

private CommentsModel mCommentsModel;

public CommentsPresenter(){
    mCommentsModel = new CommentsModel();
}

@Override
protected void onFirstViewAttach() {
    super.onFirstViewAttach();
    showLoading(true);
    loadComments();
}

public void loadComments(){
    mCommentsModel.loadComments(new CommentsModel.LoadCommentsCallback() {
        @Override
        public void onLoad(List<Comment> comments) {
            getViewState().showComments(comments);
        }
    });
}

public void showLoading(boolean state){
    getViewState().setUpdating(state);
}

public void setLink(String link){
    mCommentsModel.setLink(link);
 }
}

Here is the snippet code:
public class CommentFragment extends MvpAppCompatFragment implements CommentsView{

/*биндим view*/

private CommentsAdapter mCommentsAdapter;
private LinearLayoutManager mLinearLayout;


private static final String ARG_COMMENTS_LINK = "comments_link";
private String mLink;

public static CommentFragment newInstance(String link){
    Bundle bundle = new Bundle();
    bundle.putSerializable(ARG_COMMENTS_LINK, link);
    CommentFragment commentFragment = new CommentFragment();
    commentFragment.setArguments(bundle);
    return commentFragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if(getArguments() != null){
        mLink = (String)getArguments().getSerializable(ARG_COMMENTS_LINK);
        setHasOptionsMenu(true);
        setRetainInstance(true);
    }
}

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.comments_fragment_layout, container, false);
    ButterKnife.bind(this, v);
    AppCompatActivity actionBar = (AppCompatActivity) getActivity();
    actionBar.setSupportActionBar(mToolbar);
    actionBar.getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    actionBar.getSupportActionBar().setDisplayShowHomeEnabled(true);
    actionBar.getSupportActionBar().setTitle(R.string.comments_title);
    mCommentsAdapter = new CommentsAdapter();
    mLinearLayout = new LinearLayoutManager(getContext());
    mRecyclerView.setLayoutManager(mLinearLayout);
    mRecyclerView.setAdapter(mCommentsAdapter);
    mCommentsPresenter.setLink(mLink);
    return v;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case android.R.id.home:
            getActivity().finish();
            return false;
    }
    return super.onOptionsItemSelected(item);
}

@Override
public void setUpdating(boolean state) {
    mSwipeRefreshLayout.setRefreshing(state);
}

@Override
public void showComments(List<Comment> comments) {
    mCommentsAdapter.setComments(comments);
    mCommentsAdapter.notifyDataSetChanged();
    setUpdating(false);
 }
}

I have another fragment which is designed in a similar way, but which doesn't get any value, it works fine and saves its state.
Where might the error be hiding? PS In the onCreate method I tried to run a fragment which works fine - it doesn't save either. It seems to me that the problem is somewhere at the onCreate level of the activity.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Eugene, 2018-07-02
@YungSherman

It seems to me that the problem is somewhere at the onCreate level of the activity.

Of course, with every onCreate a new fragment is created,
try this:
if (savedInstanceState == null) {
addFragment(R.id.frame, new Fragment());
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question