Issue
I have activity with a button.When button is clicked,a new activity starts and in this activity , various data fetch from web.But when I click the button , first activity holds on for a while and after 3-4 seconds second activity starts with ready datas.Apparently,before second activity is visible,app tries to fetch data.But firstly,I want second activity is visible to user, after that fetching data must start. I examined activity lifecycle and replaced fetching to onStart() but not working
@Override
protected void onStart() {
super.onStart();
//Fetch data
}
How can I do that ?
Here is my all code (just second activity) :
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);
}
}
}
With above code , when I click button , first activity holds on for a while and second activity is visible when fetching data finished.Progress dialog is visible for just a few milisecond
Solution
Sounds as if you are running all your code in the UI thread. Would be nice with more code to be able to give a better example.
In your second activity:
private class BackgroundTask extends AsyncTask<Void, Void, Void> {
// Before running code in separate thread
@Override
protected void onPreExecute() {
// setup loading dialog
}
@Override
protected Void doInBackground(Void... arg0) {
// do long running code
return null;
}
// after executing the code in the thread
@Override
protected void onPostExecute(Void arg) {
// dismiss your dialog
}
}
And inside onCreate:
new BackgroundTask().execute();
Edit:
Now that I have seen your code I would suggest you try something like this:
private void setUpMap(){
map.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(39.910526,32.804435),15));
map.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null);
new Handler().postDelayed(new Runnable() {
public void run() {
try {
String result = new MyNewTask().execute();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ExecutionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
/*
* you should move this call to onPostExecute
* then you will not need to call execute().get()
* which takes away the advantage of AsyncTask
* as it then is run on UI thread
*/
overlay.setOverlay(map);
}
}, 2000);
}
Setting up the map is an UI heavy task, so in my experience it looks nicer to wait a bit before loading markers etc. Furthermore this should enable the dialog to properly show before it is dismissed.
Answered By - cYrixmorten
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.