片段中的Firebase数据不是持久的,会重新下载

时间:2017-12-29 21:23:55

标签: android firebase android-fragments firebase-realtime-database android-viewpager

我有一个ViewPager,其中包含3个片段

我在片段1中有一个TextView“Name”和ImageView“Photo”,当我滚动到片段2或片段3时,我回到片段一,数据被“重新下载”,花费更多的移动数据

我需要在更改片段时,数据保持不变,无需再次下载,如Facebook或Tinder

这是我的代码:

片段一:

 @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    firebaseUser = FirebaseAuth.getInstance().getCurrentUser();
    usr = Utils.obtainPref(getContext(), "usuario");
    mRootView =  inflater.inflate(R.layout.fragment_user_client, container, false);
    receiveData();

    return mRootView;
}

方法接收数据:

 private void receiveData(){

    FirebaseUtils.getUsuarioReferencia(usr).child(firebaseUser.getUid()).child("username").addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            if(dataSnapshot.getValue(String.class) != null){
                String name = dataSnapshot.getValue(String.class);
                mUsername.setText(name);
            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

    FirebaseUtils.getUsuarioReferencia(usr).child(firebaseUser.getUid()).child("description").addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            if(dataSnapshot.getValue(String.class) != null){
                String desc = dataSnapshot.getValue(String.class);
                mDescription.setText(desc);
            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

    FirebaseUtils.getUsuarioReferencia(usr).child(firebaseUser.getUid()).child("photoUrl").addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            if(dataSnapshot.getValue(String.class) != null){
                String url = dataSnapshot.getValue(String.class);
                Log.d("fotoo", url);
                StorageReference storageReference = FirebaseStorage.getInstance().getReference(url);
                if(getActivity() != null){
                    storageReference.getDownloadUrl().addOnSuccessListener(getActivity(), new OnSuccessListener<Uri>() {
                        @Override
                        public void onSuccess(Uri uri) {
                            if(getContext() != null){
                                Picasso.with(getContext()).load(uri).into(mFotoDePerfil);
                            }
                        }
                    });
                }

            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });
}

2 个答案:

答案 0 :(得分:0)

覆盖片段setUserVisibilityHint()中的函数 并在return语句之后添加此if(getActivity()==null) receivedata(); 不要在onCreateView()方法中调用接收数据,或者你可以在一个单独的类中使用公共静态计数器变量,并在onCreateView()本身上添加这个...

if (theclass.counter==0) receivedata(); theclass.counter++;

答案 1 :(得分:0)

创建新类并添加清单

public class MyApp extends Application {

@Override
public void onCreate() {
    super.onCreate();
    FirebaseDatabase.getInstance().setPersistenceEnabled(true);
}
}

清单:

<application
    android:name=".app.MyApp"
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">