简体   繁体   中英

Android: How to start an Activity from an alert dialog

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...

builder.setItems(items, new DialogInterface.OnClickListener() {

        public void onClick(DialogInterface dialog, int item) {

                Intent i = new Intent(<WHAT DO I PUT HERE?>, <new activity>.class);    
                startActivity(i);

      }

});

Is it the use of the inner class?? Any thoughts?

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);
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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM