Finish application from parent class

Have some problem with android finish() methods. I have one parent-class activity. Lets call it ParentActivity. All other activities in my project extends ParentActivity. Each time on ParentActivity.onCreate there are some statement, and I want to stop activity from executing if it fails. But when I call finish() in parent, I cant stop executing onCreate method on its child. Something like that:

public class ParentActivity extends Activity {
     @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (!someStatement) finish();
}

public class Test extends ParentActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);        
    Log.d("TAG", "I dont want this code!");
}

}

Surely, I can just verify in parent activity its status each time, but I dont think its a good idea.

public class Test extends RexActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (isFinishing()) return; /// It works - but it bad :(((
    Log.d("TAG", "I dont want this code!");
}

}

Can I somehow stop executing onCreate method on child activity from its parent? Many thanks for any help!

Upvotes: 1

Views: 161

Answers (2)

Akshay Chordiya
Akshay Chordiya

Reputation: 4841

I'm not sure if I got your question right. As you have some grammatical issues.

  1. The onCreate statements are always executed. You can either have a Boolean in ParentActivity to stop the code from executing in ChildActivity#onCreate().
  2. You can try making your onCreate() code more modular by dividing it into functions so that it's not called.

Let me know what works for you.

Upvotes: 1

Androider
Androider

Reputation: 3873

Best option is to Use finish() in your splash screen just before you create your second activity,

Upvotes: 0

Related Questions