几秒间隔后显示相同的活动

时间:2018-03-22 19:39:10

标签: android

我开发了一个Android应用程序,一旦滑块完成MainActivity就会显示启动画面但是在我的情况下,一旦显示MainActivity,它会在几秒钟后自动重新加载(像MainActivity第一次打开时一样打开) 5秒)。

这是我的SplashScreenActivity

import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Html;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

public class SplashScreenActivity extends AppCompatActivity {

    private ViewPager viewPager;
    private LinearLayout dots_layout;
    private TextView[] dots_count;
    private int[] layouts;
    private Button button_next;
    private Button button_skip;
    private SplashScreenAdapter splashScreenAdapter;
    private SharedPreference sharedPreference;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        sharedPreference = new SharedPreference(this);
        if (!sharedPreference.isFirstTimeLaunch()) {
            startFunctionsActivity();
            finish();
        }else{
            sharedPreference.setFirstTimeLaunch(false);
        }
        setStatusbarTransparent();
        setContentView(R.layout.activity_splash_screen);

        viewPager = findViewById(R.id.view_pager);
        dots_layout =(LinearLayout) findViewById(R.id.splashscreen_dots);
        button_next =(Button) findViewById(R.id.button_next);
        button_skip =(Button) findViewById(R.id.button_skip);

        button_next.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int currentpage = viewPager.getCurrentItem() + 1;
                if (currentpage < layouts.length) {
                    viewPager.setCurrentItem(currentpage);
                } else {
                    startFunctionsActivity();
                }
            }
        });
        button_skip.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            startFunctionsActivity();
            }
        });

        layouts = new int[]{R.layout.slider_one,R.layout.slider_two,R.layout.slider_three,R.layout.slider_four};
        splashScreenAdapter = new SplashScreenAdapter(layouts,getApplicationContext());
        viewPager.setAdapter(splashScreenAdapter);

        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }
            @Override
            public void onPageSelected(int position) {
                if (position == layouts.length - 1) {
                    button_next.setText("START");
                    button_skip.setVisibility(View.GONE);
                } else {
                    button_next.setText("NEXT");
                    button_skip.setVisibility(View.VISIBLE);
                }
                setDotStatus(position);
            }
            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });
        setDotStatus(0);

    }
    private void setDotStatus(int page) {
        dots_layout.removeAllViews();
        dots_count = new TextView[layouts.length];
        for (int i = 0; i < dots_count.length; i++) {
            dots_count[i] = new TextView(this);
            dots_count[i].setText(Html.fromHtml("&#8226;"));
            dots_count[i].setTextSize(30);
            dots_count[i].setTextColor(Color.parseColor("#a9b4bb"));
            dots_layout.addView(dots_count[i]);
        }
        if (dots_count.length > 0) {
            dots_count[page].setTextColor(Color.parseColor("#ffffff"));
        }
    }
    private void startFunctionsActivity() {
        sharedPreference.setFirstTimeLaunch(false);
        startActivity(new Intent(SplashScreenActivity.this, FunctionsActivity.class));
        finish();
    }

    private void setStatusbarTransparent() {
        if (Build.VERSION.SDK_INT >= 21) {
            getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
            Window window = getWindow();
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.TRANSPARENT);
        }
    }
}

SplashScreenAdapter

public class SplashScreenAdapter extends PagerAdapter {

    private LayoutInflater inflater;
    private int[] layouts;
    private Context context;

    public SplashScreenAdapter(int[] layouts, Context context) {
        this.layouts = layouts;
        this.context = context;
        inflater=LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return layouts.length;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view==object;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        View V = inflater.inflate(layouts[position],container,false);
        container.addView(V);
        return V;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        ((ViewPager) container).removeView((View) object);
        View v =(View)object;
        container.removeView(v);
    }
}

MainActivity

public class MainActivity extends AppCompatActivity {

    private TextView textview_myQuote;
    private ImageView imageView_logo;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);

        if (!isFirstTimeStartApp()) {
            startSplashScreenActivity();
            finish();
        }else{
            setFirstTimeStartStatus(false);
        }

        setContentView(R.layout.activity_main);

        textview_myQuote = (TextView) findViewById(R.id.splash_screen_text);
        imageView_logo = (ImageView) findViewById(R.id.splash_screen_logo);

        Animation animation = (Animation) AnimationUtils.loadAnimation(this,R.anim.welcomescreen_animation);
        textview_myQuote.startAnimation((Animation) animation);
        imageView_logo.startAnimation((Animation) animation);

        final  Intent intent = new Intent(this,SplashScreenActivity.class);

         /* Purpose : splash screen animation on logo and Textview */

        Thread timerThread = new Thread(){
            public void run(){
                try{
                    sleep(5000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                finally {
                    startActivity(intent);
                    finish();
                }
            }
        };
        timerThread.start();
    }
    private boolean isFirstTimeStartApp() {
        SharedPreferences ref = getApplicationContext().getSharedPreferences("splashscreen", Context.MODE_PRIVATE);
        return ref.getBoolean("Flag", true);
    }

    private void setFirstTimeStartStatus(boolean status) {
        SharedPreferences ref = getApplicationContext().getSharedPreferences("splashscreen", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = ref.edit();
        editor.putBoolean("Flag", status);
        editor.commit();
    }
    private void startSplashScreenActivity() {
        setFirstTimeStartStatus(false);
        startActivity(new Intent(MainActivity.this, SplashScreenActivity.class));
        finish();
    }
}

从这个活动开始我只调用了FunctionsActivity(MainActivity)。但是几秒后它就会重新加载。请指导我解决它。

1 个答案:

答案 0 :(得分:0)

你完成了两次Splash Activity ......

if (!sharedPreference.isFirstTimeLaunch()) { startFunctionsActivity(); return;}

itz足够我认为