开发者

android: pausing an activity until another finishes

开发者 https://www.devze.com 2022-12-29 21:35 出处:网络
When my app starts, it checks to see if it has s开发者_如何学Ctored login credentials. if it doesn\'t, it starts another activity to prompt the user for those credentials. My problem is, that when the

When my app starts, it checks to see if it has s开发者_如何学Ctored login credentials. if it doesn't, it starts another activity to prompt the user for those credentials. My problem is, that when the prompt activity is started, the first activity continues execution and ends up with null pointers because the prompt activity has not yet returned the needed data

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    tv = new TextView(this);
    setContentView(tv);

    promptForLoginInfo(); //method creates intent and starts activity

    displayCredentials(); //prints data to screen
}

the output reads: "null" because the program executes "displayCredentials()" before the login prompt activity returns.

Anyone have a clue what to do?


Your "promptForLoginInfo()" method should be calling startActivityForResult. Your "displayCredentials()" method should not be called in the onCreate() method, but in the onActivityResult method.


In promptForLoginInfo(); you need to start activityForResult. then you need move displayCredentials(); from onCreate to onActivityResult


Did you tried to check for stored credentials before calling displayCredentials()? If credentials are not found you can start activity by startActivityForResult() method and call displayCredentials() after your prompt activity finishes in onActivityResult().

0

精彩评论

暂无评论...
验证码 换一张
取 消