user3720749 user3720749 - 1 month ago 15
Android Question

Keeping the Content View of a New Activity

I have a large amount of code, so changing the way it's done is undesirable.
That being said, I have a need to make a new activity be aware of the content on the screen.

It seems that when I create a new activity, within that new activity class I cannot get something that should be on the screen currently using findViewById(..);

I have two activities: MainActivity.Java and MapsActivity.java. Which at this instance are running simultaneous to each other.
I can include the MapsActivity file as it's short. Further explanation is in the comments.

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {

private GoogleMap mMap;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.activity_maps); <--This will change the content view, which is not what I want as I already have the content view set before this activity is called. But, if I allow this to run, then the subsequent code works since it's "aware" of the contents on the screen.

// Basically I need this activity to function as if I were to write
// setContentView(layout); (but not actually write it)

Button backBtn = (Button) findViewById(R.id.backBTN);
backBtn.setBackgroundColor(Color.RED); //This causes a null error because it cannot find backBtn since backBtn is in my previous activity. (But backBtn is on the screen at the point when this activity runs)

SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this); //This is what I'm trying to make work, if I get the above functioning. This will work.
}

@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;

// Add a marker in Sydney and move the camera
LatLng sydney = new LatLng(-34, 151);
mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
}


}

At some point in MainActivity.Java, I change the screens contents to layout --
setContentView(layout). I then create a new intent which starts the MapsActivity.Java.

The goal is to have a SlidingPaneLayout with a Google Map in it. I would like the new activity to run inside the SlidingPaneLayout. I'm close, but since I can't get my SupportMapFragment (or any elements) from the view currently on the screen (since the setContentView was called in my MainActivity), I can't make it work.

Any ideas?

Answer

when I create a new activity, within that new activity class I cannot get something that should be on the screen currently using findViewById(..);

Right... however, since you seem to have extends FragmentActivity, that suggests you are using Fragments, so you don't really need to have a new activity.

You can simply extends SupportMapFragment to define your custom implementation of the Google Maps Fragment. Then, you can either update your XML to use that subclass and findFragmentById to get it, or you can programmatically load that map fragment into the MainActivity into some FrameLayout, for example.
Either way, therefore allowing you to reference data in the MainActivity via the getActivity() method of the Fragment class.

At some point in MainActivity.Java, I change the screens contents to layout -- setContentView(layout).

You really shouldn't do that after the initial setContentView in onCreate. Main reason is that all the views that you did findViewById for at the start would be invalid. The solution -- use Fragments and a FrameLayout to simulate the setContentView method.

//  Basically I need this activity to function as if I were to write
//   setContentView(layout); (but not actually write it)

Sure, you could implement an abstract parent Activity plus an abstract int getLayoutResource() method, and override onCreate there to always call setContentView(getLayoutResource()). Just seems too complicated for what you need, though