2

I want to do a test for a condition in an Activity's onCreate() which if true will call finish(). Is there any code which must be called before finish() is called? (And must any code be run after finish() is called?)

e.g.

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState); // I'm pretty sure this is always required
    setContentView(R.layout.activity_main); // is this required?
    finish();
}
Jodes
  • 14,118
  • 26
  • 97
  • 156

1 Answers1

2

You can freely call finish() in onCreate without calling some extra methods, for example:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState); //REQUIRED

    if (isVariable) {
     finish();
    } else {
     setContentView(R.layout.activity_main);
     //code here
    }
}

And when you call finish It will not stop executing method, so make sure nothing is below finish() line.

And you should not call setContentView If you will call finish in onCreate to save memory/speed.

I have tested this approach and it works fine.

Yuraj
  • 3,185
  • 1
  • 23
  • 42