Skip start of Activity if a condition has already been met

前端 未结 2 1619
甜味超标
甜味超标 2021-01-07 15:23

In my Android app i have a Google plus login activity with the method

@Override
public void onConnected(Bundle connectionHint) {
    String accountName = mPl         


        
相关标签:
2条回答
  • 2021-01-07 15:30

    Following @MysticMaggic's answer, i finally got it to work.For future reference, this is what i did:

    In the onCreate(Bundle savedInstanceState) method of my UserDetailsCaptureActivity.java class (where i want to save the sharedPreferences) i did

    SharedPreferences userDetailsPrefEditor = getApplicationContext().getSharedPreferences("MyPref", MODE_PRIVATE); 
        final Editor editor = userDetailsPrefEditor.edit();
    
    
        final Intent userProfileDisplayIntent = new Intent(this, UserProfileActivity.class);
    
        submitButton.setOnClickListener(new View.OnClickListener() {
    
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                if(v.getId() == R.id.userDetailsCaptureButton) {
                    new EndpointsTask().execute(getApplicationContext());
                    editor.putBoolean("hasSetDetails", true);
                    editor.commit();
                    startActivity(userProfileDisplayIntent);
    
                }
                }
            });
    

    and then in the onConnected() method of my LoginActivity (where i want to access the sharedPreferences and check if the user has already entered their details) i did this

    @Override
    public void onConnected(Bundle connectionHint) {
        ...
        SharedPreferences userDetailsPrefEditor = getApplicationContext().getSharedPreferences("MyPref", MODE_PRIVATE); 
        boolean hasSetDetails = userDetailsPrefEditor.getBoolean("hasSetDetails", false);
        ...
    
      if (hasSetDetails == true) { //If user has already entered their details show profile page, else show user details capture screen.
           Intent userProfileDisplayIntent = new Intent(this, UserProfileActivity.class);
           startActivity(userProfileDisplayIntent);
       } else {
           Intent userDetailsCaptureIntent = new Intent(this, UserDetailsCaptureActivity.class);
           startActivity(userDetailsCaptureIntent);
        }
    }
    

    That's all.

    0 讨论(0)
  • 2021-01-07 15:43

    Save these user data in SharedPreference when user fills up them in UserDetailsCaptureActivity.java.

    userDetailsPrefEditor.putString("user_name", userName).commit();
    

    Then after each successsful login, you need to check if the data already exists in SharedPreference or not.

    userName = userDetailsPrefEditor.getString("user_name", "default");
    
    if (userName == "default")
    {
        //start activity for capturing details
    }
    else
    {
        //do something else
    }
    

    Hope this helps.

    0 讨论(0)
提交回复
热议问题