Alex
Alex

Reputation: 1934

App activity delay's at opening for first time on navigation drawer

I'm facing a problem with an activity of my navigation drawer.When i open this activity for the first time it has a delay 1-2 seconds and when i open it again it's getting better(not perfect but better).Is it because it's importing an other API maybe?I've added an asynctask but still nothing.

Here is the code of this activity

public class ImportAPI extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener{
public TextView fullnameside, emailside;

public static String stravaToken;

public static ImageButton btnStrava;
public ImageView tickStrava;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.importapi);


    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayShowTitleEnabled(false);
    toolbar.setTitle("");
    toolbar.setSubtitle("");

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
            this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.addDrawerListener(toggle);
    toggle.syncState();

    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    View headerView = navigationView.getHeaderView(0);
    fullnameside = (TextView) headerView.findViewById(R.id.fullnameside);
    emailside = (TextView) headerView.findViewById(R.id.emailside);
    fullnameside.setText(""+GetInfo.fullname);
    emailside.setText(""+GetInfo.email);

    navigationView.setNavigationItemSelectedListener(this);
    navigationView.getMenu().getItem(3).setChecked(true);

    //STRAVA
    tickStrava=(ImageView) findViewById(R.id.tickStrava);
    btnStrava=(ImageButton) findViewById(R.id.stravaBtn);
    new connectStrava().execute("start");

}

@SuppressLint("StaticFieldLeak")
class connectStrava extends AsyncTask<String, String, String> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        btnStrava.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent getStravaApi= new Intent(ImportAPI.this,StravaSetupApi.class);
                startActivity(getStravaApi);
            }
        });

        //GET ACCESS TOKEN FROM STRAVAS AUTHORIZE ACCOUNT
        String accessToken = StravaAuthenticateActivity.getStravaAccessToken(ImportAPI.this);
        stravaToken=accessToken; //make static var so i can use it anywhere i want

        //Get athletes activities from GetStravaAthleteActivities.java
        new GetStravaAthleteActivities.AthleteActivities();

        //check if token is null so i can display the tick and also disable the button press
        if(stravaToken!=null)
        {
            btnStrava.setEnabled(false);
            tickStrava.setVisibility(View.VISIBLE);
        }
        else {
            tickStrava.setVisibility(View.INVISIBLE);
        }
    }

    @Override
    protected String doInBackground(String... aurl) {
        Log.d("Alekos","TAK4");
        return null;

    }
    protected void onProgressUpdate(String... progress) {

        Log.d("ANDRO_ASYNC",progress[0]);

    }

    @Override
    protected void onPostExecute(String unused) {
    }

}

Upvotes: 2

Views: 216

Answers (2)

Vir Rajpurohit
Vir Rajpurohit

Reputation: 1937

FYI

btnStrava.setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View view) {
                 Intent getStravaApi= new Intent(ImportAPI.this,StravaSetupApi.class);
                 startActivity(getStravaApi);
             }
         });

This code is not required to be kept inside onPreExecute() keep it inside of onCreate()

You are calling API in the start which is actually not delay but the time it is taking for the API calling.

Use ProgressBar untill your AsyncTask gets completed.

Upvotes: 2

Manish
Manish

Reputation: 1091

I think you are doing some blocking task in onPreexecute() of your AsyncTask. pls modify your AsyncTask as below.

class connectStrava extends AsyncTask<String, String, String> {

@Override
protected void onPreExecute() {
    super.onPreExecute();
    btnStrava.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent getStravaApi= new Intent(ImportAPI.this,StravaSetupApi.class);
            startActivity(getStravaApi);
        }
    });




}

@Override
protected String doInBackground(String... aurl) {
    //GET ACCESS TOKEN FROM STRAVAS AUTHORIZE ACCOUNT
    String accessToken = StravaAuthenticateActivity.getStravaAccessToken(ImportAPI.this);
    stravaToken=accessToken; //make static var so i can use it anywhere i want

    //Get athletes activities from GetStravaAthleteActivities.java
    new GetStravaAthleteActivities.AthleteActivities();
    return stravaToken;

}
protected void onProgressUpdate(String... progress) {

    Log.d("ANDRO_ASYNC",progress[0]);

}

@Override
protected void onPostExecute(String result) {
    //check if token is null so i can display the tick and also disable the button press
    if(stravaToken!=null)
    {
        btnStrava.setEnabled(false);
        tickStrava.setVisibility(View.VISIBLE);
    }
    else {
        tickStrava.setVisibility(View.INVISIBLE);
    }
}

}

Upvotes: 3

Related Questions