简体   繁体   中英

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

You can use instanceof:

if ( this.ActivityContext instanceof MyActivity ) {
 /// ....
}

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

if (this.activity is AppActivity) {
    // ...
}
override fun onAttach(context: Context) {
    super.onAttach(context)
    if (context is MyInterFace) {
        interfaceVariable = context as MyInterFace
    }
}

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