仅运行一次活动,然后始终运行主活动

时间:2015-05-09 18:57:24

标签: java android xml android-activity launch

正如标题所说,情景是: 第一次使用应用程序时,显示屏幕A. 完成屏幕A后,按钮将显示屏幕B. 从现在开始,永远,当您启动应用程序时,屏幕B将始终是主屏幕(活动?)。 我正在尝试这2天,我无法得到它。 有人请详细解释,甚至更好地给我一个code.rar所以我可以研究它。我为此疯狂了!!!

4 个答案:

答案 0 :(得分:0)

所有你必须像这样检查

 SharedPreferences prefs = getSharedPreferences("mySHaredpref", Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = prefs.edit();
    boolean isFirst = prefs.getBoolean("isfirstTime", true);
    if(isFirst) {
        Intent intent = new Intent(this, ActivtyA.class);
        editor.putBoolean(KEY_IS_FIRST_TIME, false);
        editor.commit();
        startActivity(intent);
    }
    else{
      Intent intent = new Intent(this, MainActivty.class);
      startActivity(intent);
    }

答案 1 :(得分:0)

只需将您的活动A声明为AndroidManifest.xml中的启动器活动,然后在您的活动A onCreate()内,您就可以执行此操作

private SharedPreferences mSharedPreferences;
private Editor mEditor;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_a);

    mSharedPreferences = getSharedPreferences("yourPrefsFileName", Context.MODE_PRIVATE));
    mEditor = mSharedPreferences.edit();

    if (mSharedPreferences.getBoolean("isfirstTime", true)) {
        mEditor.putBoolean("isFirstTime",false);
        mEditor.apply();
    }else{
         startActivity(new Intent(this, ActivityB.class));
         overridePendingTransition(0, 0);
         finish();
      }
}

答案 2 :(得分:0)

public class FirstActivity extends Activity {

   public void onCreate(Bundle saved){
      super.onCreate();

      SharedPreferences prefs = getSharedPreferences("MyPrefs", Context.MODE_PRIVATE);

     if (!prefs.getBoolean("firstStart", true)){
          startActivity(this, SecondActivity.class);
          finish(); // Finish the current one, is like forwarding directly to the second one
      }

   }
}

每当您完成显示第一个活动时,将共享prefs boolean标志设置为false:

prefs.getEditor.setBoolean("firstStart", false).commit();

答案 3 :(得分:0)

SharedPreferences sp = getSharedPreferences("checking",MODE_PRIVATE);

String data = sp.getString("check", "");

if (data.equals("success")) {

//one time proccess code

//with follow code

SharedPreferences sp= getSharedPreferences("checking",MODE_PRIVATE);

Editor e1 = sp.edit();

e1.putString("check","success");

e1.commit();


} else {

// code for main

 Intent intent = new Intent(this, MainActivty.class);
 startActivity(intent);

}