RecyclerView in a fragment Empty after coming back from an activity












1














I have a recycler view in a HomeFragment (of MainActivity), populated with a FirestoreRecyclerAdapter and a product card view, which has an onClick event that opens a ProductActivity.



The recyclerview displays the items fine when first opened. but if one clicks on one of the items, which opens the Product Activity and then press the back button to come back to Homefragment, the recycler view is empty



here is my Fragment:



public class ExploreFragment extends Fragment {
public static final String TAG = "ExploreFragment";
Context context;

View rootView;
@BindView(R.id.toolbar)
Toolbar toolbar;
@BindView(R.id.rv_categories)
RecyclerView rv_categories;
@BindView(R.id.btn_more_categories)
MaterialButton btn_more_categories;

RecyclerView.Adapter mCategoryAdapter;
LinearLayoutManager mCategoryLayoutManager;

@BindView(R.id.rv_ads_nearby)
RecyclerView rv_ads_nearby;

FirestoreRecyclerAdapter mAdsAdapter;
StaggeredGridLayoutManager mAdsLayoutManager;








public ExploreFragment() {
// Required empty public constructor
}



@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
rootView = inflater.inflate(R.layout.fragment_explore, container, false);
ButterKnife.bind(this,rootView);
context = this.getContext();

((MainActivity) getActivity()).setToolbar(toolbar,"The Marketplace",false);


setupCategories();
setupAds();

return rootView;
}



private void setupCategories(){

mCategoryLayoutManager = new LinearLayoutManager(context,RecyclerView.HORIZONTAL,false);

mCategoryAdapter = new CategoryAdapter(Express.categories(),context);
rv_categories.setLayoutManager(mCategoryLayoutManager);
rv_categories.setAdapter(mCategoryAdapter);

Log.d("CATE","Categories: "+mCategoryAdapter.getItemCount());
Log.d("CATE","Categories Express: "+Express.categories().size());

}

private void setupAds(){
mAdsLayoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
rv_ads_nearby.setLayoutManager(mAdsLayoutManager);

// Set up FirebaseRecyclerAdapter with the Query
Query adsQuery = Express.firestore.collection(FS_ADS).orderBy("postedAt",DESCENDING);

// Firestore options
FirestoreRecyclerOptions options = new FirestoreRecyclerOptions.Builder<Ad>()
.setQuery(adsQuery, Ad.class)
.build();

mAdsAdapter = new FirestoreRecyclerAdapter<Ad, AdViewHolder>(options) {

@NonNull
@Override
public AdViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.ad_card, parent, false);
return new AdViewHolder(view);
}

@Override
protected void onBindViewHolder(@NonNull AdViewHolder adViewHolder, int i, @NonNull Ad ad) {
adViewHolder.bindToAd(ad, getActivity(), new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getActivity(), AdActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);

intent.putExtra("AD",ad);
context.startActivity(intent);
}
});
}

@Override
public int getItemCount() {
return super.getItemCount();
}

@Override
public void onError(FirebaseFirestoreException e) {
Log.e(TAG, e.getMessage());
}
};

rv_ads_nearby.setAdapter(mAdsAdapter);
mAdsAdapter.notifyDataSetChanged();
}

@Override
public void onStart() {
super.onStart();
if (mAdsAdapter != null) {
mAdsAdapter.startListening();
}
}

@Override
public void onStop() {
super.onStop();
if (mAdsAdapter != null) {
mAdsAdapter.stopListening();
mAdsAdapter = null;
}
}


}









share|improve this question




















  • 1




    you need to share more codes. and let me see what you have in onResume()
    – user987760
    Nov 19 '18 at 13:11












  • I dont have anything in onResume, what should i put there? will share more code ...
    – Tnaffh
    Nov 19 '18 at 13:14










  • @Tnaffh Update the data for the FirestoreRecyclerAdapter
    – Vall0n
    Nov 19 '18 at 13:15












  • @Vall0n how can do that? give me an example please
    – Tnaffh
    Nov 19 '18 at 13:22










  • Please post your code of the HomeFragment so that we can to find the right solution
    – Vall0n
    Nov 19 '18 at 13:23


















1














I have a recycler view in a HomeFragment (of MainActivity), populated with a FirestoreRecyclerAdapter and a product card view, which has an onClick event that opens a ProductActivity.



The recyclerview displays the items fine when first opened. but if one clicks on one of the items, which opens the Product Activity and then press the back button to come back to Homefragment, the recycler view is empty



here is my Fragment:



public class ExploreFragment extends Fragment {
public static final String TAG = "ExploreFragment";
Context context;

View rootView;
@BindView(R.id.toolbar)
Toolbar toolbar;
@BindView(R.id.rv_categories)
RecyclerView rv_categories;
@BindView(R.id.btn_more_categories)
MaterialButton btn_more_categories;

RecyclerView.Adapter mCategoryAdapter;
LinearLayoutManager mCategoryLayoutManager;

@BindView(R.id.rv_ads_nearby)
RecyclerView rv_ads_nearby;

FirestoreRecyclerAdapter mAdsAdapter;
StaggeredGridLayoutManager mAdsLayoutManager;








public ExploreFragment() {
// Required empty public constructor
}



@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
rootView = inflater.inflate(R.layout.fragment_explore, container, false);
ButterKnife.bind(this,rootView);
context = this.getContext();

((MainActivity) getActivity()).setToolbar(toolbar,"The Marketplace",false);


setupCategories();
setupAds();

return rootView;
}



private void setupCategories(){

mCategoryLayoutManager = new LinearLayoutManager(context,RecyclerView.HORIZONTAL,false);

mCategoryAdapter = new CategoryAdapter(Express.categories(),context);
rv_categories.setLayoutManager(mCategoryLayoutManager);
rv_categories.setAdapter(mCategoryAdapter);

Log.d("CATE","Categories: "+mCategoryAdapter.getItemCount());
Log.d("CATE","Categories Express: "+Express.categories().size());

}

private void setupAds(){
mAdsLayoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
rv_ads_nearby.setLayoutManager(mAdsLayoutManager);

// Set up FirebaseRecyclerAdapter with the Query
Query adsQuery = Express.firestore.collection(FS_ADS).orderBy("postedAt",DESCENDING);

// Firestore options
FirestoreRecyclerOptions options = new FirestoreRecyclerOptions.Builder<Ad>()
.setQuery(adsQuery, Ad.class)
.build();

mAdsAdapter = new FirestoreRecyclerAdapter<Ad, AdViewHolder>(options) {

@NonNull
@Override
public AdViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.ad_card, parent, false);
return new AdViewHolder(view);
}

@Override
protected void onBindViewHolder(@NonNull AdViewHolder adViewHolder, int i, @NonNull Ad ad) {
adViewHolder.bindToAd(ad, getActivity(), new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getActivity(), AdActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);

intent.putExtra("AD",ad);
context.startActivity(intent);
}
});
}

@Override
public int getItemCount() {
return super.getItemCount();
}

@Override
public void onError(FirebaseFirestoreException e) {
Log.e(TAG, e.getMessage());
}
};

rv_ads_nearby.setAdapter(mAdsAdapter);
mAdsAdapter.notifyDataSetChanged();
}

@Override
public void onStart() {
super.onStart();
if (mAdsAdapter != null) {
mAdsAdapter.startListening();
}
}

@Override
public void onStop() {
super.onStop();
if (mAdsAdapter != null) {
mAdsAdapter.stopListening();
mAdsAdapter = null;
}
}


}









share|improve this question




















  • 1




    you need to share more codes. and let me see what you have in onResume()
    – user987760
    Nov 19 '18 at 13:11












  • I dont have anything in onResume, what should i put there? will share more code ...
    – Tnaffh
    Nov 19 '18 at 13:14










  • @Tnaffh Update the data for the FirestoreRecyclerAdapter
    – Vall0n
    Nov 19 '18 at 13:15












  • @Vall0n how can do that? give me an example please
    – Tnaffh
    Nov 19 '18 at 13:22










  • Please post your code of the HomeFragment so that we can to find the right solution
    – Vall0n
    Nov 19 '18 at 13:23
















1












1








1







I have a recycler view in a HomeFragment (of MainActivity), populated with a FirestoreRecyclerAdapter and a product card view, which has an onClick event that opens a ProductActivity.



The recyclerview displays the items fine when first opened. but if one clicks on one of the items, which opens the Product Activity and then press the back button to come back to Homefragment, the recycler view is empty



here is my Fragment:



public class ExploreFragment extends Fragment {
public static final String TAG = "ExploreFragment";
Context context;

View rootView;
@BindView(R.id.toolbar)
Toolbar toolbar;
@BindView(R.id.rv_categories)
RecyclerView rv_categories;
@BindView(R.id.btn_more_categories)
MaterialButton btn_more_categories;

RecyclerView.Adapter mCategoryAdapter;
LinearLayoutManager mCategoryLayoutManager;

@BindView(R.id.rv_ads_nearby)
RecyclerView rv_ads_nearby;

FirestoreRecyclerAdapter mAdsAdapter;
StaggeredGridLayoutManager mAdsLayoutManager;








public ExploreFragment() {
// Required empty public constructor
}



@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
rootView = inflater.inflate(R.layout.fragment_explore, container, false);
ButterKnife.bind(this,rootView);
context = this.getContext();

((MainActivity) getActivity()).setToolbar(toolbar,"The Marketplace",false);


setupCategories();
setupAds();

return rootView;
}



private void setupCategories(){

mCategoryLayoutManager = new LinearLayoutManager(context,RecyclerView.HORIZONTAL,false);

mCategoryAdapter = new CategoryAdapter(Express.categories(),context);
rv_categories.setLayoutManager(mCategoryLayoutManager);
rv_categories.setAdapter(mCategoryAdapter);

Log.d("CATE","Categories: "+mCategoryAdapter.getItemCount());
Log.d("CATE","Categories Express: "+Express.categories().size());

}

private void setupAds(){
mAdsLayoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
rv_ads_nearby.setLayoutManager(mAdsLayoutManager);

// Set up FirebaseRecyclerAdapter with the Query
Query adsQuery = Express.firestore.collection(FS_ADS).orderBy("postedAt",DESCENDING);

// Firestore options
FirestoreRecyclerOptions options = new FirestoreRecyclerOptions.Builder<Ad>()
.setQuery(adsQuery, Ad.class)
.build();

mAdsAdapter = new FirestoreRecyclerAdapter<Ad, AdViewHolder>(options) {

@NonNull
@Override
public AdViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.ad_card, parent, false);
return new AdViewHolder(view);
}

@Override
protected void onBindViewHolder(@NonNull AdViewHolder adViewHolder, int i, @NonNull Ad ad) {
adViewHolder.bindToAd(ad, getActivity(), new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getActivity(), AdActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);

intent.putExtra("AD",ad);
context.startActivity(intent);
}
});
}

@Override
public int getItemCount() {
return super.getItemCount();
}

@Override
public void onError(FirebaseFirestoreException e) {
Log.e(TAG, e.getMessage());
}
};

rv_ads_nearby.setAdapter(mAdsAdapter);
mAdsAdapter.notifyDataSetChanged();
}

@Override
public void onStart() {
super.onStart();
if (mAdsAdapter != null) {
mAdsAdapter.startListening();
}
}

@Override
public void onStop() {
super.onStop();
if (mAdsAdapter != null) {
mAdsAdapter.stopListening();
mAdsAdapter = null;
}
}


}









share|improve this question















I have a recycler view in a HomeFragment (of MainActivity), populated with a FirestoreRecyclerAdapter and a product card view, which has an onClick event that opens a ProductActivity.



The recyclerview displays the items fine when first opened. but if one clicks on one of the items, which opens the Product Activity and then press the back button to come back to Homefragment, the recycler view is empty



here is my Fragment:



public class ExploreFragment extends Fragment {
public static final String TAG = "ExploreFragment";
Context context;

View rootView;
@BindView(R.id.toolbar)
Toolbar toolbar;
@BindView(R.id.rv_categories)
RecyclerView rv_categories;
@BindView(R.id.btn_more_categories)
MaterialButton btn_more_categories;

RecyclerView.Adapter mCategoryAdapter;
LinearLayoutManager mCategoryLayoutManager;

@BindView(R.id.rv_ads_nearby)
RecyclerView rv_ads_nearby;

FirestoreRecyclerAdapter mAdsAdapter;
StaggeredGridLayoutManager mAdsLayoutManager;








public ExploreFragment() {
// Required empty public constructor
}



@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
rootView = inflater.inflate(R.layout.fragment_explore, container, false);
ButterKnife.bind(this,rootView);
context = this.getContext();

((MainActivity) getActivity()).setToolbar(toolbar,"The Marketplace",false);


setupCategories();
setupAds();

return rootView;
}



private void setupCategories(){

mCategoryLayoutManager = new LinearLayoutManager(context,RecyclerView.HORIZONTAL,false);

mCategoryAdapter = new CategoryAdapter(Express.categories(),context);
rv_categories.setLayoutManager(mCategoryLayoutManager);
rv_categories.setAdapter(mCategoryAdapter);

Log.d("CATE","Categories: "+mCategoryAdapter.getItemCount());
Log.d("CATE","Categories Express: "+Express.categories().size());

}

private void setupAds(){
mAdsLayoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
rv_ads_nearby.setLayoutManager(mAdsLayoutManager);

// Set up FirebaseRecyclerAdapter with the Query
Query adsQuery = Express.firestore.collection(FS_ADS).orderBy("postedAt",DESCENDING);

// Firestore options
FirestoreRecyclerOptions options = new FirestoreRecyclerOptions.Builder<Ad>()
.setQuery(adsQuery, Ad.class)
.build();

mAdsAdapter = new FirestoreRecyclerAdapter<Ad, AdViewHolder>(options) {

@NonNull
@Override
public AdViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.ad_card, parent, false);
return new AdViewHolder(view);
}

@Override
protected void onBindViewHolder(@NonNull AdViewHolder adViewHolder, int i, @NonNull Ad ad) {
adViewHolder.bindToAd(ad, getActivity(), new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getActivity(), AdActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);

intent.putExtra("AD",ad);
context.startActivity(intent);
}
});
}

@Override
public int getItemCount() {
return super.getItemCount();
}

@Override
public void onError(FirebaseFirestoreException e) {
Log.e(TAG, e.getMessage());
}
};

rv_ads_nearby.setAdapter(mAdsAdapter);
mAdsAdapter.notifyDataSetChanged();
}

@Override
public void onStart() {
super.onStart();
if (mAdsAdapter != null) {
mAdsAdapter.startListening();
}
}

@Override
public void onStop() {
super.onStop();
if (mAdsAdapter != null) {
mAdsAdapter.stopListening();
mAdsAdapter = null;
}
}


}






java android android-recyclerview






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 19 '18 at 13:19

























asked Nov 19 '18 at 13:10









Tnaffh

286




286








  • 1




    you need to share more codes. and let me see what you have in onResume()
    – user987760
    Nov 19 '18 at 13:11












  • I dont have anything in onResume, what should i put there? will share more code ...
    – Tnaffh
    Nov 19 '18 at 13:14










  • @Tnaffh Update the data for the FirestoreRecyclerAdapter
    – Vall0n
    Nov 19 '18 at 13:15












  • @Vall0n how can do that? give me an example please
    – Tnaffh
    Nov 19 '18 at 13:22










  • Please post your code of the HomeFragment so that we can to find the right solution
    – Vall0n
    Nov 19 '18 at 13:23
















  • 1




    you need to share more codes. and let me see what you have in onResume()
    – user987760
    Nov 19 '18 at 13:11












  • I dont have anything in onResume, what should i put there? will share more code ...
    – Tnaffh
    Nov 19 '18 at 13:14










  • @Tnaffh Update the data for the FirestoreRecyclerAdapter
    – Vall0n
    Nov 19 '18 at 13:15












  • @Vall0n how can do that? give me an example please
    – Tnaffh
    Nov 19 '18 at 13:22










  • Please post your code of the HomeFragment so that we can to find the right solution
    – Vall0n
    Nov 19 '18 at 13:23










1




1




you need to share more codes. and let me see what you have in onResume()
– user987760
Nov 19 '18 at 13:11






you need to share more codes. and let me see what you have in onResume()
– user987760
Nov 19 '18 at 13:11














I dont have anything in onResume, what should i put there? will share more code ...
– Tnaffh
Nov 19 '18 at 13:14




I dont have anything in onResume, what should i put there? will share more code ...
– Tnaffh
Nov 19 '18 at 13:14












@Tnaffh Update the data for the FirestoreRecyclerAdapter
– Vall0n
Nov 19 '18 at 13:15






@Tnaffh Update the data for the FirestoreRecyclerAdapter
– Vall0n
Nov 19 '18 at 13:15














@Vall0n how can do that? give me an example please
– Tnaffh
Nov 19 '18 at 13:22




@Vall0n how can do that? give me an example please
– Tnaffh
Nov 19 '18 at 13:22












Please post your code of the HomeFragment so that we can to find the right solution
– Vall0n
Nov 19 '18 at 13:23






Please post your code of the HomeFragment so that we can to find the right solution
– Vall0n
Nov 19 '18 at 13:23














1 Answer
1






active

oldest

votes


















1














Figured it out, I was setting the adapter to null in the onStop() method of the fragment. so just removed this line mAdsAdapter = null; from onStop()



also added mAdsAdapter.notifyDataSetChaged() and mAdsAdapter.startListening();



Thank you for your help guys!






share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53375375%2frecyclerview-in-a-fragment-empty-after-coming-back-from-an-activity%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    Figured it out, I was setting the adapter to null in the onStop() method of the fragment. so just removed this line mAdsAdapter = null; from onStop()



    also added mAdsAdapter.notifyDataSetChaged() and mAdsAdapter.startListening();



    Thank you for your help guys!






    share|improve this answer


























      1














      Figured it out, I was setting the adapter to null in the onStop() method of the fragment. so just removed this line mAdsAdapter = null; from onStop()



      also added mAdsAdapter.notifyDataSetChaged() and mAdsAdapter.startListening();



      Thank you for your help guys!






      share|improve this answer
























        1












        1








        1






        Figured it out, I was setting the adapter to null in the onStop() method of the fragment. so just removed this line mAdsAdapter = null; from onStop()



        also added mAdsAdapter.notifyDataSetChaged() and mAdsAdapter.startListening();



        Thank you for your help guys!






        share|improve this answer












        Figured it out, I was setting the adapter to null in the onStop() method of the fragment. so just removed this line mAdsAdapter = null; from onStop()



        also added mAdsAdapter.notifyDataSetChaged() and mAdsAdapter.startListening();



        Thank you for your help guys!







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 19 '18 at 13:43









        Tnaffh

        286




        286






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53375375%2frecyclerview-in-a-fragment-empty-after-coming-back-from-an-activity%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            MongoDB - Not Authorized To Execute Command

            in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith

            Npm cannot find a required file even through it is in the searched directory