I have an app that has two tabs with fragments. One tab is called map and the other restaurantList. When I click on map marker or a card in the list it opens a restaurantDetailsActivity that has info about restaurant - lat
, lang
, name, rating, etc. There I have a floating action button that the user should click and it should close the current activity and go to maps tab fragment, to the location that I passed from the activity. I have tried a lot of stuff without any success: 1 2 3 4 5 6 7 8 9 ...
This is what I would want - when the user clicks the FAB, it should pass lat
and lon
from the restaurantDetailsActivity, to my map fragment and zoom in into that location (based on the lat and lon), regardless whether it was opened from the list fragment or the map fragment.
My restaurantDetailsActivity:
final String lat = restaurant.getLat();
final String lon = restaurant.getLon();
FloatingActionButton fabGoToMap = findViewById(R.id.fabGoToMap);
fabGoToMap.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// MapsFragment fragment = new MapsFragment();
// Bundle bundle = new Bundle();
// bundle.putString("lat", lat);
// bundle.putString("lon", lon);
// MapsFragment mapsFragment = new MapsFragment();
// mapsFragment.setArguments(bundle);
// Intent restaurantDescriptionIntent = new Intent(this, MapsFragment.class);
Bundle bundle = new Bundle();
bundle.putString("lat", lat);
bundle.putString("lon", lon);
MapsFragment fragInfo = new MapsFragment();
fragInfo.setArguments(bundle);
android.support.v4.app.FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
// transaction.add(R.id.map, mapsFragment, "tag").commit();
finish();
}
});
UPDATED CODE: Calling the activity from list fragment:
public void fetchRestaurant(String restaurantId) {
ApiInterface apiService =
ApiClient.getClient().create(ApiInterface.class);
Call<RestaurantResponse> call = apiService.getRestaurantById(restaurantId);
call.enqueue(new Callback<RestaurantResponse>() {
@Override
public void onResponse(Call<RestaurantResponse> call, retrofit2.Response<RestaurantResponse> response) {
final Restaurant restaurant= response.body().getResults();
Intent intent = new Intent(getActivity().getApplicationContext(), AvailableRestaurantActivity.class);
intent.putExtra("estaurant", estaurant);
startActivity(intent);
}
@Override
public void onFailure(Call<RestaurantResponse> call, Throwable t) {
// Log error here since request failed
Log.e(TAG, t.toString());
Toast.makeText(getActivity().getApplicationContext(), R.string.failed_connectivity, Toast.LENGTH_LONG).show();
}
});
}
My main activity:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
tutorialUsed = false;
tutorialPage = 1;
db = new SQLiteHandler(getApplicationContext());
// session manager
session = new SessionManager(getApplicationContext());
if (!session.isLoggedIn()) {
Intent intent = new Intent(this, LoginActivity.class);
startActivity(intent);
} else {
List<restaurant> restaurants = new ArrayList<>();
TabLayout tabLayout = findViewById(R.id.sliding_tabs);
tabLayout.addTab(tabLayout.newTab().setText(getString(R.string.tab_one)));
tabLayout.addTab(tabLayout.newTab().setText(getString(R.string.tab_two)));
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
tabLayout.setTabMode(TabLayout.MODE_FIXED);
final ViewPager viewPager = findViewById(R.id.viewpager);
PagerAdapter adapter = new PagerAdapter
(getSupportFragmentManager(), tabLayout.getTabCount());
viewPager.setAdapter(adapter);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
viewPager.setOffscreenPageLimit(3);
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
});
....
myUserName.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent accountIntent = new Intent(MainActivity.this, MyProfileActivity.class);
startActivity(accountIntent);
}
});
}
}