一次性欢迎屏幕无效

时间:2017-12-16 21:12:46

标签: java android sharedpreferences

因此,我希望在我在其他帖子中找到的答案中使用package org.reliefdev.reliefdevedworking; import android.content.Intent; import android.content.SharedPreferences; import android.preference.PreferenceManager; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; public class WelcomeActivity extends AppCompatActivity { SharedPreferences prefs; final String showWelcomeScreenString = "showWelcome"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_welcome); Button getStarted = findViewById(R.id.getStartedBtn); prefs = PreferenceManager.getDefaultSharedPreferences(this); final Boolean welcomeScreenShown = prefs.getBoolean(showWelcomeScreenString, false); getStarted.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if (welcomeScreenShown) { startActivity(new Intent(WelcomeActivity.this, LoginReliefAccountActivity.class)); SharedPreferences.Editor editor = prefs.edit(); editor.putBoolean(showWelcomeScreenString, true); editor.apply(); } else if(!welcomeScreenShown){ startActivity(new Intent(WelcomeActivity.this, LoginReliefAccountActivity.class)); } } }); } } 为我的应用程序创建一次性介绍屏幕。起初,代码似乎正在运行,但现在不是, 有人可以告诉我发生了什么吗?

WelcomeActivity.java(启动器活动):

var targetElement;

3 个答案:

答案 0 :(得分:0)

Init:final Boolean welcomeScreenShown = prefs.getBoolean(showWelcomeScreenString,true); AS True。第一次运行后将其设为False:   editor.putBoolean(showWelcomeScreenString,false);

 public class WelcomeActivity extends AppCompatActivity {

SharedPreferences prefs;
final String showWelcomeScreenString = "showWelcome";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_welcome);


    Button getStarted = findViewById(R.id.getStartedBtn);

    prefs = PreferenceManager.getDefaultSharedPreferences(WelcomeActivity.this);
    final Boolean welcomeScreenShown = prefs.getBoolean(showWelcomeScreenString, true);

    getStarted.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (welcomeScreenShown) {
                startActivity(new Intent(WelcomeActivity.this, LoginReliefAccountActivity.class));
                SharedPreferences.Editor editor = prefs.edit();
                editor.putBoolean(showWelcomeScreenString, false);
                editor.apply();

            } else if(!welcomeScreenShown){
                startActivity(new Intent(WelcomeActivity.this, LoginReliefAccountActivity.class));
            }
        }
    });
}

}

答案 1 :(得分:0)

它有效

public class MainActivity extends AppCompatActivity {

SharedPreferences prefs;
 boolean welcomeScreenShown ;
final String showWelcomeScreenString = "showWelcome";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    Button getStarted = (Button) findViewById(R.id.txt);

    prefs = PreferenceManager.getDefaultSharedPreferences(MainActivity.this);
    welcomeScreenShown = prefs.getBoolean(showWelcomeScreenString, true);

    getStarted.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Toast.makeText(MainActivity.this, "welcomeScreenShown:"+welcomeScreenShown, Toast.LENGTH_SHORT).show();
            System.out.println("welcomeScreenShown:"+welcomeScreenShown);
            if (welcomeScreenShown) {
                welcomeScreenShown = false;

                SharedPreferences.Editor editor = prefs.edit();
                editor.putBoolean(showWelcomeScreenString, false);
                editor.apply();
                //startActivity(new Intent(MainActivity.this, LoginReliefAccountActivity.class));

            } else if(!welcomeScreenShown){
               // System.out.println("welcomeScreenShown:"+welcomeScreenShown);
               // startActivity(new Intent(MainActivity.this, LoginReliefAccountActivity.class));
            }
        }
    });
}

}

答案 2 :(得分:0)

行。我在代码中犯了一个愚蠢的错误。我将SharedPreferences代码放在OnClickListener中。如果我反过来,那么如果sharedprefs布尔值为true,则设置OnClickListener。