Home > database >  Update TextView from seperate java file
Update TextView from seperate java file

Time:06-12

I have three files: MainActivity.java, MapsActivity.java and MainActivity.xml. I would like to change this TextView object in the MainActivity.xml:

    <TextView
        android:id="@ id/destination_latlng"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="80dp"
        android:layout_marginBottom="40dp"
        android:textColor="@color/black"
        android:text="Destination Lat, Lng" />

I would like to change it from within the MapsActivity.java file, but whenever I try to do this by assigning a variable to the "destination_latlng" TextView and using setText() the app crashes.

public class Map extends AppCompatActivity implements OnMapReadyCallback {

    // Destination coords
    TextView destination_latLng;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Retrieve the content view that renders the map.
        setContentView(R.layout.activity_map);


        // destination coords
        destination_latLng = (TextView) findViewById(R.id.destination_latlng);
        destination_latLng.setText("testing...");

How do I change the TextView from the MapsActivity.java file?

CodePudding user response:

Try to think of Activities/Fragments as temporary windows to your app's data - you should not try to store persistent data in them and you have no guarantee how long they will continue to exist once they are not shown. In your case, MainActivity could have been destroyed by the OS and when you go back a completely new layout would be drawn. You cannot access views in MainActivity from a different activity, and you shouldn't try to.

If you have some persistent data, e.g. some text about your destination displayed in MainActivity, you should store it in something like a database, a ViewModel, a singleton, or SharedPreferences. Here's how you could do it using SharedPreferences

When you have a new value to set in the map activity, you update the SharedPreferences value.

public class Map extends AppCompatActivity implements OnMapReadyCallback {

    SharedPreferences prefs;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_map);
        prefs = PreferenceManager.getDefaultSharedPreferences(this);

        // Anywhere in here you can edit the saved value in SharedPreferences
        SharedPreferences.Editor ed = prefs.edit();
        ed.putString("DEST", "Going Somewhere");
        ed.apply();
    }
}

Then if you want to show that saved value in the main activity, you can load it from SharedPreferences in onResume

public class MainActivity extends AppCompatActivity  {

    // Destination coords
    TextView destination_latLng;
    SharedPreferences prefs;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        prefs = PreferenceManager.getDefaultSharedPreferences(this);

        // If R.id.destination_latlng is in R.layout.activity_main you can
        // ONLY access it in MainActivity
        destination_latLng = (TextView) findViewById(R.id.destination_latlng);        
    }
    
    @Override
    protected void onResume() {
        super.onResume();
        String dest = prefs.getString("DEST", "");
        destination_latLng.setText(dest);
    }
}
  • Related