android: pausing an activity until another finishes
-
26-09-2019 - |
Question
When my app starts, it checks to see if it has stored 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?
Solution
Your "promptForLoginInfo()" method should be calling startActivityForResult. Your "displayCredentials()" method should not be called in the onCreate() method, but in the onActivityResult method.
OTHER TIPS
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().