活动花了太多时间打开

时间:2018-03-31 17:33:46

标签: android

我正在使用java开发一个Android应用程序。其中我遇到一个问题,我的一些活动花了太多时间加载大约3到4秒后点击。 我想在这里提一件事,以便清楚地解释问题并获得确切的解决方案,这是我完成当前活动后转到下一个。这是问题吗?有些活动运作良好,没有任何延误。

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();
 }
}

1 个答案:

答案 0 :(得分:0)

您可能在主线程本身上进行了大量计算,这会降低您的应用用户界面的速度。分享您的任何一项活动的代码。

您还应该查看此链接,他们已经解释了如何让您的Android应用程序更快,您可以从此链接中找到很多提示。

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