Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Start DialogFragment from Activity

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?

like image 872
Markus Avatar asked Oct 31 '15 23:10

Markus


People also ask

How do I start a DialogFragment?

To create a DialogFragment , first create a class that extends DialogFragment , and override onCreateDialog() , as shown in the following example. Similar to how onCreateView() should create a root View in an ordinary fragment, onCreateDialog() should create a Dialog to display as part of the DialogFragment .

Is DialogFragment deprecated?

This class was deprecated in API level 28.

What is the difference between dialog and DialogFragment?

Dialog: A dialog is a small window that prompts the user to make a decision or enter additional information. DialogFragment: A DialogFragment is a special fragment subclass that is designed for creating and hosting dialogs.


1 Answers

for approved namings use About instead of about its just

new About().show(getSupportFragmentManager(),"about");
like image 108
Elltz Avatar answered Nov 03 '22 23:11

Elltz