Start DialogFragment from Activity

Issue

I’m learning Android programming with IntelliJ right now and got a little problem.

I’ve got an Activity which looks like this:

public class example2 extends Activity {
    ...some code...
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.optExit:
                finish();
                return true;
            case R.id.optSettings:
                Intent sintent = new Intent(this, settings.class);
                startActivity(esintent);
                return true;
            case R.id.optAbout:
                //need to start the fragmentdialog

        }
        return true;
    }
    ...some code...
}

And this is how my DialogFragment looks like

public class about extends DialogFragment {

    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder b = new AlertDialog.Builder(getActivity());
        b.setTitle("About");
        b.setMessage("some text");
        b.setCancelable(false);
        b.setPositiveButton("OK", null);
        return b.create();
    }
}

I’ve tried nearly everything, creating a new instance and start the method, using FragmentManager, which i wasn’t able to use. What should I do?

Solution

for approved namings use About instead of about its just

new About().show(getSupportFragmentManager(),"about");

Answered By – Elltz

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published