0

嗨我试图检测我的应用程序是否已经第一次打开。如果有,我需要运行一个活动,一旦它第二次打开,它不应该再次显示它。Android使用共享首选项来检查第一次运行

这是我的代码:

片段:

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    //Intent intent = new Intent(getActivity(), TutorialFeaturedActivity.class); 
    //startActivity(intent); 

    SharedPreferences settings = this.getActivity().getSharedPreferences(PREFS_NAME, 0); // Get preferences file (0 = no option flags set) 
    boolean firstRun = settings.getBoolean("firstRun", true); // Is it first run? If not specified, use "true" 

    if(firstRun) { 
     Log.w("onCreate: ","first time"); 
     Intent intent = new Intent(getActivity(), TutorialFeaturedActivity.class); 
     startActivity(intent); 

     SharedPreferences.Editor editor = settings.edit(); // Open the editor for our settings 
     editor.putBoolean("firstRun", false); // It is no longer the first run 
     editor.apply(); // Save all changed settings 
    } else { 
     Log.w("onCreate: ","second time"); 
     Intent intent = new Intent(getActivity(), MainActivity.class); 
     startActivity(intent); 
    } 

    getSpecials(); 
} 

但它是所有启动的活动,当我再次启动它,它冻结在一个白色的屏幕,但检查记录,它显示像其他声明不断地反复运行。我对Android很陌生,所以一些帮助或建议将不胜感激

+0

你应该使用editor.commit();保存更改 – gaurang

+0

请问使用getActivity()。finish()是什么?这就是我看到的第一次和第二次运行之间的差异。这可能会让你回到你检查第一次跑步的班级,并且你进入了一个无限循环?! – Nico

+0

当我使用editor.commit();或申请我仍然得到相同的结果,也ide告诉我我shoudl使用应用而不是承诺,但同样的问题。 – thaabitv

回答

0

试试这个办法:

public class MainActivity extends Activity { 

    SharedPreferences prefs = null; 

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

     prefs = getSharedPreferences(PREFS_NAME, MODE_PRIVATE); 
    } 

    @Override 
    protected void onResume() { 
     super.onResume(); 

     if (prefs.getBoolean("firstRun", true)) { 
      Intent intent = new Intent(MainActivity.this, TutorialFeaturedActivity.class); 
      startActivity(intent); 
      prefs.edit().putBoolean("firstRun", false).commit(); 
     } 
     else 
     { 
      //do nothing 
     } 

     getSpecials(); 
    } 
} 
+0

这工作完美谢谢但是,当它添加在下一个活动,我显示一个tut屏幕它不工作相同,无论如何,它在多个活动工作相同? – thaabitv

1

它看起来像你的活动循环,因为在你的else语句中,你告诉它重新开始在else语句中再次登陆的活动,等等等等。

+0

我同意..类似这样的事情正在发生 - SharedPreferences不是问题。如果OP代码中的片段连接到MainActivity,这将是有意义的。 –

0

尝试使用editor.commit如果editor.apply不工作

editor.putBoolean("firstRun", false); 
editor.apply(); // Save all changed settings 
editor.commit(); // Save all changed settings 
+0

如上述评论所述,除了异步性之外,这两者之间没有区别。 OP还表示,改变并没有解决问题。 – Nico

+0

我知道没有区别B/W适用和承诺 – Pehlaj

+0

我认为“没有区别”是误导。编辑。commit()在editor.apply()保存在后台时立即保存更改。 –

0

在其他情况下,你的起点 “MainActivity.class”。 为什么从MainActivity的oncreate中加载MainActivity? 它会形成一个循环。

从其他案例中删除开始活动。

0
@Override 
public void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    SharedPreferences pref = YourActivityName.this.getSharedPreferences(PREFS_NAME,0); 
    SharedPreferences.Editor editor= pref.edit(); 
    boolean firstRun = pref.getBoolean("firstRun", true); 
    if(firstRun) 
    { 
     Log.i("onCreate: ","first time"); 
     editor.putBoolean("firstRun",false); 
     editor.commit(); 
     Intent intent = new Intent(getActivity(), TutorialFeaturedActivity.class); 
     startActivity(intent); 
    } 
    else 
    { 
     Log.i("onCreate: ","second time"); 
     Intent intent = new Intent(getActivity(), MainActivity.class); 
     startActivity(intent); 

    } 
    // getSpecials(); 
} 
0

请尽量将我的朋友

public class SessionManager { 

private static String TAG = SessionManager.class.getSimpleName(); 

SharedPreferences pref; 
SharedPreferences.Editor editor; 
Context _context; 

// Shared pref mode 
int PRIVATE_MODE = 0; 

// Shared preferences file name 
private static final String PREF_NAME = "ManageRun"; 

private static final String KEY_IS_RUN = "isRun"; 


public SessionManager(Context context) { 
    this._context = context; 
    pref = _context.getSharedPreferences(PREF_NAME, PRIVATE_MODE); 
    editor = pref.edit(); 
} 

public void setLatest(boolean isRun) { 

    editor.putBoolean(KEY_IS_RUN, isRun); 
    // commit changes 
    editor.commit(); 
    Log.d(TAG, "Manage Version session modified!"); 
} 

public boolean isLatest() { 
    return pref.getBoolean(KEY_IS_RUN, true); 
} 
} 

**在第一个活动检查**

private SessionManager session; 


session = new SessionManager(getApplicationContext()); 

if (session.isLatest()) { 
    session.setLatest(false); 
    Log.w("onCreate: ","first time"); 
    Intent intent = new Intent(getActivity(), TutorialFeaturedActivity.class); 
    startActivity(intent); 
} 
else 
{ 
    Log.w("onCreate: ","second time"); 
    Intent intent = new Intent(getActivity(), MainActivity.class); 
    startActivity(intent); 
} 
+0

这可以在多个活动中完成吗? @Ratilal – thaabitv

+0

多个活动的意思是? –

+0

我想即时通讯代码与试图在另一个活动中使用代码的代码问题,但因为首选项正在保存在一个活动的第一次运行它不显示在下一个。 – thaabitv

0
private boolean isFirstTime() { 
     if (firstTime == null) { 
      SharedPreferences mPreferences = this.getSharedPreferences("first_time", Context.MODE_PRIVATE); 
      firstTime = mPreferences.getBoolean("firstTime", true); 
      if (firstTime) { 
       SharedPreferences.Editor editor = mPreferences.edit(); 
       editor.putBoolean("firstTime", false); 
       editor.commit(); 
      } 
     } 
     return firstTime; 
    } 



if (isFirstTime()) { 

        Intent i = new Intent(SplashActivity.this, Intro_slider.class); 
        i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
        startActivity(i); 
        finish(); 

       } else { 
        final UserFunctions uf = new UserFunctions(); 
        if (uf.isUserLoggedIn(SplashActivity.this)) { 
         Intent i = new Intent(SplashActivity.this, MainActivity.class); 
         i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
         startActivity(i); 
         finish(); 
        } else { 
         Intent i = new Intent(SplashActivity.this, Social_Login_Activity.class); 
         i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
         startActivity(i); 
         finish(); 
        } 
       } 

试试这个代码。