问题描述
这是入职片段3的代码。在此代码中,我在点击侦听器上设置了浮动操作按钮fb,并试图保存被点击按钮的状态。
public class onBoardinFragment3 extends Fragment {
FloatingActionButton fab;
@Override
public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState) {
ViewGroup root = (ViewGroup) inflater.inflate(R.layout.fragment_onboarding3,container,false);
**fab = root.findViewById(R.id.fab);
fab.setonClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(getActivity(),MainActivity.class);
startActivity(intent);**
**SharedPreferences prf = getActivity().getApplicationContext().getSharedPreferences("mypref",MODE_PRIVATE);
SharedPreferences.Editor editor = prf.edit();
editor.putBoolean("isOpened",true);
editor.commit();**
}
});
return root;
}
}
这是介绍性活动的代码。的价值 isIntroductoryOpened doe也不会在单击浮动后变为false 加载片段3中的按钮。
public class IntroductoryActivity extends AppCompatActivity {
ImageView BgImage;
LottieAnimationView lottieAnimationView;
private static final int Num_Pages = 3;
private ViewPager viewPager;
private ScreenSlidePagerAdapter pagerAdapter;
Animation anim;
@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 (isIntroductoryOpened()){
Intent intent = new Intent(getApplicationContext(),MainActivity.class);
startActivity(intent);
}**
setContentView(R.layout.activity_introductiory);
BgImage = findViewById(R.id.bgImage);
lottieAnimationView = findViewById(R.id.lottie);
BgImage.animate().translationY(-4000).setDuration(1000).setStartDelay(4000);
lottieAnimationView.animate().translationY(1400).setDuration(1000).setStartDelay(4000);
viewPager = findViewById(R.id.pager);
pagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(pagerAdapter);
anim = AnimationUtils.loadAnimation(this,R.anim.customanim);
viewPager.startAnimation(anim);
}
**private boolean isIntroductoryOpened() {
SharedPreferences pref = getApplicationContext().getSharedPreferences("myPref",MODE_PRIVATE);
Boolean isIntroOpened = pref.getBoolean("isOpened",false);
return isIntroOpened;
}**
private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter{
public ScreenSlidePagerAdapter(@NonNull FragmentManager fm) {
super(fm);
}
@NonNull
@Override
public Fragment getItem(int position) {
switch (position){
case 0:
onBoardinFragment1 tab1 = new onBoardinFragment1();
return tab1;
case 1:
onBoardinFragment2 tab2 = new onBoardinFragment2();
return tab2;
case 2:
onBoardinFragment3 tab3 = new onBoardinFragment3();
return tab3;
}
return null;
}
@Override
public int getCount() {
return Num_Pages;
}
}
}
解决方法
在OnBoarding活动之前的活动中,将代码更改为:
SharedPreferences onBoarding = getSharedPreferences("onBoardingScreen",MODE_PRIVATE);
boolean isFirstTime = onBoarding.getBoolean("firstTime",true);
if(isFirstTime){
SharedPreferences.Editor editor = onBoarding.edit();
editor.putBoolean("firstTime",false);
editor.commit();
Intent intent = new Intent(PrevActivity.this,OnBoarding.class);
startActivity(intent);
}
else{
Intent intent = new Intent(PrevActivity.this,NextActivity.class);
startActivity(intent);
}
PrevActivity是OnBoarding活动之前的活动,而NextActivity是OnBoarding活动之后的活动。
在第二行中,真实值是firstTime
的默认值,进入if
块后,该值将设置为false
并且OnBoarding活动不会再次显示在用户。
此代码将代替您编写的从PrevActivity到OnBoarding活动的代码。