我知道还有其他线索在讨论这个问题,但在我的案例中他们并没有帮助。我有一个按钮列表,每个按钮都会导致不同的mapActivity。目前,我已经受到Android Studio for LoginActivity的代码的启发,它还有一个ProgressBar。按钮列表消失,progressBar开始显示,但随后停止,因为HomeActivity.onPause已启动,myMap活动的onCreate也已启动,但应用程序冻结在阻止的progressBar上。大约一会儿,我的地图活动可见。如何在此过程中使进度条旋转?
package com.example.florian.myapplication.Activities;
import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.annotation.TargetApi;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.database.Cursor;
import android.os.AsyncTask;
import android.os.Build;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import com.example.florian.myapplication.Activities.MapsActivities.MainActivity;
import com.example.florian.myapplication.Activities.MapsActivities.MainActivityRec;
import com.example.florian.myapplication.Activities.MapsActivities.MainActivityRel;
import com.example.florian.myapplication.R;
public class HomeActivity extends AppCompatActivity {
private ChangeActivityTask changeTask;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
Button obsPonctButton = (Button) findViewById(R.id.obs_ponc_button);
obsPonctButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showProgress(true);
changeTask = new ChangeActivityTask(false);
changeTask.execute((Void) null);
}
});
Button protocoleButton = (Button) findViewById(R.id.protocole_button);
protocoleButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showProgress(true);
changeTask = new ChangeActivityTask(false);
changeTask.execute((Void) null);
}
});
Button releveBouton = (Button) findViewById(R.id.relevé_button);
releveBouton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showProgress(true);
changeTask = new ChangeActivityTask(false);
changeTask.execute((Void) null);
}
});
Button syncButton = (Button) findViewById(R.id.sync_button);
syncButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
synchroniserMission();
}
});
}
@Override
protected void onRestart() {
super.onRestart();
showProgress(false);
}
public Intent generateIntent (boolean isReleveActivity){
Intent intent;
if(isReleveActivity)
intent = new Intent(HomeActivity.this,MainActivityRel.class);
else
intent = new Intent(HomeActivity.this,MainActivityRec.class);
return intent;
}
/**
* Shows the progress UI and hides the login form.
*/
@TargetApi(Build.VERSION_CODES.HONEYCOMB_MR2)
private void showProgress(final boolean show) {
// On Honeycomb MR2 we have the ViewPropertyAnimator APIs, which allow
// for very easy animations. If available, use these APIs to fade-in
// the progress spinner.
final LinearLayout buttonList = (LinearLayout) findViewById(R.id.buttonList);
final ProgressBar mProgressView = (ProgressBar) findViewById(R.id.action_progress);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {
int shortAnimTime = getResources().getInteger(android.R.integer.config_shortAnimTime);
buttonList.setVisibility(show ? View.GONE : View.VISIBLE);
buttonList.animate().setDuration(shortAnimTime).alpha(
show ? 0 : 1).setListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
buttonList.setVisibility(show ? View.GONE : View.VISIBLE);
}
});
mProgressView.setVisibility(show ? View.VISIBLE : View.GONE);
mProgressView.animate().setDuration(shortAnimTime).alpha(
show ? 1 : 0).setListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
mProgressView.setVisibility(show ? View.VISIBLE : View.GONE);
}
});
} else {
// The ViewPropertyAnimator APIs are not available, so simply show
// and hide the relevant UI components.
mProgressView.setVisibility(show ? View.VISIBLE : View.GONE);
buttonList.setVisibility(show ? View.GONE : View.VISIBLE);
}
}
@Override
protected void onPause() {
super.onPause();
Log.w("Bienvenue", "dans le onpause");
}
private void synchroniserMission(){
}
public class ChangeActivityTask extends AsyncTask<Void, Void, Boolean> {
private final boolean isReleveActivity;
private Intent intent;
ChangeActivityTask(boolean isReleveActivity) {
this.isReleveActivity = isReleveActivity;
}
@Override
protected Boolean doInBackground(Void... params) {
// TODO: attempt authentication against a network service.
intent = generateIntent(isReleveActivity);
startActivity(intent);
return true;
}
@Override
protected void onPostExecute(final Boolean success) {
showProgress(false);
}
@Override
protected void onCancelled() {
showProgress(false);
}
}
}
我希望我能清楚,你可以帮助我扔掉它。