简体   繁体   English

Android从父活动中停止子活动

[英]Android stop Child activity from parent Activity

I have a base activity that does some authentication checks like follows: 我有一个基本活动,它会进行一些身份验证检查,如下所示:

public class BaseActivity extends AppCompatActivity {

    protected MyApp application;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        application = (MyApp) getApplication();

        if (application.getAuth() == null || !application.getAuth().isLoggedIn()) {

            startActivity(new Intent(this, LoginActivity.class));
            finish();
            return;
        }

    }        
}

When the above BaseActivity finds that Auth is null, i redirect the user to the login Activity and call finish and return. 当上述BaseActivity发现Auth为空时,我将用户重定向到登录Activity并调用finish并返回。 The problem is that the activity that is inherits from it still runs it's onCreate code. 问题是从其继承的活动仍在运行onCreate代码。

How can i stop the Activity inheriting from the BaseActivity to run at all? 我如何才能停止从BaseActivity继承的Activity来运行?

You can keep one member variable in your base activity. 您可以在基本活动中保留一个成员变量。 Something like: protected boolean authFailed = false . 类似于: protected boolean authFailed = false Before returning from base activity, you can make authFailed = true . 从基本活动返回之前,可以使authFailed = true In your child class, after caliing super.onCreate, you can check the value of this protected variable. 在子类中,校准了super.onCreate之后,可以检查此受保护变量的值。 And can return from child activity if its true . 并且可以从儿童活动中返回,如果它是true

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

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