【问题标题】:Android using shared preferences to check on first runAndroid 使用共享首选项检查首次运行
【发布时间】:2017-10-23 05:01:40
【问题描述】:

您好,我正在尝试检测我的应用是否已首次打开。如果有,我需要运行一个活动,一旦它第二次打开,它就不应该再次显示它。

这是我的代码:

片段:

@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();
}

但它所做的只是启动活动,当我再次启动它时,它会冻结在一个白屏中,但检查它显示的日志,就像 else 语句不断地反复运行一样。我对 Android 还很陌生,因此非常感谢您提供一些帮助或建议

【问题讨论】:

  • 你应该使用 editor.commit();用于保存更改
  • 请问getActivity().finish()有什么用?这就是我看到的第一次和第二次运行之间的区别。这可能会让你回到你检查第一次运行并进入无限循环的类?!
  • 当我使用 editor.commit();或应用我仍然得到相同的结果,ide 也告诉我我应该使用 apply 而不是 commit 但同样的问题。
  • @Nico 我使用 getActivity().finish();要结束正在运行的教程活动,我忘了在问题中将其取出,因为它仍然无法解决任何问题
  • 请将所有代码发布到这个文件中。

标签: java android android-fragments android-intent android-sharedpreferences


【解决方案1】:
@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();
}

【讨论】:

    【解决方案2】:

    看起来您的活动正在循环,因为在您的 else 语句中,您告诉它重新启动再次落在 else 语句中的活动,依此类推。

    【讨论】:

    • 我同意......这样的事情正在发生 - SharedPreferences 不是问题。如果将 OP 代码中的 Fragment 附加到 MainActivity 将是有意义的。
    【解决方案3】:

    如果editor.apply 不起作用,请尝试使用editor.commit

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

    【讨论】:

    • 正如上面提到的cmets,除了异步之外,这两者之间没有区别。 OP 还表示,改变并没有解决问题。
    • 我知道没有差异 b/w 应用和提交
    • 我认为“没有区别”具有误导性。 editor.commit() 立即保存更改,而 editor.apply() 在后台保存。
    • 但它的功能是一样的,对吧?这就是我想说的
    【解决方案4】:

    在其他情况下,您开始“MainActivity.class”。 为什么要从 MainActivity 的 oncreate 加载 MainActivity ? 它会循环。

    从 else case 中移除 start Activity。

    【讨论】:

      【解决方案5】:

      试试这个方法:

      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();
          }
      }
      

      【讨论】:

      • 这很好用,谢谢,但是当我在下一个活动中添加它时,我会显示一个 tut 屏幕,它的工作方式不同,无论如何让它在多个活动中工作相同??
      【解决方案6】:

      请朋友试试这个

      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);
      }
      

      【讨论】:

      • 多活动是什么意思?
      • 我认为我的代码存在问题,我也试图在另一个活动中使用该代码,但由于首选项是在第一次运行一个活动时保存的,因此它不会在下一个活动中显示。
      【解决方案7】:
          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();
                          }
                      }
      

      试试这个代码。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多