Answer the question
In order to leave comments, you need to log in
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();
}
@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);
}
}
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);
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question