When the map fragment opens for the first time it calls onMapReady and it adds a marker in there and zooms towards with animation.
I want if user click a button on the map, the map will restart as if its open like the begining with zoom and whatnot.
I tried using the method below which I call when I click the button, but it only recreate the map but it didn't call onMapReady because it didn't add maker or anything.!
public void reLoadFragment(Fragment fragment) {
Fragment currentFragment = fragment;
if (currentFragment instanceof MapTransportFragment_BusDriver) {
FragmentTransaction fragTransaction = getActivity().getSupportFragmentManager().beginTransaction();
fragTransaction.detach(currentFragment);
fragTransaction.attach(currentFragment);
fragTransaction.commit();
}
}
Edit #1
I call the method using all three ways belowa and it gives me the same error.
/*1*/ reLoadFragment(this);
/*2*/ reLoadFragment(MyMap.this);
/*3*/ reLoadFragment(new MyMap()); // MyMap is the name of the current fragment.
However, If you want just to restart your fragment it means you want to create new fragment. calling detach and attach doesn't destroy fragment.So try below code
public void reLoadFragment(Fragment fragment) {
Fragment currentFragment = fragment;
if (currentFragment instanceof MapTransportFragment_BusDriver) {
FragmentTransaction fragTransaction = getActivity().getSupportFragmentManager().beginTransaction();
fragTransaction.replace(R.id.YourFragmentContainer,currentFragment,"TAG");
fragTransaction.commit();
}
}
Related
Apparently I have a scenario where my launcher activity is splash screen which contains a handler class to show animation on start.
handler.postDelayed(new Runnable() {
#Override
public void run() {
//animation coder inside
}, 5);
After it is launched the app then navigates to next activity i.e. Home Activity. This activity is basically a fragment loader where I have a custom top bar, frame layout (to load fragment in it) and BottomAppBar. Now on main method I'm intialising all the ids and calling two methods i.e. loadFrag() which loads default fragment to display when home activity is first loaded and clicklisteners() which contains click listener implementation of image views and bottom navigation view included in top and bottom bar.
public void loadFrag() {
HomeFrag = new HomeFragment();
getSupportFragmentManager().beginTransaction().replace(R.id.frameLayout, HomeFrag,
HomeFrag.getClass().getSimpleName()).addToBackStack("fragment").commit();
}
public void clickListeners() {
bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
#SuppressLint("NonConstantResourceId")
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
switch (item.getItemId()) {
/* when user opens home fragment, title and back button will be set to gone
in order to display his location on home */
case R.id.homePage:
Fragment fragment = new HomeFragment();
getSupportFragmentManager().beginTransaction().replace(R.id.frameLayout, fragment,
fragment.getClass().getSimpleName()).commit();
iv_location.setVisibility(View.VISIBLE);
tv_location.setVisibility(View.VISIBLE);
iv_edit.setVisibility(View.VISIBLE);
tv_title.setVisibility(View.GONE);
iv_back.setVisibility(View.GONE);
currentFragment = fragment;
break;
/* when user opens wallet fragment, location and edit location icon will be set to gone
in order to display tab title and back icon inside wallet fragment */
case R.id.wallet:
Fragment fragment1 = new WalletFragment();
getSupportFragmentManager().beginTransaction().replace(R.id.frameLayout, fragment1,
fragment1.getClass().getSimpleName()).commit();
iv_location.setVisibility(View.GONE);
tv_location.setVisibility(View.GONE);
iv_edit.setVisibility(View.GONE);
tv_title.setVisibility(View.VISIBLE);
iv_back.setVisibility(View.VISIBLE);
tv_title.setText("Wallet");
currentFragment = fragment1;
break;
/* when user opens myorders fragment, location and edit location icon will be set to gone
in order to display tab title and back icon inside myorders fragment */
case R.id.myorders:
Fragment fragment3 = new MilkCheeseAndYogurtFragment();
getSupportFragmentManager().beginTransaction().replace(R.id.frameLayout, fragment3,
fragment3.getClass().getSimpleName()).commit();
iv_location.setVisibility(View.GONE);
tv_location.setVisibility(View.GONE);
iv_edit.setVisibility(View.GONE);
tv_title.setVisibility(View.VISIBLE);
iv_back.setVisibility(View.VISIBLE);
tv_title.setText("My Orders");
currentFragment = fragment3;
break;
/* when user opens search fragment, location and edit location icon will be set to gone
in order to display tab title and back icon inside search fragment */
case R.id.search:
Fragment fragment4 = new MilkAndMilkPowderFragment();
getSupportFragmentManager().beginTransaction().replace(R.id.frameLayout, fragment4,
fragment4.getClass().getSimpleName()).commit();
iv_location.setVisibility(View.GONE);
tv_location.setVisibility(View.GONE);
iv_edit.setVisibility(View.GONE);
tv_title.setVisibility(View.VISIBLE);
iv_back.setVisibility(View.VISIBLE);
tv_title.setText("Search");
currentFragment = fragment4;
break;
}
return true;
}
});
Now this all is happening on a single thread which is displaying home fragment after a delay of 2-6 seconds approximately on cold start. I have read about AsyncTask and Threads too but I'm unable to find any proper tutorial or code to take hint on how to implement it. If there's any approach better than that please let me know. Ask more if my question is still not clear.
Im trying with no success to disable the back key only in one fragment, in the first one.
I did the override of the onBackPressed method on the Activity that contains the fragment with this:
#Override
public void onBackPressed() {
if (this.fragmentManager.getBackStackEntryCount() > 1) {
//getFragmentManager().popBackStack();
super.onBackPressed();
} else {
//super.onBackPressed();
}
}
So, it works perfectly. BUT when i finish all the path of fragments, at the final fragment i have a button that take me back to the first fragment. I will paste the code where i load this fragment again.
for (Fragment fragment1:getSupportFragmentManager().getFragments()) {
getSupportFragmentManager().beginTransaction().remove(fragment1).commit();
}
MontoFragment montoFragment = new MontoFragment();
cargarFragment(montoFragment);
And here the "cargarFragment()" method
public void cargarFragment(Fragment fragment){
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.setCustomAnimations(R.anim.animation_slide_in,R.anim.animation_slide_out);
fragmentTransaction.replace(R.id.layoutContenedorFragments,fragment);
fragmentTransaction.addToBackStack(null).commit();
}
After i complete all the fragments and i go back to the first one with that, my onBackPressed method doesnt work anymore, because this.fragmentManager.getBackStackEntryCount() > 1 now is 5 or 6 i dont remember now. And this make the back key enable.
So i dont know what to do.
Thanks!
I just achieve it with this code
#Override
public void onBackPressed() {
Fragment currentFragment = getSupportFragmentManager().findFragmentById(R.id.layoutContenedorFragments);
if (currentFragment instanceof MontoFragment){
}else{
super.onBackPressed();
}
}
i got the current fragment and if this one is an instanceOf That Fragment just do nothing but if not just do the normal onBackPressed.
I look that it works great
try to use popBackStack, replace code block:
for (Fragment fragment1:getSupportFragmentManager().getFragments()) {
getSupportFragmentManager().beginTransaction().remove(fragment1).commit();
}
MontoFragment montoFragment = new MontoFragment();
cargarFragment(montoFragment);
by
void popFragments(int number) {
for (int i = 0, i < number, i++) {
supportFragmentManager.popBackStackImmediate()
}
}
number here is the number of fragments you want to remove, ex: we have fragment 1,2,3,4, so when we are in fragment 4 and want to back to fragment 1, so the number will be 3.
I have one User Activity class which has drawer menu (includes home page, my profil etc..). When user clicks the one of the menu item, I will load fragment such as HomePageFragment, MyProfilFragment etc..
Here is the code example:
public void navigationItemSelectedListener(DrawerLayout drawerLayout, NavigationView navigationView) {
navigationView.setNavigationItemSelectedListener(menuItem -> {
menuItem.setChecked(true);
int menuId = menuItem.getItemId();
switch (menuId){
case R.id.home_menu_item:
loadFragment(new HomePageFragment());
break;
// goes like that
Here is the loadFragment:
public void loadFragment(Fragment fragment) {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.user_page_activity_frame_layout, fragment);
fragmentTransaction.commit();
}
}
Now I have run CountDownTimer(60 seconds) in HomePageFragment, when counter is 0, I will run some methods().
However I can not do that. Because each time user navigates new item, then when back to the HomePage menu item, new HomePageFragment() is creating, therefore CountDownTimer starts with 60s again and again. What I want CounDownTimer to run even if user navigates another menu.
Note that, I had tried to create HomePageFragment attribute as global. But it didn't work. (Like 2 timer counts down. One says 60-59... another says 30-29 ...)
Problem here is, your Fragment gets destroyed every time user navigates to another item.
To solve this
1.Either Create the Timer in your host activity instead the fragment, maybe inside navigationItemSelectedListener()
2.Make sure your fragment doesn't get destroyed everytime. Check How to resume Fragment from BackStack if exists
I'm starting to work with fragments and ran into a problem. I have file viewing fragments(more than one) and a search fragment. To make things easier I add my file viewing fragment to backstack whenever I go to search so I could easily pop it back if I decide to just close it. Now the problem is that when I press on file in the search fragment so that I travel to the specific fragment in which that file is, the onCreateView of the first fragment(which was in backstack) always gets called even though I replaced it(?) and I don't want that. Here are things I do to change the fragments on search file press:
1.I call pop to get to previous fragment
#Override
public boolean onSearchViewClose() {
if (getActivity() != null) {
getActivity().getSupportFragmentManager().popBackStack();
return true;
}
return false;
}
2. Then I replace the remaining fragment
public void setFragment(Fragment fragment, String tag, boolean clearBackStack, boolean addToBackStack) {
// Insert the fragment by replacing any existing fragment
hideLoadingProgress();
FragmentManager fragmentManager = getSupportFragmentManager();
if (clearBackStack) {
clearBackStack(fragmentManager);
}
FragmentTransaction transaction = fragmentManager.beginTransaction().replace(R.id.content_frame, fragment, tag);
if (addToBackStack) {
transaction.addToBackStack(null);
}
transaction.commit();
}
while clearing backstack:
public void clearBackStack(FragmentManager fragmentManager) {
for (int i = 0; i < fragmentManager.getBackStackEntryCount(); ++i) {
fragmentManager.popBackStack();
}
}
Even if I don't clear backstack the problem persists.Thanks for your answers in advance.
The answer was simple - I should've just used getActivity().getSupportFragmentManager().popBackStackImmediate(); when exiting search view as the earlier method didn't work because it waited for program to return to its event loop (asynchronous).
I use in my Android application a TabListener similar to this one: http://developer.android.com/guide/topics/ui/actionbar.html#Tabs
My onTabSelected implementation:
public void onTabSelected(Tab tab, FragmentTransaction ft) {
Fragment preInitFrag = mActivity.getFragmentManager().findFragmentByTag(mTag);
if (preInitFrag == null) {
mFragment = Fragment.instantiate(mActivity, mClass.getName(), mArgs);
ft.add(android.R.id.content, mFragment, mTag);
} else {
ft.attach(preInitFrag);
}
}
Every time a tab is selected, I want to add it to the back stack. How can I do this? Using the parameter ft with ft.addToBackStack("test"), it does not work. It throws a fatal exception:
java.lang.RuntimeException:
Unable to start activity ComponentInfo{.../...BaseActivity}:
java.lang.IllegalStateException:
This FragmentTransaction is not allowed to be added to the back stack.
The ActionBar guide that you linked to in your question has this to say about the back stack (In the "caution" section below the tab listener sample code):
You also cannot add these fragment transactions to the back stack.
The guide doesn't explain why, but what you want is not supported. You'll have to keep tag history in some other way, or don't keep tab history at all.
Create you own FragmentTrasaction inside the onTabChanged callback, and try commit();
Check the below link.
FragmentTransaction is not allowed to be added to back stack?