Reputation: 2443
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);
}
});
}
}
Upvotes: 0
Views: 161
Reputation: 696
Kemo, You have an event that takes place in a fragment and as a result of the event you want the activity to swap one fragment for a second fragment. So your problem is how to communicate from fragment to activity. The fragment's on attach will give you the context for the activity. The activity should have a method to swap fragments in and out and using the context you got in the on attach you can use in the fragments oncreateview and run the activity's method.here Good luck professor
Upvotes: 1
Reputation: 6697
Start restaurantDetailactivity from your MainActivity using startActivityForResult like this
startActivityForResult(intent, SHOW_DETAILS_REQUEST);
Click on FAB should be like this
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) {
Intent intent = new Intent();
Bundle bundle = new Bundle();
bundle.putString("lat", lat);
bundle.putString("lon", lon);
intent.putExtras(bundle);
setResult(Activity.RESULT_OK,intent);
}
});
in your MainActivity handle activity result like below
@Override
protected void onActivityResult(final int requestCode,
final int resultCode,
final Intent data) {
if (resultCode == RESULT_OK) {
switch (requestCode) {
case SHOW_DETAILS_REQUEST: {
//select mapfragment as current, assuming it is at index 0
viewpager.setCurrentItem(0);
//add your zoom logic here in zoomToCenter method
mapfragment.zoomToCenter(data.getStringExtra("lat"),data.getStringExtra("lon"));
}
}
}
super.onActivityResult(requestCode, resultCode, data);
}
Upvotes: 2