我尝试使用Onclick按钮只显示一次活动
首先,当用户点击此button
主要活动时,我的活动 useheadphone 包含button
我的useheadphone.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>
答案 0 :(得分:2)
我认为应该是这样的(如果需要,可以添加其他内容):
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();
}
}
});
答案 1 :(得分:1)
在onCreate
方法(dont delete the original I have just copied and paste)
底部的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);