Run activity only once, then always run the main one

后端 未结 4 1721
北海茫月
北海茫月 2021-01-16 20:11

As the title says, Scenario is: On first time using the app, Show Screen A. Once you are done with screen A, the button will lead to you Screen B. From now on and forever, t

相关标签:
4条回答
  • 2021-01-16 20:42
    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);
    
    }
    
    0 讨论(0)
  • 2021-01-16 20:53

    All you have to check like this

     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);
        }
    
    0 讨论(0)
  • 2021-01-16 20:55
    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
          }
    
       }
    }
    

    Whenever you are done with showing the first activity simple set the shared prefs boolean flag to false:

    prefs.getEditor.setBoolean("firstStart", false).commit();
    
    0 讨论(0)
  • 2021-01-16 21:01

    Just declare your Activity A as a launcher Activity in your AndroidManifest.xml and inside your Activity A onCreate() you can simply do this

    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();
          }
    }
    
    0 讨论(0)
提交回复
热议问题