Reputation: 48
I have an app in wich there are 4 screens, screens are created after clicking on button, each screen has a new activity, but activities are running too slow (3 seconds for creation). In second activity I have information which depends on first activity, so I can't create activity before the button clicks. Is there any other way to have 4 screens which are created one after another, and speed is fast?
package your.pack.namespace;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.Collection;
import java.util.LinkedList;
import java.util.concurrent.TimeUnit;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.Html;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
public class ParsActivity extends Activity implements OnClickListener {
private Collection<String> title = new LinkedList<String>();
public Collection<String> desc = new LinkedList<String>();
public NodeList Items = getNodeList();
Collection<String> secTitle = new LinkedList<String>();
Document doc;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
ll.setScrollContainer(true);
ll.setBackgroundResource(R.drawable.gradientbackground);
CreateScr(ll,Items);
}
private void CreateScr(LinearLayout l1,NodeList btns){
for(int i=0;i<btns.getLength();i++){
Element eElement= (Element)btns.item(i);
this.title.add(getTagValue("title",eElement));
this.desc.add(getTagValue("description",eElement));
}
btnCreate(title,l1);
this.setContentView(l1);
}
public void btnCreate (Collection<String> name,LinearLayout l1)
{
int k=0;
for(String i:name ){
Button btnNew = new Button(this);
btnNew.setId(k);
//btnNew.setBackgroundResource(R.drawable.bottomrow) ;
btnNew.setText(Html.fromHtml("<b>" + i + "</b>" + "<br />"));
l1.addView(btnNew);
k++;
btnNew.setOnClickListener(this);
}
this.setContentView(l1);
}
public static Document getDocument(String url)
throws MalformedURLException, IOException, Exception {
URL documentUrl = new URL(url);
URLConnection conn = documentUrl.openConnection();
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document;
InputStream stream = null;
try {
stream = conn.getInputStream();
document = builder.parse(stream);
} finally {
if (stream != null) stream.close();
}
return document;
}
public NodeList getNodeList (){
try {
doc = getDocument("http://afishadroid.about.od.ua/main.xml");
} catch (Exception ioe) {
//Обрабатываем ошибку
}
NodeList nd = doc.getElementsByTagName("item");
return nd;
}
public Element getElement (NodeList nl,int id1){
Element eElement = (Element)nl.item(id1);
return eElement;
}
protected static String getTagValue(String sTag, Element eElement) {
NodeList nlList = eElement.getElementsByTagName(sTag).item(0).getChildNodes();
Node nValue = (Node) nlList.item(0);
return nValue.getNodeValue();
}
public void onClick(View v) {
Intent intent = new Intent();
intent.setClass(this, SecondActivity.class);
// intent.putExtra("ID",v.getId());
startActivity(intent);
//Intent intent = new Intent(this, SecondActivity.class) .putExtra("ID", v.getId()); startActivity(intent);
}
}
Upvotes: 0
Views: 386
Reputation: 694
If you are loading the data locally(means local data) then it won't take much time but if you are displaying some data that will be fetched from server, try to do that in a separate thread so that your UI thread won't block and once fetching data is done,refresh your screen in UI thread.
Try to finish your onCreate() as soon as possible and do all the calculations and rest in your onResume() or onPause() method.
Upvotes: 0
Reputation: 5644
If you want to load your activity fast then you need to do as less as you can in the onCreate. Use AsyncTask to load data which can take a few seconds.
Upvotes: 1