应用 activity 首次在导航抽屉中打开时出现延迟
App activity delay's at opening for first time on navigation drawer
我的导航 activity 遇到问题 drawer.When 我打开这个 activity
第一次
它有 1-2 秒的延迟,当我再次打开它时它变得更好(不是完美但更好)。是因为它正在导入另一个 API 吗?我添加了一个 asynctask 但仍然没有。
这是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) {
}
}
我认为您正在 AsyncTask 的 onPreexecute() 中执行一些阻塞任务。
请按如下方式修改您的 AsyncTask。
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);
}
}
}
FYI
btnStrava.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent getStravaApi= new Intent(ImportAPI.this,StravaSetupApi.class);
startActivity(getStravaApi);
}
});
此代码不需要保存在 onPreExecute()
保存在 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.
我的导航 activity 遇到问题 drawer.When 我打开这个 activity 第一次 它有 1-2 秒的延迟,当我再次打开它时它变得更好(不是完美但更好)。是因为它正在导入另一个 API 吗?我添加了一个 asynctask 但仍然没有。
这是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) {
}
}
我认为您正在 AsyncTask 的 onPreexecute() 中执行一些阻塞任务。 请按如下方式修改您的 AsyncTask。
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);
}
}
}
FYI
btnStrava.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent getStravaApi= new Intent(ImportAPI.this,StravaSetupApi.class);
startActivity(getStravaApi);
}
});
此代码不需要保存在 onPreExecute()
保存在 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.