我需要等到活动完成才能继续,但有时候只是

时间:2013-04-23 20:13:33

标签: android android-intent android-activity

我的应用程序以检查登录状态的InitActivity开始,在进行某些逻辑之前相应地调用LoginActivity

我知道我可以使用startAcivityForResult()onActivityResult()来确保LoginActivitydoSomeMainLogic()之前完成,但我的if检查会在其中引发曲线。如果我这样做:

public class InitActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (!userIsLoggedIn) {
            Intent intent = new Intent("com.example.myapp.LOGINACTIVITY");
            startActivityForResult(intent,1);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == 1) {
            if (resultCode == RESULT_OK) {
                doSomeMainLogic();
            }
        }
    }

}

如果用户已登录,如何触发doSomeMainLogic()

非常感谢。

1 个答案:

答案 0 :(得分:1)

public class InitActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (!userIsLoggedIn) {
            Intent intent = new Intent("com.example.myapp.LOGINACTIVITY");
            startActivityForResult(intent,1);
        }
        else {
            doSomeMainLogic();  // this part is added
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == 1) {
            if (resultCode == RESULT_OK) {
                doSomeMainLogic();
            }
        }
    }

}