3

I'm developing an Android app for a school project and i have the following problem. I have a MainActivity with a Button and a SecondActivity. When I click on the button in the MainActivity it have to open the SecondActivity. I tested it on my two devices (samsung galaxy s9+ and asus zenfone2):

MainActivity.java

public class MainActivity extends AppCompatActivity {
    Button button;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button=(Button)findViewById(R.id.button);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(MainActivity.this,SecondActivity.class)
                startActivity(intent);
            }
        });
}
}

This works fine on both devices and when i click on the button it correctly opens the SecondActivity.

The problem is when i add a controller class and try to start the SecondActivity in it. This is the controller class:

Controller.java

public class Controller {
    public void open(Context cont){
        Intent intent=new Intent(cont,SecondActivity.class);
        cont.getApplicationContext().startActivity(intent);
     }
  }

And I change the MainActivity this way:

public class MainActivity extends AppCompatActivity {
    Button button;
    Controller c;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button=(Button)findViewById(R.id.button2);
        c=new Controller();

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                c.open(getApplicationContext());
            }
        });
}
}

This works fine on my s9+, while on my zenfone2 crashes when i click on the button. Where is the problem? if it's not correct, why works on s9+?

Thank you

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Steve
  • 33
  • 3
  • public class controller { public void open(MainActivity cont){ Intent intent=new Intent(cont,SecondActivity.class); cont.getApplicationContext().startActivity(intent); } } try this – Chickenturtle Oct 07 '18 at 11:17

4 Answers4

5

As of Android P you can't start an activity with the application context unless you add Intent.FLAG_ACTIVITY_NEW_TASK flag. So just change your controller and mainactivity to use the mainactivity context instead.

c.open(MainActivity.this);

public void open(Context context) {
    Intent intent = new Intent(context, SecondActivity.class);
    context.startActivity(intent);
}

Or

public void open(Context context) {
    Intent intent = new Intent(context.getApplicationContext(), SecondActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    context.startActivity(intent);
}
MidasLefko
  • 4,499
  • 1
  • 31
  • 44
  • Wow thank you so much!!! It works (also without flag). I've just changed c.open(getApplicationContext()) with c.open(MainActivity.this) – Steve Oct 07 '18 at 11:42
0

Try this,

c.open(MainActivity.this);

and in Controller class do following :

public void open(Context cont){
        Intent intent=new Intent(cont,SecondActivity.class);
        cont.startActivity(intent);
     }
Zaid Mirza
  • 3,540
  • 2
  • 24
  • 40
  • Thanks for your reply!!! It works but using MainActivity.this instead of MainActivity.class – Steve Oct 07 '18 at 11:43
0

Remove getApplicationContext() from your Controller class in open function. Make it cont.startActivity(intent);

Muhammad Awais
  • 448
  • 5
  • 17
0

You cannot call startActivity from getApplicationContext anymore and you don't need to call it so many times.

Replace the getApplicationContext with "this".

button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                c.open(MainActivity.class);
            }
        });

and then just call startActivity

public class Controller {
    public void open(Context cont){
        Intent intent=new Intent(cont,SecondActivity.class);
        cont.startActivity(intent);
     }
  }
mariuss
  • 1,177
  • 2
  • 14
  • 30
Itamar Kerbel
  • 2,508
  • 1
  • 22
  • 29
  • Thanks for your reply. I've tried to change the code how you suggest, but in the MainActivity changing 'getApplicationContext()' with 'this' i have the following error: open(android.content.Context) in controller cannot be applied to (anonymous android.view.View.OnClickListener) – Steve Oct 07 '18 at 11:34
  • Ahhh sorry didn't notice it was anonymous. Corrected it. You need to call the outer class by name. – Itamar Kerbel Oct 07 '18 at 11:42