ViewPager片段和CircleIndicator重新创建自己

时间:2016-04-24 07:06:35

标签: android android-fragments android-viewpager

美好的一天,我遇到了与viewPagerCircleIndicator有关的问题。 每次我切换到另一个Activity或按下 TAB硬件键盘面板(应用程序之间的切换选项卡),然后返回 到WelcomeActivity.java它重新创建自己 - circleDots和片段中的项目(重新创建片段)。 而且,如果我按下面板中的TAB硬件键盘,然后重新进入我的应用程序,它将不允许我直接返回,它会飞溅白色然后消失。 如果我点击图标返回我的应用程序,它会重新创建片段。你能帮帮我吗?

Bug

这是我的代码: WelcomeActivity.java

protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_welcome_page);

    //Function Call
        assignWidgetId();
    //ViewPager for text AppIntro
        mViewPager = (ViewPager) findViewById(R.id.viewpager_welcome_msg);
        circlePageIndicator = (CirclePageIndicator) findViewById(R.id.circle_indicator);
        setUpViewPager(mViewPager);
        circlePageIndicator.setViewPager(mViewPager);

    runnable = new Runnable() {
        @Override
        public void run() {
            int currentPage = 0;
            if(currentPage == FRAGMENT_PAGE - 1){
                currentPage = 0;
            }
            mViewPager.setCurrentItem(currentPage,true);
            handler.postDelayed(runnable,DELAY_TIME_CHANGE_SLIDE);
        }
    };

    swipeTimer = new Timer();


    try{
        mVideoView = (VideoView) findViewById(R.id.yayong_welcome_video);
        mVideoView.setVideoURI(uri);
        mediaPlayer.setVolume(0, 0);
        mVideoView.start();
    } catch (Exception e){
        //mImageView.setVisibility(View.VISIBLE);
    }

    //IF ERROR VIDEO CANNOT BE PLAYED
    mVideoView.setOnErrorListener(new MediaPlayer.OnErrorListener() {
        @Override
        public boolean onError(MediaPlayer mediaPlayer, int i, int i1) {
            //wait for 1 seconds
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        Thread.sleep(1000);
                        //mImageView.setVisibility(View.VISIBLE);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }).start();
            if (videoCompleted) {
                mVideoView.start();
                mediaPlayer.setVolume(0, 0);
            }
            return false;
        }
    });
    //IF COMPLETE REPEAT AGAIN - SILENT
    mVideoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
        @Override
        public void onCompletion(MediaPlayer mediaPlayer) {
            videoCompleted = true;
            mVideoView.start();
            mediaPlayer.setVolume(0, 0);
        }
    });


}

public void assignWidgetId(){
    btnSignIn = (Button) findViewById(R.id.signIn);
    btnSignIn.setOnClickListener(this);
    btnSignUp = (Button) findViewById(R.id.signUp);
    btnSignUp.setOnClickListener(this);
    textViewForgotPass = (TextView) findViewById(R.id.textViewForgotPassword);
    textViewForgotPass.setOnClickListener(this);
    //mImageView = (ImageView) findViewById(R.id.welcome_image_backup);
}

@Override
public void onClick(View v) {

    switch (v.getId()){
        //GO TO LOGIN ACTIVITY
        case R.id.signIn:
            Intent intentSignIn = new Intent(WelcomePageActivity.this,LoginActivity.class);
            startActivity(intentSignIn);
            finish();
            break;

        //GO TO REGISTER ACTIVITY
        case R.id.signUp:
            Intent intentSignUp = new Intent(WelcomePageActivity.this,RegisterActivity.class);
            startActivity(intentSignUp);
            finish();
            break;

        case R.id.textViewForgotPassword:
            forgotPasswordDialog();
            break;
    }
}

public void setUpViewPager(ViewPager viewPager){
    Adapter adapter = new Adapter(getSupportFragmentManager());
    adapter.addFragments(new Welcome_First_Fragment());
    adapter.addFragments(new Welcome_Fragment_Second());
    adapter.addFragments(new Welcome_Fragment_Third());
    viewPager.setAdapter(adapter);
}

static class Adapter extends FragmentPagerAdapter{
    private static List<Fragment> mFragmentList = new ArrayList<>();

    public Adapter(FragmentManager fragmentManager){
        super(fragmentManager);
    }

    public void addFragments(Fragment fragment){
        mFragmentList.add(fragment);
    }

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

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

}

WelcomeFragments.java

public class Welcome_First_Fragment extends Fragment {
RelativeLayout relativeLayout;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View viewRoot = inflater.inflate(R.layout.welcome_fragment_first, container,false);
    relativeLayout = (RelativeLayout) viewRoot.findViewById(R.id.welcome_about_first);

    return viewRoot;
}

我正在使用jake wharton viewpager circleIndicator 我通过使用System.exit(0)解决了这个问题;每次我回到这个页面,这样它就不会重新创建这些项目。我知道这不是最好的方法。还有其他方法吗?

0 个答案:

没有答案