简体   繁体   English

从父班完成申请

[英]Finish application from parent class

Have some problem with android finish() methods. android finish()方法有问题。 I have one parent-class activity. 我有一个家长班活动。 Lets call it ParentActivity. 让我们将其称为ParentActivity。 All other activities in my project extends ParentActivity. 我项目中的所有其他活动都扩展了ParentActivity。 Each time on ParentActivity.onCreate there are some statement, and I want to stop activity from executing if it fails. 每次在ParentActivity.onCreate上都有一些语句,如果活动失败,我想停止执行活动。 But when I call finish() in parent, I cant stop executing onCreate method on its child. 但是,当我在父级中调用finish()时,我无法停止对其子级执行onCreate方法。 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? 我可以以某种方式停止在其父项的子活动中执行onCreate方法吗? Many thanks for any help! 非常感谢您的帮助!

I'm not sure if I got your question right. 我不确定我是否正确回答了您的问题。 As you have some grammatical issues. 由于您有一些语法问题。

  1. The onCreate statements are always executed. onCreate语句始终执行。 You can either have a Boolean in ParentActivity to stop the code from executing in ChildActivity#onCreate() . 您可以在ParentActivity使用Boolean来阻止代码在ChildActivity#onCreate()执行。
  2. You can try making your onCreate() code more modular by dividing it into functions so that it's not called. 您可以尝试通过将onCreate()代码划分为多个函数来使其不被调用,从而使它们更具模块化

Let me know what works for you. 让我知道什么对您有用。

最好的选择是在创建第二个活动之前在初始屏幕中使用finish(),

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM