我的应用程序以检查登录状态的InitActivity
开始,在进行某些逻辑之前相应地调用LoginActivity
。
我知道我可以使用startAcivityForResult()
和onActivityResult()
来确保LoginActivity
在doSomeMainLogic()
之前完成,但我的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()
?
非常感谢。
答案 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();
}
}
}
}