Sometimes we pass context of an activity as a parameter to some other function and later we might want the activity too. So instead of passing the activity also to that function, we can get the activity from context itself.

Method 1

You can get the activity from the context by type casting the context to activity.

Activity activity = (Activity) _context;

Ravi is hardcore Android programmer and Android programming has been his passion since he compiled his first hello-world program. Solving real problems of Android developers through tutorials has always been interesting part for him.

  • Nagendra

    It would be great, if can you provide sample snippet code for the above tip?

  • onewhodoesnotcareaboutfools

    He just did, and it helped me nothing :S

  • Kien Le

    It not work 🙁

  • Michael

    Actually, not very useful tip as it can cause ClassCastException. So you must always embrace this code into trycatch block( when you using Service for example).

  • viral

    i am a new student , I am the new in this filed. so please help me, which software i have download? for make android apps.

    • silicon mohit

      android studio..

  • Ammoula Abidi

    hi, i’m student and i’m new in android . i have a problem “how to devellope code USSD with android