Home > Software engineering >  How to Start an AppCompatActivity from a non AppCompatActivity Class?
How to Start an AppCompatActivity from a non AppCompatActivity Class?

Time:01-21

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.

CodePudding user response:

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

CodePudding user response:

@Gino The app crashes, would it be possible to start activity C directly in class C? like: B.java

private void show(AppCompactActivity myActivityA){
   C c = new C();
   c.show(myActivityA);
}

C.java

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