Home > Net >  how to use activity context from other activity?
how to use activity context from other activity?

Time:02-05

I want to use activity context from another activity simple code example below Any idea ?

public class Activity_A extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_Activity_A);
}
}

public class Activity_B extends AppCompatActivity {
Dialog dialog1 ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_Activity_B);

dialog1 = new Dialog(I want Activity_A Context) ; // Is this possible ??

}
}

Thanks

CodePudding user response:

Tamim you can definitely achieve this using a different class by making a public static function in that other class

public class Utils {

 public static void showDialog(Context context){
 //// your code here
 }

now you use it wherever you want...

Utils.showDialog(this);
  •  Tags:  
  • Related