我有一个TapTarget
,当我打开应用程序时,我只希望它在应用程序首次启动时就打开,如何存储值以确保它在其他启动时不会再次打开,这是我尝试过的方法,但无法正常工作,TapTarget
每次应用启动时都会打开。
代码:
realm.executeTransaction { realm ->
val result = Taptarget()
result.cal = ""
result.chat = ""
result.depfpsc = ""
result.info = ""
result.module = ""
realm.insert(result)
}
realm.executeTransaction { realm ->
val result = realm.where(Taptarget::class.java).findFirst()!!
if(result.module == "Y")
{
}
else
{
if (mFabPrompt != null) {
return@executeTransaction
}
mFabPrompt = MaterialTapTargetPrompt.Builder(this@MainActivity)
.setTarget(findViewById<View>(R.id.navigation_modules))
.setPrimaryText("Send your first email")
.setSecondaryText("Tap the envelope to start composing your first email")
.setIconDrawable(resources.getDrawable(R.drawable.ic_folder_black_24dp))
.setBackgroundColour(resources.getColor(R.color.colorAccentTrans))
.setAnimationInterpolator(FastOutSlowInInterpolator())
.setPromptStateChangeListener { prompt, state ->
if (state == MaterialTapTargetPrompt.STATE_FOCAL_PRESSED || state == MaterialTapTargetPrompt.STATE_DISMISSING) {
mFabPrompt = null
}
}
.create()
mFabPrompt!!.show()
result.depfpsc = "Y"
realm.insertOrUpdate(result!!)
}
}
答案 0 :(得分:1)
我不确定您使用哪种语言编写代码。但是让我用我精通的Java进行解释。您可以轻松地将其适应任何其他语言。
首先像这样创建一个类PreferencesManager:
public class PreferencesManager {
SharedPreferences pref;
SharedPreferences.Editor editor;
Context _context;
// shared pref mode
int PRIVATE_MODE = 0;
// Shared preferences file name
private static final String PREF_NAME = "splash-welcome";
// Shared preference variable name
private static final String IS_FIRST_TIME_LAUNCH = "IsFirstTimeLaunch";
// Constructor
public PrefManager(Context context) {
this._context = context;
pref = _context.getSharedPreferences(PREF_NAME, PRIVATE_MODE);
editor = pref.edit();
}
// This method to be used as soon as the fist time launch is completed to update the
// shared preference
public void setFirstTimeLaunch(boolean isFirstTime) {
editor.putBoolean(IS_FIRST_TIME_LAUNCH, isFirstTime);
editor.commit();
}
// This method will return true of the app is launched for the first time. false if
// launched already
public boolean isFirstTimeLaunch() {
return pref.getBoolean(IS_FIRST_TIME_LAUNCH, true);
}
}
现在,在每个活动中,您都必须检查应用程序是否是首次启动:
PreferencesManager preferencesManager = new PreferencesManager (this);
if (!preferencesManager.isFirstTimeLaunch()) {
// Set shared preference value to false so that this block will not be called
// again until your user clear data or uninstall the app
preferencesManager.setFirstTimeLaunch(false);
// Write your logic here
}
这可能不是您要查找的确切答案。但这可能会为您指明正确的方向:)