-1

I have used the following codes for showing and canceling dialogfragment :

public static void showDialogFragment(FragmentManager fm,String type){

    FragmentTransaction ft = fm.beginTransaction();
    MyDialogFragment prev = (MyDialogFragment)fm.findFragmentByTag(type);
    if (prev != null) {
        prev.dismissAllowingStateLoss();
        ft.remove(prev);
    }
    ft.addToBackStack(null);
    MyDialogFragment newFragment = MyDialogFragment.newInstance();
    try{
        newFragment.show(ft,type);
    }catch(IllegalStateException e){
         return;
    }
}



public static void cancelDialogFragment(FragmentManager fm,String tag){
    FragmentTransaction ft = fm.beginTransaction();
    MyDialogFragment prev = (MyDialogFragment )fm.findFragmentByTag(tag);
    if (prev != null) {
        prev.dismiss();
        ft.remove(prev);

    }
    ft.addToBackStack(null);
    ft.commit();

}

when I open the activity I show a dialogFragment and after receiving the data from internet I cancel it and show the recieved data, But if I press back button again it shows the dialogFragment and I have to press back button again to dismiss it and one more time to finish the activity. I know I can override onBackPressed but I want to know why this happens? why dose it again show the dialogfragment?

What is wrong with my code?

mmlooloo
  • 18,937
  • 5
  • 45
  • 64
  • 1
    Try removing `ft.addToBackStack(null);` – Rohit5k2 Jan 12 '15 at 19:42
  • @Rohit5k2 But I have copied from document, why do I have to remove it? – mmlooloo Jan 12 '15 at 19:45
  • can you post the code for the activity class? – Krish Jan 12 '15 at 19:59
  • @Krish If I post it, I have to explain a lot of lines whats going on but the logic is as I described. – mmlooloo Jan 12 '15 at 20:03
  • Can you debug the app ? So that you can identify who is invoking your DialogFragment in the second time(onBackButtonPress after cancel)? – Krish Jan 12 '15 at 20:07
  • 3
    What you do is in `showDialogFragment()` you add this fragment to FragmentManager to backstack. Then in `cancelDialogFragment()` method you remove it from backstack with `ft.remove(prev);` So now, your backstack as it was before showing DialogFragment. But what you do next is, that you add this DialogFragment again to backstack. It is not shown, but it is on the top of backstack. That means, if you press backButton, the top item in backstack, your DialogFragment, will be shown. On the next BackPress, your DialogFragment will be dismissed. – Vojtěch Pešek Jan 12 '15 at 20:16
  • But why if I use `MyDialogFragment prev = (MyDialogFragment )fm.findFragmentByTag(tag);` after calling cancel, the prev is null? – mmlooloo Jan 12 '15 at 20:23
  • 2
    It seems like you clearly don't understand the code you are using. I'd recommend reading more documentation and experimenting with the code yourself by commenting out and adding lines to understand it better. – Dave S Jan 12 '15 at 20:25
  • I love it when people say "what's wrong with my code"? And then refuse to admit there might be problem in their code. – Greg Ennis Jan 13 '15 at 00:29

3 Answers3

1

What you do is in showDialogFragment() you add this fragment to FragmentManager to backstack. Then in cancelDialogFragment() method you remove it from backstack with ft.remove(prev);

So now, your backstack is as it was before showing DialogFragment.

But what you do next is, that you add this DialogFragment again to backstack. It is not shown, but it is on the top of backstack. That means, if you press backButton, the top item in backstack, your DialogFragment, will be shown. On the next BackPress, your DialogFragment will be dismissed.

So dont add the fragment to backstack in your cancelDialogFragment() method.

Remove this line:

ft.addToBackStack(null);
Vojtěch Pešek
  • 1,070
  • 8
  • 25
  • But why if I use `MyDialogFragment prev = (MyDialogFragment )fm.findFragmentByTag(tag);` after calling cancel, the prev is null? – mmlooloo Jan 12 '15 at 20:21
0

Replace your entire cancelDialogFragment with this:

public static void cancelDialogFragment(FragmentManager fm,String tag){
    fm.popBackStack();
}
Greg Ennis
  • 14,917
  • 2
  • 69
  • 74
  • Really bad idea because I do not know what is on top of the backStack and also I asked for the reason of why this happens? – mmlooloo Jan 12 '15 at 19:50
  • 1
    I'm so sorry. Please forgive me trying to help. Please tell me, how many fragment transactions are you doing while there is a dialog displayed? Hmmmm! – Greg Ennis Jan 12 '15 at 20:54
0

Finally I have found the reason and the correct answer. the problem is with:

ft.addToBackStack(null); 

From document:

Add this transaction to the back stack. This means that the transaction will be remembered after it is committed, and will reverse its operation when later popped off the stack.

Parameters name An optional name for this back stack state, or null.

that menas:

hey android I have removed dialogfragment from backstack (so there is nothing on the top of the backStack and the answer of @Vojtaaa9 is wrong because as I added the comment when you run MyDialogFragment prev = (MyDialogFragment )fm.findFragmentByTag(tag); after calling cancel you will get null, this means the backStack dose not have any dialogfragment) but remmber my action, remember that there was a dialogfragment but now it has removed. When user presses the back button the transaction reverses, it means that now there is nothing on the top of the backStack but then android pushes a dialogFragment to the backStack to do the transaction in a reverse order.

mmlooloo
  • 18,937
  • 5
  • 45
  • 64