Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determine if context is a Specific Activity

I'm passing the Activity context to a dialog but that dialog is global to other Activities, so its possible that other activities create that dialog too. My question is how can I determine that Activity context is a specific Activity?

I'm passing ActivityContext like this :

private Activity ActivityContext;  public MessageDialog(Activity context,int DialogStyle,int Dialog_Layout,String Msg)  {     super(context,DialogStyle,Dialog_Layout);     this.ActivityContext = context;     this.Msg = Msg; } 
like image 217
Memento Avatar asked Jan 30 '14 10:01

Memento


People also ask

Is a context an activity?

In android, Context is the main important concept and the wrong usage of it leads to memory leakage. Activity refers to an individual screen and Application refers to the whole app and both extend the Context class.

How do you pass activity context?

You can get the context by invoking getApplicationContext() , getContext() , getBaseContext() or this (when in the activity class). Context is tied with lifecycle of its activity/application and commonly used for creating new objects, accessing resources e.t.c.


2 Answers

You can use instanceof:

if ( this.ActivityContext instanceof MyActivity ) {  /// .... } 
like image 80
marcinj Avatar answered Oct 13 '22 10:10

marcinj


I know the question is in java but if you are looking the answer in the kotlin :

if (this.activity is AppActivity) {     // ... } 
like image 43
surga Avatar answered Oct 13 '22 09:10

surga