Search code examples
javaandroidandroid-layoutandroid-activity

How to Start an AppCompatActivity from a non AppCompatActivity Class?


Classes

Let's say I have my main activity A that is a AppCompactActivity, I want in this class A (for example in OnCrete) to do:

new B().show();

Class B would have a method show like :

private void show(){
   Intent c = new Intent(A.this, C.class);
   startActivity(c);
}

How can I make this work?

I tried to do the implementation above.


Solution

  • I suggest you to add a parameter to the show() method passing a reference to the activity A:

    new B().show(myActivityA);
    
    
    private void show(AppCompactActivity myActivityA){
       Intent c = new Intent(A.this, C.class);
       myActivityA.startActivity(c);
    }