我有一個按鈕的活動。當按鈕被點擊時,一個新的活動開始,並在這個活動中,各種數據從web.But獲取當我點擊按鈕,第一個活動持有一個而在3-4秒後,第二個活動以就緒數據開始。顯然,在第二個活動可見之前,應用程序嘗試獲取數據。但是,首先,我希望第二個活動對用戶可見,在獲取數據之後必須啓動。 我檢查活動的生命週期和更換取到在onStart(),但不工作Android在活動後做某事
@Override
protected void onStart() {
super.onStart();
//Fetch data
}
我怎麼能這樣做?
這裏是我的代碼(只次活動):
public class GuzergahActivity extends android.support.v4.app.FragmentActivity{
private GoogleMap map;
private GuzergahOverlay overlay;
private WebView webview;
private ImageView imageview;
private ProgressDialog dialog;
private int flag;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
overridePendingTransition(0, 0);
setContentView(R.layout.guzergah);
flag=this.getIntent().getExtras().getInt("flag");
setUpMapIfNeeded();
setUpView();
}
@Override
protected void onResume() {
super.onResume();
setUpMapIfNeeded();
}
@Override
protected void onDestroy() {
if (dialog!=null) {
dialog.dismiss();
}
super.onDestroy();
}
private void setUpMapIfNeeded() {
// Do a null check to confirm that we have not already instantiated the map.
if (map == null) {
map = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.guzergah_fragment)).getMap();
if(map!=null){
setUpMap();
}
}
}
private void setUpMap(){
try {
String result=new MyNewTask().execute().get();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ExecutionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
overlay.setOverlay(map);
map.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(39.910526,32.804435),15));
map.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null);
}
private void setUpView() {
//Binding imageview,webview...and setting up their features
}
private class MyNewTask extends AsyncTask<String,Integer,String>
{
@Override
protected void onPreExecute()
{
dialog= new ProgressDialog(GuzergahActivity.this);
dialog.setIndeterminate(true);
dialog.setCancelable(false);
dialog.setMessage("Downloading! Please wait...!");
dialog.show();
}
@Override
protected String doInBackground(String... params)
{
/* Fetching data is in this class - GuzergahOverlay*/
overlay = new GuzergahOverlay(flag);
return "Done";
}
@Override
protected void onPostExecute(String result)
{
super.onPostExecute(result);
Log.i("result","" +result);
if(result!=null){
dialog.dismiss();
}
}
} //end MyNewTask
}
public class GuzergahOverlay {
private MarkerOptions beytepe_kampusu;
private MarkerOptions sihhiye_kampusu;
private ArrayList<PolylineOptions> lines;
private ArrayList<MarkerOptions> stops;
public GuzergahOverlay(int mode) {
beytepe_kampusu=new MarkerOptions().position(new LatLng(39.87159,32.735435)).title("Hacettepe Üniversitesi Beytepe Yerleşkesi").icon(BitmapDescriptorFactory.fromResource(R.drawable.marker_beytepe));
sihhiye_kampusu=new MarkerOptions().position(new LatLng(39.931599,32.862365)).title("Hacettepe Üniversitesi Sıhhıye Yerleşkesi").icon(BitmapDescriptorFactory.fromResource(R.drawable.marker_sihhiye));
getLinesAndStops(mode); //Here is fetching geopoint and preparing lines and stops arraylists
}
private void getLinesAndStops(int mode) {
// Fetching data from Parse.com
// Fillinp up lines and stops arraylists
}
public void setOverlay(GoogleMap map){
map.addMarker(beytepe_kampusu);
map.addMarker(sihhiye_kampusu);
for(PolylineOptions polyline : lines){
map.addPolyline(polyline);
}
for(MarkerOptions marker : stops){
map.addMarker(marker);
}
}
}
有了上面的代碼,當我點擊按鈕,第一個活動持有了一段時間,獲取數據finished.Progress對話框時,第二項活動是可見的僅在幾個毫秒內可見
如果我成功了,也許我的progressdialog在第二項活動中出現 –