2

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?

nerdy kid
  • 371
  • 1
  • 3
  • 14
  • What part are you having trouble with- detecting the back button or removing the fragment? Either you should have no problem finding dozens of examples on – Gabe Sechan Jan 27 '17 at 16:02
  • Just for confirmation. Are you trying to use fragment for a dialog box? – Mohammed Atif Jan 27 '17 at 16:04
  • Possible duplicate of [Android Fragment handle back button press](http://stackoverflow.com/questions/7992216/android-fragment-handle-back-button-press) – Mohammed Atif Jan 27 '17 at 16:05
  • the fragment is within the activity. it is not a dialog box. the fragment has an edit text to enter the number of quantity and with "OK" button. – nerdy kid Jan 27 '17 at 16:06
  • @nerdykid So basically you are trying to use fragment as a dialog box (150X150 means its a clear dialog box) just to take an input? – Mohammed Atif Jan 27 '17 at 16:06
  • https://www.mkyong.com/android/android-prompt-user-input-dialog-example/ – Mohammed Atif Jan 27 '17 at 16:08
  • https://developer.android.com/guide/topics/ui/dialogs.html – Mohammed Atif Jan 27 '17 at 16:08
  • http://www.viralandroid.com/2016/04/android-user-input-dialog-example.html – Mohammed Atif Jan 27 '17 at 16:09
  • Do tell me if you need more references to achieve your result with simple dialog instead of a fragment. – Mohammed Atif Jan 27 '17 at 16:09
  • @Mohammed Atif yes. But I don't know why the problem now is that i'm using a fragment.? it functions well. the problem is just when I press the back button it should remove the fragment. – nerdy kid Jan 27 '17 at 16:10
  • 1
    my question is simple. i don't need to rework what I've done. – nerdy kid Jan 27 '17 at 16:11
  • OK, I will tell you the problem, Fragment has higher impact on memory and performance when compared to Dialog. For your fragment issue, I have already marked a duplicate question. But ideally I must you suggest a better solution. Right? – Mohammed Atif Jan 27 '17 at 16:11

3 Answers3

0

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

Maryeme Alaoui
  • 93
  • 2
  • 13
  • What happens if the user pressed the back button while the popupwindow is still open? will it automatically close? – nerdy kid Jan 28 '17 at 02:44
0

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).

Luke
  • 1,284
  • 1
  • 12
  • 34
0

Use popbackstack on backpress of activty by overriding it.

@Override
public void onBackPressed() {
    if (getFragmentManager().getBackStackEntryCount() > 0) {
        getFragmentManager().popBackStack();
    } else {
        super.onBackPressed();
    }
}
IteratioN7T
  • 363
  • 8
  • 21