DialogFragment does not close after its display followed by device orientation change

0

Hello. I'm doing a test with a simple one-screen app and a button that should open a Dialog that displays a placard.

The openScoreBoardDialog () method opens a Dialog Fragment with 2 buttons ("New Game" and "Back"), clicking the "New Game" button will execute the " closeScoreBoardDialog () "from Acitity.

The problem occurs after displaying a DialogFragment. If I change the screen orientation the method " getSupportFragmentManager (). FindFragmentByTag (" testDialog ")" returns null and does not close the DialogFragment. I believe that android is re-creating the fragment and losing the DialogFragment reference. Does anyone know how to solve or circumvent it?

Activity Code:

public class MainActivity extends AppCompatActivity implements OptionsDialogIntf,Serializable{

    private TestDialog testDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void openScoreBoardDialog(View view) {

        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        testDialog = new TestDialog();

        Bundle args = new Bundle();

        args.putSerializable("classCallBack", MainActivity.this);
        args.putInt("yellowWins(", 0);
        args.putInt("redWins", 0);

        testDialog.setArguments(args);
        testDialog.show(ft, "testDialog");

    }

    private void closeScoreBoardDialog() {

        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        testDialog = (TestDialog) getSupportFragmentManager().findFragmentByTag("testDialog");
        if (testDialog != null) {
            testDialog.dismiss();
            ft.remove(testDialog);
        }

    }

    @Override
    public void dialogAnswer(Integer opcao) {

        if (opcao == 0) {
            Log.i("teste","opcao 0");
        } else {
            finish();
        }

        closeScoreBoardDialog();

    }
}

DialogFragment Code:

public class TestDialog extends DialogFragment implements Serializable{

    private static final String TESTE = "TESTE";
    private OptionsDialogIntf classCallBack;
    private int yellowWins;
    private int redWins;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Log.i(TESTE,"onCreate");

        classCallBack = (OptionsDialogIntf) getArguments().getSerializable("classCallBack");
        yellowWins = getArguments().getInt("yellowWins(");
        redWins = getArguments().getInt("redWins");

        setCancelable(false);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);

        Log.i(TESTE,"onCreateView");

        View view = inflater.inflate(R.layout.scoreboard, container);

        Button btnNewGame = view.findViewById(R.id.btnNewGame);
        Button btnBack = view.findViewById(R.id.btnBack);
        TextView yellowScore = view.findViewById(R.id.yellow_score);
        TextView redScore = view.findViewById(R.id.red_score);

        yellowScore.setText(Integer.toString(yellowWins));
        redScore.setText(Integer.toString(redWins));


        btnNewGame.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                classCallBack.dialogAnswer(0);
            }
        });

        btnBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                classCallBack.dialogAnswer(1);
            }
        });

        return view;
    }
}
    
asked by anonymous 21.05.2018 / 21:41

0 answers