Starting intent after Async task gets over

后端 未结 3 1226
隐瞒了意图╮
隐瞒了意图╮ 2021-01-26 20:17

I created a login activity for my Android app. After the user enters the correct credentials, the login activity will switch over to the homepage but I don\'t know why my code w

相关标签:
3条回答
  • 2021-01-26 20:53

    Add a checker to your AsyncTask such as

    // Background ASYNC Task to login by making HTTP Request 
    class LoginEmployer extends AsyncTask<String, String, String> { 
    
    boolean validUser = false;
    

    Then once the user is validated inside your background task set the value to true

     if (Integer.parseInt(res) == 1) { 
          // user successfully logged in 
          // Store user details in SQLite Database 
    
         validUser = true;  //set valid to true
    

    Now in postExecute check if the user is valid

    protected void onPostExecute(String file_url) { 
        // dismiss the dialog once done 
        pDialog.dismiss(); 
        if ( validUser )
        {
            Intent homepage = new Intent( LoginEmployerActivity.this, 
            HomepageEmployerActivity.class); 
    
            // Close all views before launching Employer 
            // homePage 
             homepage.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
             startActivity(homepage); 
        }
    
    0 讨论(0)
  • 2021-01-26 20:56

    I don't think you have added the Intent code here that will help you switch to another Activity.

    protected void onPostExecute(String file_url) { 
    
        // dismiss the dialog once done             // Intent Code Missing.
    
        pDialog.dismiss(); 
    

    You should do a UI work in UI thread and Non-UI work in Non-UI thread, thats a rule from the arrival of HoneyComb version of android.

    You have added the below code in doInBackground(), That should be in onPostExcute()

    Intent homepage = new Intent( getApplicationContext(), HomepageEmployerActivity.class); 
    homepage.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
    startActivity(homepage);
    
    0 讨论(0)
  • 2021-01-26 21:11

    You are calling the intent to start a new activity inside the doInBackgorund() which runs on a non-UI thread and the Activity needs to be run on a UI thread. That is why your Login activity is never stopped.

    Put the code to go to the new activity inside onPostExecute() or onProgressUpdate().

    Here is something you can do. Declare a global variable loginVerfied = false;

    When your doInBackground verifies that the authenticity of the user, make loginVerified = true , otherwise keep it false.

    Then inside onPostExecute()

    if(loginVerifed == true)
    {
           Intent homepage = new Intent(getApplicationContext(),HomepageEmployerActivity.class                               
           homepage.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
           startActivity(homepage);
           finish();
    }
    

    EDIT :

    Also, you have declared class LoginEmployer extends AsyncTask<String, String, String>, so to call it you need to use new LoginEmployer.execute(""); (you are missing the double quotes and not passing any String to the Task so it does not match it's parameters). The first parameter in the definition of the AsyncTask is the datatype of the value being passed to it when execute() function is called. The second parameter is the datatype related to displaying progress during the time when the background thread runs. And the third parameter specifies the return value of the result.

    More about AsyncTask here.

    So, here is what you need to do now.

    Declare the Async Task like this.

    class LoginEmployer extends AsyncTask<String, Void, String> and make a call to it by using new LoginEmployer.execute(""). Make sure to return null from your doInBackground().

    Hope this solves your problem now!

    0 讨论(0)
提交回复
热议问题