How are you able to disable all fragment animations including the exit animation when clearing the back stack, currently I am using the following code but it doesn't seem to stop the animation exiting the screen, rather it only stops the animation of the new fragment entering the screen.
The code that I'm using is as follows:
Menu Fragment - code used to clear back stack
private void clearBackStack() {
FragmentUtils.sDisableFragmentAnimations = true;
getSupportFragmentManager().popBackStackImmediate(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
FragmentUtils.sDisableFragmentAnimations = false;
}
Fragment Utils Class:
public class FragmentUtils {
public static boolean sDisableFragmentAnimations = false;
}
All fragments have this:
#Override
public Animation onCreateAnimation(int transit, boolean enter, int nextAnim) {
if (FragmentUtils.sDisableFragmentAnimations) {
Animation a = new Animation() {};
a.setDuration(0);
return a;
}
return super.onCreateAnimation(transit, enter, nextAnim);
}
Navigation menu code (where I want to disable the animations)
bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.action_events:
replaceFragment(new MainFragment());
break;
case R.id.action_promotions:
replaceFragment(new MainFragment());
break;
case R.id.action_menu:
clearBackStack();
replaceFragment(new MenuFragment());
break;
}
return true;
}
});
public void replaceFragment(BaseFragment fragment, int resId, boolean addToBackStack) {
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.setCustomAnimations(0, 0);
transaction.replace(resId, fragment);
if (addToBackStack) {
transaction.addToBackStack(null);
}
transaction.commitAllowingStateLoss();
}
Related
So i have a problem with my local variable it says Unreachable statement.
This is the line of code i get the error (" View textViewOptions; ") i cant even run the app
private View findViewById(int position) {
View view = findViewById(R.id.textViewOptions);
return view;
View textViewOptions;
textViewOptions.setOnClickListener(new View.OnClickListener() {
ViewHolder holder;
public void onClick(View view) {
//creating a popup menu
PopupMenu popup = new PopupMenu(context, holder.textViewOptions);
//inflating menu from xml resource
popup.inflate(R.menu.chatmenu);
//adding click listener
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.BlockUser:
//handle menu1 click
return true;
case R.id.MuteNotificationsCH:
//handle menu2 click
return true;
default:
return false;
}
}
});
popup.show();
}
});
}
As mentioned in commnets, return keyword is used to exit the method. From there onwards execution will stop. So the code below return statement will not be executed and hence will become unreachable.
So put return view; as the last statement of the method
I have taken one activity and with activity I have attached navigation drawer and the home fragment. In that navigation drawer there is a option of "Contact Us". When user click on that option a fragment gets open. But I am not able to maintain the stack of that. means when I am on contact us fragment then using navigation drawer again click on contact us it overlaps previous one. I have to press back button 2 times to go on home fragment. Please Help me how to maintain back stack for this. Here is my code..
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
int id = item.getItemId();
switch (id) {
case R.id.rate:
Uri uri = Uri.parse("market://details?id=" + getPackageName());
Intent myAppLinkToMarket = new Intent(Intent.ACTION_VIEW, uri);
try {
startActivity(myAppLinkToMarket);
} catch (ActivityNotFoundException e) {
Toast.makeText(getApplicationContext(), "Unable to find source market app!", Toast.LENGTH_SHORT).show();
}
break;
case R.id.contact_us:
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.add(R.id.fragment_container_dashboard, ContactUsFragment.newInstance());
transaction.addToBackStack(null);
transaction.commit();
mDrawerLayout.closeDrawer(GravityCompat.START);
break;
}
return false;
}
});
After this code I use this to maintain back stack:
#Override
public void onBackPressed() {
if (getFragmentManager().getBackStackEntryCount() != 0) {
getFragmentManager().popBackStack();
}
else new AlertDialog.Builder(this)
.setMessage("Are you sure you want to exit?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Dashboard.super.onBackPressed();
}
})
.setNegativeButton("No", null)
.show();
}
But the issue is It's show exit dialogue on contact us fragment when ever I press back button. But I want firstly I reach to home fragment and after that If I press back button then it show exit dialogue.
I solved this by adding this in the onCreate method.
getSupportFragmentManager().addOnBackStackChangedListener(new FragmentManager.OnBackStackChangedListener() {
#Override
public void onBackStackChanged() {
if(getSupportFragmentManager().getBackStackEntryCount() == 0){
drawer.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);
menuToggle.setDrawerIndicatorEnabled(true);
// your dialog
}else{
drawer.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
menuToggle.setDrawerIndicatorEnabled(false);
// remove to your previous fragment
}
}
});
I am trying to use a GitHub library (MeowBottomNavigation)in Android Studio.But its written in kotlin and i cant use the listeners in it.
The only thing which is given is this
bottomNavigation.setOnShowListener {
}
bottomNavigation.setOnClickMenuListener {
}
the suggestions shows to use
(Function1)
i am not sure as to how to implement this in java . Any help will be appreciated.
I am familiar with java but the library is written in Kotlin. Is there any way to use these listeners in java?
bottomNavigation.setOnClickMenuListener(new
Function1<MeowBottomNavigation.Model, Unit>() {
#Override
public Unit invoke(MeowBottomNavigation.Model p1) {
int i = p1.getId();
switch (i){
case 4:
Toast.makeText(UserMainActivity.this, i, Toast.LENGTH_SHORT).show();
break;
case 1:
Toast.makeText(UserMainActivity.this, i, Toast.LENGTH_SHORT).show();
break;
case 2:
Toast.makeText(UserMainActivity.this, i, Toast.LENGTH_SHORT).show();
break;
case 3:
Toast.makeText(UserMainActivity.this, i, Toast.LENGTH_SHORT).show();
break;
}
return Unit.INSTANCE;
}
});
Function0, Function1, Function2, ... FunctionN are higher-order functions in kotlin.
After converting to java, your click listeners become something like below.
// Set Menu Click Listener
bottomNavigation.setOnClickMenuListener(new Function1<MeowBottomNavigation.Model, Unit>() {
#Override
public Unit invoke(MeowBottomNavigation.Model p1) {
return Unit.INSTANCE;
}
});
// Set Menu Show listener
bottomNavigation.setOnShowListener(new Function1<MeowBottomNavigation.Model, Unit>() {
#Override
public Unit invoke(MeowBottomNavigation.Model s) {
return Unit.INSTANCE;
}
});
something like This::
bottomNavigation.setOnShowListener( new IBottomNavigationListener(Model model)
{
} );
if you are using fragments
//1.-declare fragments globally in your activity
private HomeFragment homeFragment = new HomeFragment();
//2.- declare a method to switch between fragments
public void loadFragment(Fragment fragment){
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.yourFragmentContainer,fragment);
transaction.commit();
}
//3.- in the Set Menu Click/show Listener call the fragment to show
// Set Menu Click Listener
bottomNavigation.setOnClickMenuListener(new Function1<MeowBottomNavigation.Model, Unit>() {
#Override
public Unit invoke(MeowBottomNavigation.Model model) {
int i = model.getId();
switch (i){
case 1:
loadFragment(homeFragment);
break;
//...other cases
}
return Unit.INSTANCE;
}
});
// Set Menu Show listener
bottomNavigation.setOnShowListener(new Function1<MeowBottomNavigation.Model, Unit>() {
#Override
public Unit invoke(MeowBottomNavigation.Model model) {
int i = model.getId();
switch (i){
case 1:
loadFragment(homeFragment);
break;
//...other cases
}
return Unit.INSTANCE;
}
});
use
implementation 'com.etebarian:meow-bottom-navigation-java:1.2.0'
for details watch
https://www.youtube.com/watch?v=MiphbOtSyWY
I am making an android app, and when I open up a page I can then swipe between 'Line Details' and 'Verify Line'. However when I swipe to the second page 'Verify Line' the objects i.e. textboxes in 'Line Details' move across as well but I don't want this to happen, could anyone tell me why? I also have a fragment in my code.
#Override
public int getCount() {
return mCount;
}
#Override
public Fragment getItem(int position) {
Fragment fragment = new LineDetails();
fragment.setArguments(extras);
switch (position) {
case 0:
return new LineDetails();
case 1:
return new VerifyLine();
default:
return new LineDetails();
}
}
#Override
public void finishUpdate(ViewGroup container) {
super.finishUpdate(container);
}
I have an action bar that looks like so:
I am using the following to see which button is clicked:
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case R.id.view_all_trains:
Intent i = new Intent(ToStationActivity.this, MapActivity.class);
startActivityForResult(i, 0);
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
}
return true;
}
No matter what button you press, (the back or the pin), it goes back to the previous screen and I am not sure why. Any suggestions as to why this may happen?
Put a break statement at the end of each case of the switch!
You need to finish the case at the end using break;, else the control flow continues at the next case.
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case R.id.view_all_trains:
Intent i = new Intent(ToStationActivity.this, MapActivity.class);
startActivityForResult(i, 0);
break;
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
}
return true;
}