AlexMel
AlexMel

Reputation: 21

Change TextView Between Activities

So i got a project with the following activities : MainActivity/GetJson/ TimerActivity.

GetJson activity :

public class GetJson extends AppCompatActivity     {
    String JSON_STRING;
    String json;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }



    public void getJSON(View view){
        new BackgroundTask().execute();
    }


    public class BackgroundTask  extends AsyncTask<Void,Void,String>  {
        String json_url;



        @Override
        protected void onPreExecute() {
            json_url="http://10.10.103.36/projet/php/fichier.php";

        }

        @Override
        protected String doInBackground(Void... params) {
            try {
                URL url=new URL(json_url);
                HttpURLConnection httpURLConnection=(HttpURLConnection)url.openConnection();
                InputStream inputStream=httpURLConnection.getInputStream();
                BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(inputStream));
                StringBuilder stringBuilder=new StringBuilder();
                while  ((JSON_STRING= bufferedReader.readLine())!=null){
                    stringBuilder.append(JSON_STRING+"\n");
                }

                bufferedReader.close();
                inputStream.close();;
                httpURLConnection.disconnect();
                return stringBuilder.toString().trim();

            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onProgressUpdate(Void... values) {
            super.onProgressUpdate(values);
        }

        @Override
        protected void onPostExecute(String result) {

            json=result;

        }



    }
}

Timer Activity

public class TimerActivity extends Activity {

    private TextView test;
    String msg = "Hey";

    private Handler mHandler = new Handler();





    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        test = (TextView) findViewById(R.id.compteur);
        Timer timer = new Timer();
        TimerTask tt = new TimerTask()
        {
            @Override
            public void run()
            {
                test.setText(msg); 
            }
        };

        timer.scheduleAtFixedRate(tt,5000,1000);  // Delay 5 seconds on the first run
        // then run every second
        test.setText(msg);


        setContentView(R.layout.activity_main);
    }


}

In my xml main activity i got 2 textview : - compteur : to display a text from my timeractivity - textViewJson : to display my json

I think my methods to get json( from GetJson) and display text(from TimerActivity) are correct. But the problem is that i can't setText from others activities to my main activity. I don't have any compilation problem bu my textView aren't getting updated. I tried both in GetJson and TimerActivity to just do :

TextView test;
test = (TextView) findViewById(R.id.compteur);
test.setText(msg);

In order to check if i can change the textview text without even using the returned values and nothing happens.

Any ideas ?

Have a good day !

Upvotes: 1

Views: 180

Answers (1)

T.Dimitrov
T.Dimitrov

Reputation: 157

Once you have the information you want to show in your TVs you should save it somewhere and load it when your Activity is created. You can't change the state of Views in a destroyed Activity. Use Intents (putExtra();) to pass data between your Activies or use SharedPreferences

Upvotes: 1

Related Questions