In my application, I made a change of fragments using ViewPager , but for some reason it slows down a lot on the emulator and on the phone (not because of weak hardware). What could be the problem? I attach the code below:

MainActivity

 public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //Context context = getApplicationContext(); //context.deleteDatabase("schoolsDB.db"); Intent intent = new Intent(MainActivity.this, WelcomeActivity.class); startActivity(intent); } } 

WelcomeActivity

 public class WelcomeActivity extends AppCompatActivity { private ViewPager viewPager; private ViewPagerAdapter viewPagerAdapter; private FragmentManager fragmentManager; private int scrollLimit; private Button nextPageBtn; private Button prevPageBtn; private void init(){ fragmentManager=getSupportFragmentManager(); viewPagerAdapter=new ViewPagerAdapter(fragmentManager); viewPager=findViewById(R.id.welcome_viewpager); nextPageBtn =findViewById(R.id.welcome_next_btn); prevPageBtn =findViewById(R.id.welcome_prev_btn); } private void setupViewPager(){ viewPagerAdapter.addFragment(new WelcomeFragmentOne()); viewPagerAdapter.addFragment(new WelcomeFragmentTwo()); viewPager.setAdapter(viewPagerAdapter); } void nextPage(int currentPage){ if(currentPage<scrollLimit) { viewPager.setCurrentItem(currentPage + 1); } } void prevPage(int currentPage){ if(currentPage>0){ viewPager.setCurrentItem(currentPage-1); } } @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_welcome); init(); setupViewPager(); prevPageBtn.setEnabled(false); scrollLimit = viewPager.getAdapter().getCount(); viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() { @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {} @Override public void onPageSelected(int position) { if(position+1== scrollLimit){ nextPageBtn.setEnabled(false); }else{ nextPageBtn.setEnabled(true); } if(position==0){ prevPageBtn.setEnabled(false); }else{ prevPageBtn.setEnabled(true); } } @Override public void onPageScrollStateChanged(int state) {} }); Button.OnClickListener nextPageListener = new Button.OnClickListener() { @Override public void onClick(View v) { nextPage(viewPager.getCurrentItem()); } }; Button.OnClickListener prevPageListener = new Button.OnClickListener() { @Override public void onClick(View v) { prevPage(viewPager.getCurrentItem()); } }; nextPageBtn.setOnClickListener(nextPageListener); prevPageBtn.setOnClickListener(prevPageListener); } } 

ViewPagerAdapter

 public class ViewPagerAdapter extends FragmentStatePagerAdapter { private final List<Fragment> fragments = new ArrayList<>(); public ViewPagerAdapter(FragmentManager fm) { super(fm); } @Override public Fragment getItem(int position) { return fragments.get(position); } @Override public int getCount() { return fragments.size(); } public void addFragment(Fragment fragment){ fragments.add(fragment); } } 

UPD : Added fragment code and markup.

WelcomeFragmentOne

 public class WelcomeFragmentOne extends Fragment { public WelcomeFragmentOne(){ } @Override public void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); } @Nullable @Override public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_wlcm1greeting, container, false); Log.d("Fragment One:", "Created!"); return view; } } 

WelcomFragmentOne_(разметка)

 <?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/firstColor" tools:layout_editor_absoluteY="81dp"> <TextView android:id="@+id/textview" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginTop="70dp" android:text="@string/hello_blank_fragment" android:textAlignment="center" android:textColor="@color/white" android:textSize="30sp" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <ImageView android:id="@+id/imageView" android:layout_width="220dp" android:layout_height="235dp" android:src="@drawable/school" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> </android.support.constraint.ConstraintLayout> 

WelcomeFragmentTwo

 public class WelcomeFragmentTwo extends Fragment { private EditText login; private EditText password; private Button enterBtn; private Button registerBtn; public WelcomeFragmentTwo(){ } @Override public void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); } @Nullable @Override public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_wlcm2login, container, false); login=view.findViewById(R.id.wlcm2login_edit_text_login); password=view.findViewById(R.id.wlcm2login_edit_text_password); enterBtn=view.findViewById(R.id.wlcm2login_button_confirm); registerBtn=view.findViewById(R.id.wlcm2login_button_registration); enterBtn.setEnabled(false); TextWatcher textWatcher = new TextWatcher() { @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { } @Override public void onTextChanged(CharSequence s, int start, int before, int count) { } @Override public void afterTextChanged(Editable s) { if(login.getText().length()!=0 && password.getText().length()!=0){ enterBtn.setEnabled(true); }else{ enterBtn.setEnabled(false); } } }; login.addTextChangedListener(textWatcher); password.addTextChangedListener(textWatcher); registerBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(getActivity().getApplicationContext(), RegistrationActivity.class); startActivity(intent); } }); return view; } } 

WelcomeFrafgmentTwo_(разметка)

 <?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/firstColor" tools:layout_editor_absoluteY="81dp"> <TextView android:id="@+id/textview" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginTop="24dp" android:paddingLeft="28dp" android:text="@string/log_in_invite" android:textColor="@color/white" android:textSize="30sp" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <EditText android:id="@+id/wlcm2login_edit_text_login" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginEnd="59dp" android:layout_marginStart="59dp" android:layout_marginTop="19dp" android:ems="10" android:hint="Логин" android:inputType="textPersonName" android:textColor="@color/white" android:textColorHint="@color/white" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/textview" /> <EditText android:id="@+id/wlcm2login_edit_text_password" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginEnd="59dp" android:layout_marginStart="59dp" android:layout_marginTop="27dp" android:ems="10" android:hint="Пароль" android:inputType="textPassword" android:textColor="@color/white" android:textColorHint="@color/white" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/wlcm2login_edit_text_login" /> <Button android:id="@+id/wlcm2login_button_confirm" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginEnd="59dp" android:layout_marginStart="59dp" android:layout_marginTop="26dp" android:text="Войти" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/wlcm2login_edit_text_password" /> <Button android:id="@+id/wlcm2login_button_registration" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginEnd="59dp" android:layout_marginStart="59dp" android:layout_marginTop="26dp" android:text="Зарегистрироваться" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/wlcm2login_button_confirm" /> </android.support.constraint.ConstraintLayout> 
  • 2
    In the given code there are no problems. They are in fragments. You, apparently, have a complicated interface there and it takes a long time to draw. Show the code and markup fragments. - Yuriy SPb
  • Added fragment code and markup. - mixer-09
  • 3
    Thanks for the tip, I solved the problem! It turned out that the image in the 1st fragment was too large, and reducing its resolution, the quality remained the same, and the brakes were gone. Back home - I will make a full answer. - mixer-09

1 answer 1

The problem was that the image resolution was too large. I decided to reduce the image in the fragment code using the Picasso library (although it is possible without it, you just need to set a smaller resolution).

 public class WelcomeFragmentOne extends Fragment { private ImageView imgView; public WelcomeFragmentOne(){ } @Override public void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); } @Nullable @Override public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_wlcm1greeting, container, false); imgView=view.findViewById(R.id.imageView); //Решение Picasso.get().load(R.drawable.school).resize(640, 640).into(imgView); return view; } }