FirebaseRecyclerAdapter导致内存泄漏

时间:2017-10-30 12:15:45

标签: android firebase memory-leaks firebaseui

FirebaseUI 3.1.0中的firebaserecycleradapter导致我的活动中出现内存泄漏。 以下是我在onCreateOptionsMenu中的适配器(可能是原因)

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.search, menu);


    search = menu.findItem(R.id.action_search);
    searchView = (SearchView) search.getActionView();

    SearchManager searchManager = (SearchManager) getSystemService(SEARCH_SERVICE);
    searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
    searchView.setIconified(false);
    searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String query) {
            mProgress.show();


            final Query searchQuery = searchDatabaseReference.orderByChild("BusinessName").startAt(query.toUpperCase()).endAt(query.toUpperCase()+"\uf8ff");
            searchQuery.addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    if (dataSnapshot.exists()){
                        noResult.setVisibility(View.GONE);
                        searchRecyclerView.setVisibility(View.VISIBLE);
                        mProgress.dismiss();
                        LinearLayout searchbgLL = (LinearLayout) findViewById(R.id.searchbg_LL);

                        searchbgLL.setVisibility(View.GONE);
                        FirebaseRecyclerOptions<SearchGetter> options =
                                new FirebaseRecyclerOptions.Builder<SearchGetter>()
                                        .setQuery(searchQuery, SearchGetter.class)
                                        .build();

                        searchRecyclerAdapter = new FirebaseRecyclerAdapter<SearchGetter, SearchActivity.CardViewHolder>(
                                options

                        ) {
                            @Override
                            public CardViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
                                View view = LayoutInflater.from(parent.getContext())
                                        .inflate(R.layout.search_card_layout, parent, false);

                                return new CardViewHolder(view);
                            }

                            @Override
                            protected void onBindViewHolder(CardViewHolder holder, int position, SearchGetter model) {
                                final String place_key = getRef(position).getKey();

                                holder.setBusinessName(model.getBusinessName());
                                holder.setBusinessAddress(model.getBusinessAddress());

                                holder.mView.setOnClickListener(new View.OnClickListener() {
                                    @Override
                                    public void onClick(View v) {
                                        Intent place_viewIntent = new Intent(SearchActivity.this, PlaceActivity.class);
                                        place_viewIntent.putExtra("place_id", place_key);
                                        startActivity(place_viewIntent);

                                    }
                                });
                            }


                        };
                        searchRecyclerView.setAdapter(searchRecyclerAdapter);
                        searchRecyclerAdapter.notifyDataSetChanged();
                        searchRecyclerAdapter.startListening();


                    }else{
                        mProgress.dismiss();
                        searchRecyclerView.setVisibility(View.GONE);
                        noResult.setVisibility(View.VISIBLE);

                    }


                }

                @Override
                public void onCancelled(DatabaseError databaseError) {

                }
            });




            return false;
        }

        @Override
        public boolean onQueryTextChange(String newText) {

            return true;
        }

    });
    return super.onCreateOptionsMenu(menu);
}

我在onStop()上调用 stopListening()

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

那我该如何:

  • 将我的适配器移动到onStart或onCreate?
  • 正确启动和停止收听适配器?

0 个答案:

没有答案