1

In my android app I have an image slider using viewpager,which changes images every 2.5 seconds in the main activity,it works fine when I open the app,but the problem is when I jump to another Activity from the MainActivity and come back it,starts to move the images in the slides very fast,as much as I jump to another activity the sliding become more faster.please help.

This is where I have included my 3 slider images slidelist.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <ImageView
        android:id="@+id/image1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:adjustViewBounds="true"
        android:layout_gravity="center"
        android:src="@drawable/slidetwo"
        android:scaleType="centerCrop"/>

    <ImageView
        android:id="@+id/image2"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
      android:adjustViewBounds="true"
        android:layout_gravity="center"
        android:src="@drawable/slideone"
        android:scaleType="centerCrop"/>


    <ImageView
        android:id="@+id/image3"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:adjustViewBounds="true"
        android:layout_gravity="center"
        android:src="@drawable/slidethree"
        android:scaleType="centerCrop"/>
    
</FrameLayout>

This the slider layout in content main.xml(included in activity)

 <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="250dp"
        android:layout_below="@+id/linearone">
        <android.support.v4.view.ViewPager
            android:id="@+id/pager"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_alignParentTop="true" />
        <me.relex.circleindicator.CircleIndicator
            android:id="@+id/indicator"
            android:layout_width="match_parent"
            android:layout_height="48dp"
            android:layout_alignParentBottom="true"/>
    </RelativeLayout>

The following is the adpater class

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.example.rimapps.icar_iisr_turmeric.R;

import java.util.ArrayList;


public class SlideAdapter extends PagerAdapter {
    private ArrayList<Integer> images;
    private LayoutInflater inflater;
    private Context context;

    public SlideAdapter(Context context, ArrayList<Integer> images) {
        this.context = context;
        this.images=images;
        inflater = LayoutInflater.from(context);
    }
    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
    }
    @Override
    public int getCount() {
        return images.size();
    }
    @Override
    public Object instantiateItem(ViewGroup view, int position) {
        View myImageLayout = inflater.inflate(R.layout.slidelist, view, false);
        ImageView myImage1 = (ImageView) myImageLayout.findViewById(R.id.image1);
        ImageView myImage2 = (ImageView) myImageLayout.findViewById(R.id.image2);
        ImageView myImage3 = (ImageView) myImageLayout.findViewById(R.id.image3);

        myImage1.setImageResource(images.get(position));
        myImage2.setImageResource(images.get(position));
        myImage3.setImageResource(images.get(position));

        view.addView(myImageLayout, 0);
        return myImageLayout;
    }
    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view.equals(object);
    }
}

This is my MainActivity class and here I have defined the timer for the slider

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.os.Handler;
import android.support.design.internal.NavigationMenuItemView;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;

import com.example.rimapps.icar_iisr_turmeric.R;
import com.example.rimapps.icar_iisr_turmeric.control.ButtonAdapter;
import com.example.rimapps.icar_iisr_turmeric.control.SlideAdapter;
import com.example.rimapps.icar_iisr_turmeric.model.ContentsDep;
import com.example.rimapps.icar_iisr_turmeric.utils.LocaleHelper;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import me.relex.circleindicator.CircleIndicator;

public class MainActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {


    CircleIndicator indicator;
    int period=2500, delay = 2500;
    
    private static ViewPager mPager;
    private static int currentPage = 0;
    private static final Integer[] slide = {R.drawable.slideone, R.drawable.slidetwo, R.drawable.slidethree};
    private ArrayList<Integer> slidearray = new ArrayList<Integer>();
    Timer swipeTimer;
    
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);

init();

}

//Image slider function//

    private void init() {
        for (int i = 0; i < slide.length; i++)
            slidearray.add(slide[i]);
        mPager = (ViewPager) findViewById(R.id.pager);
        mPager.setAdapter(new SlideAdapter(MainActivity.this, slidearray));
        CircleIndicator indicator = (CircleIndicator) findViewById(R.id.indicator);
        indicator.setViewPager(mPager);


        // Auto start of viewpager
        final Handler handler = new Handler();
        final Runnable Update = new Runnable() {
            public void run() {
                if (currentPage == slide.length) {
                    currentPage = 0;
                }
                mPager.setCurrentItem(currentPage++, true);
            }
    };

    swipeTimer =new Timer();
    swipeTimer.schedule(new

    TimerTask() {
        @Override
        public void run () {


           Log.d("hjgv","yughi");

            handler.post(Update);
        }
    },delay,period);

}

@Override
    protected void onStop() {
        super.onStop();
     // swipeTimer.cancel();
       delay=0;
       period=0;

    }

    @Override
    protected void onRestart() {
        super.onRestart();
        delay=2500;
        period=2500;

    }
}

2 Answers2

0

The problem is in your Timer. when you go another fragment or activity then back to that fragment you must have to cancel your timer ondestroyview because when you back to fragment your oncreateview is call again so timer are initialise and then speed is multiple by 2 time.this happens everytime.

This code in kotlin.

 override fun onDestroyView() {

    if(swipeTimer != null) {
        swipeTimer.cancel()

    }
    super.onDestroyView()
}

Hope this help you

Urvish Jani
  • 104
  • 1
  • 4
0

Just call cancel the instance of Timer in your ondestryView() method

 @Override
public void onDestroyView() {
    super.onDestroyView();
    if(swipeTimer != null) {
        swipeTimer.cancel();

    }
}