0

I have an Activity which retrieves data from Firebase and a post is created. But it has to get reloaded if it is removed from recent app or stopped. I want to store this permanently in the app. How can I do that?

My Activity:

public class MainActivity extends AppCompatActivity {


    private   RecyclerView mbloglist;
    private DatabaseReference mdatabase;

    public static final String TAG = "Homeactivity";
    private Context mcontext = MainActivity.this;
    private static final int ACTIVITY_NUM = 0;

    Dialog mycustomDialog;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setTheme(R.style.AppTheme);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Log.d(TAG, "onCreate: starting");


        mdatabase = FirebaseDatabase.getInstance().getReference().child("Global");
        mdatabase.keepSynced(true);

        mbloglist = (RecyclerView)findViewById(R.id.recycleView_post);
        mbloglist.setHasFixedSize(true);
        mbloglist.setLayoutManager(new LinearLayoutManager(this));

        setupBottomNavigationView();
        setupToolbar();
        mycustomDialog = new Dialog(this);



    }

    @Override
    protected void onStart() {
        super.onStart();
        final ProgressBar progressBar = (ProgressBar)findViewById(R.id.progressBar_post);

        progressBar.setVisibility(View.VISIBLE);
        FirebaseRecyclerAdapter<Blog,BlogViewHolder>firebaseRecyclerAdapter=new FirebaseRecyclerAdapter<Blog, BlogViewHolder>(
                Blog.class,R.layout.layout_post,BlogViewHolder.class,mdatabase
        ) {
            @Override
            protected void populateViewHolder(BlogViewHolder viewHolder, Blog model, int position) {
                viewHolder.setTitle(model.getTitle());
                viewHolder.setDesc(model.getDesc());
                viewHolder.setImage(getApplicationContext(),model.getImage());
                viewHolder.setTime(model.getTime());
            }



        };
        mdatabase.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                progressBar.setVisibility(View.GONE);
            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {
                Log.d(TAG, "onCancelled: event cancelled");
            }
        });
        mbloglist.setAdapter(firebaseRecyclerAdapter);

    }

    public static class BlogViewHolder extends RecyclerView.ViewHolder {
        View mview;

        public BlogViewHolder(View itemView) {
            super(itemView);
            mview = itemView;
        }

        public void setTitle(String title) {
            TextView post_title = (TextView) itemView.findViewById(R.id.post_title);
            post_title.setText(title);
        }

        public void setDesc(String desc) {
            TextView post_desc = (TextView) itemView.findViewById(R.id.post_description);
            post_desc.setText(desc);
        }

        public void setImage(Context context,String image) {
            PhotoView post_image = (PhotoView) itemView.findViewById(R.id.post_image);
          Glide.with(context).asBitmap().load(image).into(post_image);
        }
        public void setTime(String time) {
            TextView post_desc = (TextView) itemView.findViewById(R.id.post_time);
            post_desc.setText(time);
        }
    }

can I have this post be saved and shown even if their is no internet connection. and reload the post when connection is made available.

CEO tech4lifeapps
  • 885
  • 1
  • 12
  • 31
  • Have you tried Firestore? Support for offline persistence/saving of data when offline has been implemented in Firestore by Firebase. – Edric Aug 04 '18 at 15:03
  • i have no idea about firestore. – sagar shetty Aug 04 '18 at 15:07
  • **[This](https://stackoverflow.com/questions/49383687/how-can-i-retrieve-data-from-firebase-to-my-adapter/49384849)** is a recommended way in which you can retrieve data from a Firebase Realtime database and display it in a `RecyclerView` using `FirebaseRecyclerAdapter`. – Alex Mamo Aug 05 '18 at 09:38

1 Answers1

0

Also Use

 DatabaseReference db = FirebaseDatabase.getInstance();                                        
 db.setPersistenceEnabled(true);
 mDatabase = db.getReference().child("Global");;

Acc. to docs -

By enabling persistence, any data that the Firebase Realtime Database client would sync while online persists to disk and is available offline, even when the user or operating system restarts the app. This means your app works as it would online by using the local data stored in the cache.

along with

mdatabase.keepSynced(true);
Raj
  • 2,997
  • 2
  • 12
  • 30
  • it crashes when I relauch the same activity. java.lang.RuntimeException: Unable to start activity ComponentInfo{sagarf.com.mce/sagarf.com.mce.Home.MainActivity}: com.google.firebase.database.DatabaseException: Calls to setPersistenceEnabled() must be made before any other usage of FirebaseDatabase instance. – sagar shetty Aug 04 '18 at 15:47
  • Yes I have added it before. I have an bottom navigation view with each menu item launching an activity. So it crashes if it is relaunch for a second time. – sagar shetty Aug 04 '18 at 16:07
  • I works the first time. But when I relaunch the activity it crashes. – sagar shetty Aug 04 '18 at 16:26
  • java.lang.RuntimeException: Unable to start activity ComponentInfo{sagarf.com.mce/sagarf.com.mce.Home.MainActivity}: com.google.firebase.database.DatabaseException: Calls to setPersistenceEnabled() must be made before any other usage of FirebaseDatabase instance. – sagar shetty Aug 04 '18 at 16:30
  • So do the above part in onStart() method. – Raj Aug 04 '18 at 16:33
  • No, it couldn't use it in I start – sagar shetty Aug 06 '18 at 03:39
  • But I got it to work , by make the instruction to be executed only once (when installed). Thank you – sagar shetty Aug 06 '18 at 03:40