0

When I switch between my tabs the date won't show. I have searched for other methods for showing date with the help of textview but most of them didn't work. Only the method of thread worked for me but it has an issue that it doesn't show date in all tabs and the app goes down when we navigate from the navigation drawer.

preview

My code:

    Thread c = new Thread(){
            @Override
            public void run()
            {
                try {
                    while (!isInterrupted())
                    {
                        Thread.sleep(1000);
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                TextView tdate =(TextView)findViewById(R.id.date);
                                long date = System.currentTimeMillis();
                                SimpleDateFormat sdf =new SimpleDateFormat("dd MMM,yyyy");
                                String dateString =sdf.format(date);
                                tdate.setText(dateString);
                            }
                        });
                    }
                }catch (InterruptedException a)
                {

                }
            }
        };
        c.start();

`

shizhen
  • 12,251
  • 9
  • 52
  • 88
divine_rythm
  • 149
  • 3
  • 9
  • Why did you use thread? Any network call used in each tab fragment or any background method to get date values. If you want to retain & restore your fragment data [see this](http://www.codexpedia.com/android/store-and-retrieve-fragment-state-data-in-android/) – Mohamed Mohaideen AH Jan 28 '18 at 08:50
  • Thanks for help I'll try this for sure – divine_rythm Jan 28 '18 at 10:44
  • This is not working despite this was helpful since my app doesn't crash now... Please suggest something – divine_rythm Feb 02 '18 at 06:10

0 Answers0