I have hit a point in my app where I find myself sharing data between Activities in a clumsy way - so I have decided to try switching most of the app to a single Activity (in order to use a single ViewModel across screens), using the Navigation components to swap Fragments instead of changing Activities.
It seems that for each destination I need an xml layout file and a fragment class which looks like this:
class DestinationFragment: Fragment() {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
// Inflate the layout for this fragment
val view = inflater.inflate(R.layout.fragment_destination_layout, container, false)
return view
}
}
(I'm actually using Java but could only find Kotlin examples.) source
However I'm not sure what to do with a Maps screen. I've currently got it as its own Activity:
activity_maps.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MapsActivity">
<!-- Toolbar stuff -->
<FrameLayout android:id="@+id/container_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/my_toolbar"
xmlns:android="http://schemas.android.com/apk/res/android">
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MapsActivity" />
</FrameLayout>
</RelativeLayout>
MapsActivity.java
public class MapsActivity extends AppCompatActivity implements OnMapReadyCallback, GoogleMap.OnInfoWindowClickListener {
private GoogleMap mMap;
//Create the activity
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
//Check if user has location permissions granted
if (!locationPermissionCheck()) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION},1);
} else {
setupMap(); //custom function defined elsewhere
}
}
So my question - if currently I have an Activity which fetches a Map Fragment, to fit into the Navigation architecture do I now need a Fragment which fetches a Map Fragment? i.e. something like...
public class MapDestinationFragment extends Fragment implements OnMapReadyCallback, GoogleMap.OnInfoWindowClickListener {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.article_view, container, false);
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
//Check if user has location permissions granted
if (!locationPermissionCheck()) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION},1);
} else {
setupMap(); //custom function defined elsewhere
}
}
If this is the solution, is it ok to have a Fragment within a Fragment? Or would I be better off keeping it a separate Activity and just accepting the data sharing issue?
Thanks!