I am trying to pass an auto updating text which is latitude and longitude information from fragment_map to fragment_clocking through MainActivity but the value I get inside fragment_clocking is returned as null with no errors inside Logcat.
I am trying to use bundle to pass the values but I am not sure where and how to declare and call the bundles since I have a bottom navigation view.
MainActivity.java
public class MainActivity extends AppCompatActivity {
TextView latitude;
TextView longitude;
Bundle bundle = new Bundle();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
BottomNavigationView bottomNav = findViewById(R.id.nav_view);
bottomNav.setOnNavigationItemSelectedListener(navListener);
latitude = findViewById(R.id.id_latitude);
longitude = findViewById(R.id.id_longitude);
bundle.putString("latitudeInfo", String.valueOf(latitude));
bundle.putString("longitudeInfo", String.valueOf(longitude));
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new MapFragment()).commit();
}
private BottomNavigationView.OnNavigationItemSelectedListener navListener
= new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
Fragment selectedFragment = null;
switch (item.getItemId()) {
case R.id.navigation_map:
selectedFragment = new MapFragment();
selectedFragment.setArguments(bundle);
break;
case R.id.navigation_clocking:
selectedFragment = new ClockingFragment();
selectedFragment.setArguments(bundle);
break;
}
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.fragment_container, selectedFragment)
.commit();
return true;
}
};
}
MapFragment.java
public class MapFragment extends Fragment implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, LocationListener, OnMapReadyCallback {
TextView latitude;
TextView longitude;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_map, null);
latitude = rootView.findViewById(R.id.id_latitude);
longitude = rootView.findViewById(R.id.id_longitude);
SupportMapFragment mapFragment = (SupportMapFragment) getChildFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
return rootView;
}
@Override
public void onLocationChanged(Location location) {
latitude.setText("Longitude: "+ location.getLongitude());
longitude.setText("Latitude: "+ location.getLatitude());
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
mGoogleApiClient = new GoogleApiClient.Builder(getActivity())
.addApi(LocationServices.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
mGoogleApiClient.connect();
}
}
ClockingFragment.java
public class ClockingFragment extends Fragment implements LocationListener {
TextView latitude, longitude;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_clocking, null);
latitude = rootView.findViewById(R.id.id_latitudeLocation);
longitude = rootView.findViewById(R.id.id_longitudeLocation);
Bundle bundle = getArguments();
if(bundle != null){
String latitudetest = bundle.getString("latitudeInfo");
String longitudetest = bundle.getString("longitudeInfo");
latitude.setText(latitudetest);
longitude.setText(longitudetest);
}
return rootView;
}
activity_main.xml
<FrameLayout
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/nav_view"/>
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/nav_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="?android:attr/windowBackground"
app:menu="@menu/bottom_nav_menu" />
fragment_clocking.xml
<FrameLayout
android:id="@+id/fragment_clocking"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<TextView
android:id="@+id/id_latitudeLocation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginStart="20dp"
android:layout_marginLeft="20dp"
android:layout_marginTop="20dp"
android:text="TEST PLACEHOLDER"
android:textSize="30sp" />
<TextView
android:id="@+id/id_longitudeLocation"
android:layout_below="@+id/id_latitudeLocation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginLeft="20dp"
android:text="TEST PLACEHOLDER"
android:textSize="30sp"/>
fragment_map.xml
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/fragment_map" />
<fragment
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" />
<TextView
android:id="@+id/id_latitude"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginLeft="20dp"
android:textSize="30sp" />
<TextView
android:id="@+id/id_longitude"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:layout_below="@+id/id_latitude"
android:layout_marginStart="20dp"
android:layout_marginLeft="20dp"
android:textSize="30sp" />
Expected result: latitude and longitude value from FragmentMap.java passed through MainActivity.java and displayed in FragmentClocking.java
Actual Result: https://imgur.com/PWMPiaj
Generally speaking, if you have a Fragment
that collects data and you want to pass this data to another Fragment
, you should implement a callback interface in your Activity
. The Fragment
that collects the data should call the method in the Activity
using the callback interface. The Activity
should implement the callback interface. The method in the Activity
should store the data in some member variables when it is called. When the Activity
starts another Fragment
, it should add the previously stored data to the "arguments" Bundle
and then call setArguments()
to pass the data to the new Fragment
.
See Passing data between a fragment and its container activity for several possible ways to implement this.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.