Android: How to start an Activity from an alert dialog

前端 未结 2 565
醉话见心
醉话见心 2020-12-17 05:04

I need to start an activity when the user chooses an item in an alert dialog. How do I get the context to pass to the intent constructor in the following code...

<         


        
相关标签:
2条回答
  • 2020-12-17 05:32

    You can retrieve the context you have passed to AlertDialog.Builder with getBaseContext(). See the doc here.

    So this should work:

    Intent i = new Intent(getBaseContext(), <new activity>.class);    
    startActivity(i);
    
    0 讨论(0)
  • 2020-12-17 05:32
    Intent i = new Intent("some.thing.activity"); 
    startActivity(i);
    

    //in the brackets you can add the activity path, ie: the package name //eg: za.ac.cut.Activity

    0 讨论(0)
提交回复
热议问题