i'm trying to show an activity only once with Onclick button firstly i have an activity useheadphone contains button
when user click in this button
mainactivity will start
my 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();
}
}
});
}
my manifest :
<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>
I think it should probably be something like (you can add an else if you need to):
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();
}
}
});
Add this code inside of the onCreate method at the Bottom of onCreate
method (dont delete the original I have just copied and paste)
.:
if(pref.getBoolean("activity_executed", false)){
Intent toMain = new Intent(this,MainActivity.class);
startActivity(toMain);
}
You should also consider using smaller context here:
Change this line:
Intent toMain = new Intent(getApplicationContext(), MainActivity.class);
To:
Intent toMain = new Intent(useheadphone.this, MainActivity.class);
This code will show main activity only once when user click the button (skip for now) and redirect it to the second activity.
On your onCreate method at Main Activity
SharedPreferences sharedPreferences = getSharedPreferences("PREFERENCE", MODE_PRIVATE);
String FirstTimeInstall = sharedPreferences.getString("FirstTimeInstall", "");
if (FirstTimeInstall.equals("Yes")) {
startActivity(new Intent(MainActivity.this, SecondActivity.class));
}
On your onClickListener to your button at Main Activity
skipBtn.setOnClickListener(view -> {
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("FirstTimeInstall", "Yes");
editor.apply();
startActivity(new Intent(MainActivity.this, SecondActivity.class));
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.