Start DialogFragment from Activity
Asked Answered
C

2

8

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?

Colubrid answered 31/10, 2015 at 23:29 Comment(0)
B
13

for approved namings use About instead of about its just

new About().show(getSupportFragmentManager(),"about");
Babineaux answered 31/10, 2015 at 23:42 Comment(2)
That doesn't work either, I can't access this getSupportFragmentManager() and i also don't have the android.support.v4.app package...Colubrid
well use getFragmentMananger() @ColubridBabineaux
A
0

Just use Activity Fragment Manager i.e getSupportFragmentManager() . This will Returns a FragmentManager for this controller.

Alfi answered 17/1, 2022 at 7:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.