0

I am developing an android application using java. In which i am facing a problem that some of my activities are taking too much time to load approximately 3 to 4 second after click. There is one thing that i would like to mention here in order to explain the problem clearly and to get the exact solution as well, which is I finish the current activity after going to the next one. Is that the issue? Some of the activities are working well without any delay.

package com.lkintechnology.mBilling.activities.company.transaction.sale;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.support.annotation.Nullable;
import android.support.design.widget.CoordinatorLayout;
import android.support.design.widget.Snackbar;
import android.support.design.widget.TabLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.os.Bundle;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.TextView;
import android.widget.Toast;
import com.lkintechnology.mBilling.R;
import com.lkintechnology.mBilling.utils.TypefaceCache;
import java.util.ArrayList;
import java.util.List;
import butterknife.Bind;
import butterknife.ButterKnife;

public class CreateSaleActivity extends AppCompatActivity {

 @Bind(R.id.coordinatorLayout)
 CoordinatorLayout coordinatorLayout;
 // public static CompanyData data;
 @Bind(R.id.viewpager)
 ViewPager mHeaderViewPager;
 @Bind(R.id.tabs)
 TabLayout mTabLayout;
 ProgressDialog mProgressDialog;
 Snackbar snackbar;
 AppUser appUser;
 String title;
 public static boolean fromsalelist;
 public static boolean fromdashboard;
 public static boolean isForEdit;

 @Override
 protected void onCreate(@Nullable Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_create_sale);
  ButterKnife.bind(this);
  fromdashboard = getIntent().getExtras().getBoolean("fromdashboard");
  fromsalelist = getIntent().getExtras().getBoolean("fromsalelist");
  appUser = LocalRepositories.getAppUser(this);
  if (fromsalelist) {
   if (fromdashboard) {
    title = "CREATE SALE VOUCHER";
   } else {
    title = "EDIT SALE VOUCHER";
   }
  } else {
   title = "CREATE SALE VOUCHER";
  }
  if (isForEdit) {
   title = "EDIT SALE VOUCHER";
  }
   initActionbar();
   setupViewPager(mHeaderViewPager);
   mTabLayout.setupWithViewPager(mHeaderViewPager);
   Intent intent = getIntent();
   boolean b = intent.getBooleanExtra("is", false);
  if (b) {
   mHeaderViewPager.setCurrentItem(1, true);
  }
  android.support.v7.app.ActionBar actionBar = getSupportActionBar();
  actionBar.setLogo(R.drawable.list_button);
  actionBar.setDisplayUseLogoEnabled(true);
  actionBar.setDefaultDisplayHomeAsUpEnabled(true);
 }

 private void setupViewPager(ViewPager viewPager) {
  CreateSaleActivity.ViewPagerAdapter adapter = new 
  CreateSaleActivity.ViewPagerAdapter(getSupportFragmentManager());
  adapter.addFragment(new CreateSaleVoucherFragment(), "CREATE SALE VOUCHER");
  adapter.addFragment(new AddItemVoucherFragment(), "ADD ITEM VOUCHER");
  viewPager.setAdapter(adapter);
 }

 private void initActionbar() {
  ActionBar actionBar = getSupportActionBar();
  View viewActionBar = 
getLayoutInflater().inflate(R.layout.action_bar_tittle_text_layout, null);
  ActionBar.LayoutParams params = new ActionBar.LayoutParams( //Center the 
textview in the ActionBar !
   ActionBar.LayoutParams.WRAP_CONTENT,
   ActionBar.LayoutParams.MATCH_PARENT,
   Gravity.CENTER);
  actionBar.setBackgroundDrawable(new 
ColorDrawable(Color.parseColor("#067bc9")));
  actionBar.setDisplayHomeAsUpEnabled(true);
  actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
  actionBar.setCustomView(viewActionBar, params);
  TextView actionbarTitle = (TextView) 
viewActionBar.findViewById(R.id.actionbar_textview);
  actionbarTitle.setText(title);
  actionbarTitle.setTextSize(16);
  actionbarTitle.setTypeface(TypefaceCache.get(getAssets(), 3));
  actionBar.setDisplayShowCustomEnabled(true);
  actionBar.setDisplayShowTitleEnabled(false);
  actionBar.setDisplayHomeAsUpEnabled(true);
  actionBar.setHomeButtonEnabled(true);
 }



 public static class PlaceholderFragment extends Fragment {

  public PlaceholderFragment() {}
  }

 public class ViewPagerAdapter extends FragmentPagerAdapter {
  private final List < Fragment > mFragmentList = new ArrayList < > ();
  private final List < String > mFragmentTitleList = new ArrayList < > ();

  public ViewPagerAdapter(FragmentManager manager) {
   super(manager);
  }

  @Override
  public Fragment getItem(int position) {
   return mFragmentList.get(position);
  }

  @Override
  public int getCount() {
   return mFragmentList.size();
  }

  public void addFragment(Fragment fragment, String title) {
   mFragmentList.add(fragment);
   mFragmentTitleList.add(title);
  }

  @Override
  public CharSequence getPageTitle(int position) {
   return mFragmentTitleList.get(position);
  }
  }

   @Override
  public boolean onCreateOptionsMenu(Menu menu) {
  MenuInflater menuInflater = getMenuInflater();
  menuInflater.inflate(R.menu.activity_list_button_action, menu);
  return super.onCreateOptionsMenu(menu);
 }


 @Override
 public boolean onOptionsItemSelected(MenuItem item) {
  switch (item.getItemId()) {
   case R.id.icon_id:
    Intent i = new Intent(getApplicationContext(), 
GetSaleVoucherListActivity.class);
    startActivity(i);
    finish();
    return true;
   case android.R.id.home:
    Intent intent = new Intent(this, TransactionDashboardActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK);
    startActivity(intent);
    finish();
    return true;
   default:
    return super.onOptionsItemSelected(item);
  }
 }

 @Override
 public void onBackPressed() {
  Intent intent = new Intent(this, TransactionDashboardActivity.class);
  intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | 
  Intent.FLAG_ACTIVITY_CLEAR_TASK);
  startActivity(intent);
  finish();
 }
}
  • you might be having some errors which could be visible on your log cat , so please provide the log cat so that errors are seen ? – Niamatullah Bakhshi Mar 31 '18 at 17:35
  • Okay but i am not in office right now so i ll put up the log tomorrow... thank for consider – Shadab Aazam Mar 31 '18 at 17:41
  • 3
    Are you doing any longstanding operations in onCreate/onStart/onResume? If so these would cause the activity launch delay you are seeing. These operations should be done off the main (UI) thread, for example in an AsyncTask or HandlerThread. Please provide more information so we can help you. – Tur1ng Mar 31 '18 at 17:42
  • Now i have posted the the complete code of an activity – Shadab Aazam Apr 02 '18 at 05:24
  • You are loading a custom font (!) and bunch of XML drawables to draw the action bar. That alone is enough to delay startup by a decent amount. To make matters worse, your Activity heavily relies on Support Library classes. All those support action bars, snackbars, view pagers and coordinator layouts won't magically load themselves, — every class needs to be loaded and initialized by VM. Simply replacing support action bar with framework one (and removing non-bitmap images from it) is often enough to cut startup costs by couple of seconds. – user1643723 Apr 02 '18 at 05:31
  • have you found any solutions? – Moustafa EL-Saghier Mar 07 '22 at 13:08
  • I hope this is your question [https://stackoverflow.com/a/72854808/12748481](https://stackoverflow.com/a/72854808/12748481) this worked fine for me – Surajkaran Meghwanshi Jul 04 '22 at 11:08

1 Answers1

0

You might be doing a lot of computations on the main thread itself which slows down your apps user interface. Share the code of any one of your activities.

You should also check this link out they have explained how to make your android app faster, you can find a lot of tips from this link.

https://medium.com/@jorgemf/making-your-android-app-faster-735328eaba25

  • No in that fragment i am doing nothing initially that may problem to load this fragment... actually there are some edit text with some text view.. intent and startactivityforresult is being used. but after loading this is working well... – Shadab Aazam Apr 03 '18 at 08:31