0

So I am creating an app where I get the string from my AsyncTask which I have created as a subclass in my MainActivity class in order to get the variables I receive from the internet. And according to variables I get I change the images accordingly after every 5 seconds. Now the task is successful but on activity refresh I keep getting the default layout I created in activity_main.xml and again it changes to the one I want. Posting my code below.

Thread thread = new Thread() { //thread I am running every 5 secs
        @Override
        public void run() {
            try {
                synchronized (this) {
                    wait(5000);
                    syncapp sa = new syncapp(); //AsyncTask to get String from Internet
                    sa.execute();
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            Intent mainActivity = new Intent(getApplicationContext(), MainActivity.class); //Creating intent to restart the activity (Need a workaround if possible)
            startActivity(mainActivity);
        }

        ;
    };
    thread.start();



}
public void setImage(String str) //Function I will call to change Image
    {
        vacancy =0;
        b = a.toCharArray();
        for (int i = 0; i < 6; i++)
            if (b[i] == '0'){
                iv[i].setImageResource(R.drawable.img2);
                vacancy++;}
            else if (b[i] == '1') {
                iv[i].setImageResource(R.drawable.img1);
            }
        Log.i("abc ", a);
        tv.setText("InBackGround" + str);
    }
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
   /* for(int i =0;i<6;i++) {
        outState.putChar("imv"+(i+1), b[i]);
    }*/
    outState.putString("a",a); //Getting a from the internet (php file)
    Log.i("Saved State", "Activity A - Saving instance state");
}
  1. Now what I want is if you have a better method to do this thing. Eg. In a Stock market application the prices keep on changing. The same way I want my image to change according to the data I get.
  2. If this is the only method then how do I save changes I make (eg. like in the code above setImageResource to img2) permanently.
  3. If I can use something else ImageView.

I have already used onSaveInstanceState But as I am taking values from the internet I don't know I am not able to use them.

Gueorgui Obregon
  • 5,077
  • 3
  • 33
  • 57

1 Answers1

1

So first of all.. when working with UI elements such as Views, Widgets, you would want to avoid spawning your own threads, as View can be touched only from the thread it was created from.

Second.. you would also want to avoid sleeping inside your worker thread - basically just use the Handler class (from android.os) and post a delayed Runnable, like so: https://stackoverflow.com/a/20784353/2102748. Just be sure to stop all work on that particular handler when your Activity stops, like so: https://stackoverflow.com/a/3627399/2102748

Third - you should perhaps load the photo immediately (on the same AsyncTask or thread) if that is the only thing you need, but be sure to post the "set bitmap to view" work to the handler you created.

Hope this helps.

Community
  • 1
  • 1
milosmns
  • 3,595
  • 4
  • 36
  • 48