How to close a fragment when back button is pressed? - android

I have an activity wherein if i click the "BUY" button, it will open a fragment about (150x150 pixels) to ask for the "quantity". If the user will press the back button, it will just simply close the fragment. Any Ideas about closing the fragment?

If you really want to use Fragment instead of dialog as suggested and want to remove it, keep a reference to it when creating it and then use getFragmentManager().beginTransaction().remove(fragment).commit() (you can also detach instead of remove if you only want to detach it from the activity but not destroy it completely).

You don't need to do it in a fragment it's too much for this simple use, i suggest you to use a PopupWindow:
When you click on Buy Button, you show the popup.
PopupWindow is enough customizable to make your layout as you want
here an example : https://android--code.blogspot.com/2016/01/android-popup-window-example.html

Use popbackstack on backpress of activty by overriding it.
#Override
public void onBackPressed() {
if (getFragmentManager().getBackStackEntryCount() > 0) {
getFragmentManager().popBackStack();
} else {
super.onBackPressed();
}
}

Related

Saving Fragment State on Back Button Select

I'm so very confused and have been reading on this topic for a while.
I have a MainActivity that has multiple possible contents (switched between via navigation drawer), which I've set via multiple fragments (lets call them Fragment1, Fragment2 and Fragment3).
That works fine.
One of my fragments, Fragment3, is a View that can segue to a new activity, View2.
View2 has a back button. When I press on the View2 back button I want to see Fragment3 on my MainActivity, not Fragment1, which is what I currently get. This is because OnCreate, by default, loads Fragment1.
What is the best way to do this?
Thanks so much in advance! (vent: iOS makes this so much easier).
The official documentation for Fragments states that you should make sure to call addToBackStack() before commiting your fragment transaction on your first activity holding the 3 fragments.
In order to go back to the last used fragment from the second activity , you should override the onBackPressed() method in this activity :
#Override
public void onBackPressed() {
if (getFragmentManager().getBackStackEntryCount() > 0) {
getFragmentManager().popBackStack();
} else {
super.onBackPressed();
}
}
The documentation is very complete on the subject : Read it here
Update:
I just added this to the back button code:
finish();
return true;
I had to do it within onOptionsItemSelected(MenuItem item)... for some reason onBackPressed is not fired.

How to prevent application closed on pressing device back button?

I am developing application in which I have three fragment A,B,C, when I press on some button in A it navigate to b Thats fine ,and when I press device back button application closes instead of going to A,How can I prevent this problem .
// Your Main Activity
// Override OnBackPressed Event Which as below
#Override
public void onBackPressed() {
if (getSupportFragmentManager()
.getBackStackEntryCount() > 0) {
super.onBackPressed();
} else {
UIUtils.showAlertDialog(this, getString(R.string.app_name), "Are you sure want to Exit App?", false);
}
}
Just override the activity's onBackPressed() function according to your need.
You should override onBackPressed() method. There you can choose what action to do when this happens. If you still want to finish the activity in some cases, you can call finish() method.
Please add
addToBackStack(null);
to your FragmentTransaction object if you not added when replacing or
adding fragments.
it will autamatically maintain backstacks on Backpress.
Hope it will help you !

how to maintain the fragment depend upon back button?

I've got a navigation drawer. In that navigation drawer I've got five fragments. When I select a fragment, it shows up. When I press the back button, it goes to the previous fragment. However, I need the back button to send you back to the first fragment. How do I do this?
Overwrite the onBackPressed() method in your Activity:
#Override
public void onBackPressed(){
FragmentTransactionn ft = getFragmentManager().beginTransaction();
ft.replace([your fragment container], yourfirstFragment, TAG_FRAGMENTFIRST);
ft.commit();
}
Apparently you already figured out how to show fragments. I'd suggest using the same code you use in your navigation drawer in the public void onBackPressed() to draw up the first fragment again. To make users able to exit the app, check if the first fragment is already visible. If so, call super.onBackPressed() or finish().

Android navigation drawer exits on back pressed

I am using navigation drawer in my MainActivity.java to switch fragments and I am also extending my MainActivity to the other activities in my app. Now the problem is that, when I press back button during I am on fragment, the app suddenly exits without any notification. And if I use OnBackPressed in my MainActivity it is bydefault implemented to the other activities extending main activity too and when I press back button to any of that acivity it asks me first for confirmation and then comes back to previous activity or frag. Need a solution to avoid this. I want to set onbackpessed or anything that shows dialogue or asks for confirmation to exit app on fragments only but dont know howto do.
Or any help regarding fragment back stacking to open a fixed fragment on back button pressed is welcomed if it tells how to change the title of hence opened fragment too.
call onBackPressed() in that activity in which you have put all fragment or main activity
#Override
public void onBackPressed() {
if (getFragmentManager().getBackStackEntryCount() == 0) {
//Display your dialog here
} else {
getFragmentManager().popBackStack();
}
}
Other solution
#Override
public void onBackPressed() {
if (slidingMenu.isMenuShowing()) {
slidingMenu.toggle(true);
} else {
super.onBackPressed();
//Display your dialog
}
}
This is works for me.

Detecting the visible fragment in android

I want to implement the onbackpressed() in android and my code is as follows
public void backpressed(){
NDListeningFragment fragment1=(NDListeningFragment)getSupportFragmentManager().findFragmentByTag(ConnectedDevicesFragment.TAG);
if(fragment1!=null && fragment1.isVisible())
{
super.onBackPressed();
}
else
{
fragment1=(NDListeningFragment)SimpleFragmentFactory.createFragment(ConnectedDevicesFragment.TAG);
getSupportFragmentManager().beginTransaction().replace(R.id.content,fragment1).commit();
fragment1.setUserVisibleHint(true);
}
}
The above code checks if the visible fragment is ConnectedDevicesFragment. If yes then super() is called and if not then I create ConnectedDevicesFragment and replace it in the framelayout.
But I am not able to implement in this way. When I press back button it reloads the Connected DevicesFragment again and again.
can you help with some workaround.
Cheers!
You creates fragment1 object every time in the onBackPressed function it means it will not null and it is on invisible state. You need to add NDListeningFragment in backstack when you open NDListeningFragment first and check Is the fragment available in back stack. If yes then call super.onBackpressed.

Categories

Resources