How to speed up the loading of an Android application?

I developed an android application in which I show a splash screen before launching the main action, but to run on younger devices the application takes 5-7 seconds. I want to reduce time to a minimum. I am trying to reduce everything that needs to be done in onCreate() , but now I can no longer remove any of this. I am inserting the code that I used to show the splash and code from MainActivity. Please help me reduce the launch time of the application.

Splash.java

 @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); this.requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_splash); txtLoad = (TextView) findViewById(R.id.txtLoading); txtLoad.setText("v1.0"); new Thread() { public void run() { try { sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } finally { finish(); Intent intent = new Intent(SplashActivity.this,MainActivity.class); startActivity(intent); } } }.start(); } 

MainActivity.java

 @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); this.requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_main); editType1UserName = (EditText) findViewById(R.id.editTextType1UserName); editType1Password = (EditText) findViewById(R.id.editTextType1Password); editType2UserName = (EditText) findViewById(R.id.editTextType2UserName); editType2Password = (EditText) findViewById(R.id.editTextType2Password); editType3UserName = (EditText) findViewById(R.id.editTextType3UserName); editType3Password = (EditText) findViewById(R.id.editTextType3Password); editType4UserName = (EditText) findViewById(R.id.editTextType4UserName); editType4Password = (EditText) findViewById(R.id.editTextType4Password); mTxtPhoneNo = (AutoCompleteTextView) findViewById(R.id.mmWhoNo); mTxtPhoneNo.setThreshold(1); editText = (EditText) findViewById(R.id.editTextMessage); spinner1 = (Spinner) findViewById(R.id.spinnerGateway); btnsend = (Button) findViewById(R.id.btnSend); btnContact = (Button) findViewById(R.id.btnContact); btnsend.setOnClickListener((OnClickListener) this); btnContact.setOnClickListener((OnClickListener) this); mPeopleList = new ArrayList<Map<String, String>>(); PopulatePeopleList(); mAdapter = new SimpleAdapter(this, mPeopleList, R.layout.custcontview, new String[] { "Name", "Phone", "Type" }, new int[] { R.id.ccontName, R.id.ccontNo, R.id.ccontType }); mTxtPhoneNo.setAdapter(mAdapter); mTxtPhoneNo.setOnItemClickListener((OnItemClickListener) this); readPerson(); Panel panel; topPanel = panel = (Panel) findViewById(R.id.mytopPanel); panel.setOnPanelListener((OnPanelListener) this); panel.setInterpolator(new BounceInterpolator(Type.OUT)); getLoginDetails(); } 
0
source share
1 answer

The reason for this slowdown is that you are most likely requesting from the contact provider on the phone, extracting some data from these requests, placing them in mPeopleList , and then installing it on your SimpleAdapter . So your onCreate activity onCreate waits until PopulatePeopleList() finishes its work. I don’t know how you request this contact provider, but see if you can adapt your code to use CursorLoader (an older version of Android is available through the compatibility package). This will mean that you have to switch to a Cursor based adapter, other changes are possible depending on your code.

If you still want to use a non-based SimpleAdapter , you need to override it to implement your own AsyncTaskLoader (again available in the old version of Android via the compatibility package):

 public class ContactsDataLoader extends AsyncTaskLoader<ArrayList<Map<String, String>>> { public ContactsDataLoader(Context context) { super(context); } @Override public ArrayList<Map<String, String>> loadInBackground() { // here do what you do in the PopulatePeopleList() method // this will be done in another thread so the activity will initially // start empty(set an empty mPeoples list to the SimpleAdapter) and as // this loader finishes its job you'll have the list filled with the // data that is returned here return data; } @Override protected void onStartLoading() { super.onStartLoading(); forceLoad(); } } 

Then you will have activity where you need this data, implement LoaderManager.LoaderCallbacks<ArrayList<Map<String, String>>> :

 public class MainActivity implements LoaderManager.LoaderCallbacks<ArrayList<Map<String, String>>> 

who needs these methods:

 @Override public Loader<ArrayList<Map<String, String>>> onCreateLoader(int id, Bundle args) { return new ContactsDataLoader(context); } @Override public void onLoadFinished(Loader<ArrayList<Map<String, String>>> loader, ArrayList<Map<String, String>> data) { // your custom adapter will need a method to update its data adapter.changeData(data); // you always have the option of using a normal SimpleAdapter and create // a new instance each time the data changes // mPeopleList = data; // mAdapter = new SimpleAdapter(this, mPeopleList, // R.layout.custcontview, // new String[] { "Name", "Phone", "Type" }, new int[] { // R.id.ccontName, R.id.ccontNo, R.id.ccontType }); // mTxtPhoneNo.setAdapter(mAdapter); } @Override public void onLoaderReset(Loader<ArrayList<Map<String, String>>> loader) { // your custom adapter will need a method to update its data adapter.changeData(null); // or an empty list of data // you always have the option of using a normal SimpleAdapter and create // a new instance each time the data changes // mPeopleList = new ArrayList<Map<String, String>>; // mAdapter = new SimpleAdapter(this, mPeopleList, // R.layout.custcontview, // new String[] { "Name", "Phone", "Type" }, new int[] { // R.id.ccontName, R.id.ccontNo, R.id.ccontType }); // mTxtPhoneNo.setAdapter(mAdapter); } 

Then you only need to call:

 // mPeopleList will have to be initialized to an empty list in the `onCreate` method getLoaderManager().initLoader(0, null, this); 

in your onCreate method. The application starts up pretty quickly, but at first it will be empty until the bootloader deals with it and receives data from the contacts and installs it on your adapter.

+2
source

Source: https://habr.com/ru/post/1438561/


All Articles