仅使用Onclick按钮显示一次活动



我尝试仅使用Onclick按钮显示一次活动首先,我有一个活动使用耳机包含button当用户单击此button活动将启动

我的耳机.java :

public class useheadphone extends Activity {
    Button skip;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_useheadphone);
        final SharedPreferences pref = getSharedPreferences("ActivityPREF", Context.MODE_PRIVATE);
        hide();
        skip = (Button)findViewById(R.id.skip);
        skip.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(pref.getBoolean("activity_executed", false)){
                    Intent toMain = new Intent(getApplicationContext(),MainActivity.class);
                    startActivity(toMain);
                    finish();
                } else {
                    SharedPreferences.Editor ed = pref.edit();
                    ed.putBoolean("activity_executed", true);
                    ed.commit();
                }
            }
        });
    }

我的清单

<activity
            android:name=".useheadphone"
            android:configChanges="orientation|keyboardHidden|screenSize"
            android:label="@string/title_activity_useheadphone"
            android:screenOrientation="portrait"
            android:theme="@style/FullscreenTheme">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
在方法

(dont delete the original I have just copied and paste)底部的 onCreate 方法中添加onCreate代码:

if(pref.getBoolean("activity_executed", false)){
                Intent toMain = new Intent(this,MainActivity.class);
               startActivity(toMain);
}

您还应该考虑在此处使用较小的上下文:

更改此行:

Intent toMain = new Intent(getApplicationContext(), MainActivity.class);

自:

Intent toMain = new Intent(useheadphone.this, MainActivity.class);

我认为它应该是这样的(如果需要,您可以添加 else):

skip.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(pref.getBoolean("activity_executed", false)){
                SharedPreferences.Editor ed = pref.edit();
                ed.putBoolean("activity_executed", true);
                ed.commit();
                Intent toMain = new Intent(getApplicationContext(),MainActivity.class);
                startActivity(toMain);
                finish();
            }
        }
    });

当用户单击按钮(暂时跳过)并将其重定向到第二个活动时,此代码将仅显示一次主活动。

主活动的创建方法上

SharedPreferences sharedPreferences = getSharedPreferences("PREFERENCE", MODE_PRIVATE);
String FirstTimeInstall = sharedPreferences.getString("FirstTimeInstall", "");
    if (FirstTimeInstall.equals("Yes")) {
         startActivity(new Intent(MainActivity.this, SecondActivity.class));
    }

在您的 onClickListener主活动的按钮

skipBtn.setOnClickListener(view -> {
            SharedPreferences.Editor editor = sharedPreferences.edit();
            editor.putString("FirstTimeInstall", "Yes");
            editor.apply();
            startActivity(new Intent(MainActivity.this, SecondActivity.class));
        });

最新更新