0

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

@Override
public void onConnected(Bundle connectionHint) {
    String accountName = mPlusClient.getAccountName();
    Toast.makeText(this, accountName + " is connected.", Toast.LENGTH_LONG).show();

    ...
    Intent userDetailsCaptureIntent = new Intent(this,UserDetailsCaptureActivity.class);
    startActivity(userDetailsCaptureIntent);
}

In this method after the user has been signed in, i start the UserDetailsCaptureActivity where details about the user are collected and stored in a Google App Engine backend.The problem is after i enter the details and save them i don't want Activity to ever start again, but it starts because i keep calling it the onConnected() method. How do i let the Activity with the onConnected method know that UserDetailsCaptureActivity should only be called once? If this is confusing am willing to explain further.

Ojonugwa Jude Ochalifu
  • 26,627
  • 26
  • 120
  • 132

2 Answers2

3

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.

MysticMagicϡ
  • 28,593
  • 16
  • 73
  • 124
  • let me clarify my question.I have no problem getting the data.When i enter the user data into my textfields and press submit it is entered into my Google App Engine backend as expected.Now when the user of my app launches my app again, i want the app to "know" that the data containing user details has already been captured therefore ignoring the start of `UserDetailsCaptureActivity` and proceeding to another activity. – Ojonugwa Jude Ochalifu Feb 11 '14 at 13:49
  • It is clear to me. Read answer again. @OjonugwaOchalifu First time, it will not get Captured user details in shared preference and it will start activity. In that activity, you will save data in shared preference (not all data, just a boolean that user has filled form.) And whenever user starts the app again, on login, shared pref will give data and that activity won't be started. – MysticMagicϡ Feb 11 '14 at 13:51
  • So you want the UserDetailsCaptureActivity to open only once when the user details are not already filled in ? – San Feb 11 '14 at 13:53
  • seems like that. @San E.g. setting kind of thing – MysticMagicϡ Feb 11 '14 at 13:53
  • @Mystic Magic,Then your answer would work like a charm. – San Feb 11 '14 at 13:55
  • oh i see.I thought that was what you meant but was confused by your use of `username`.This would mean i need to delete the intent to start `UserDetailsCaptureActivity` in `onConnected` and then add the code to check the boolean there right? – Ojonugwa Jude Ochalifu Feb 11 '14 at 13:57
  • @Ojonugwa Ochalifu, You gotta receive the data from UserDetailsCaptureActivity in your onConnected and check whether it already exists in the shared preference. If this is not what you're looking for then ask for more. I'll try my best. – San Feb 11 '14 at 13:57
  • @OjonugwaOchalifu yes. right :) intentb will go in if condition, where userdata is not found in shared preferences. – MysticMagicϡ Feb 11 '14 at 13:58
  • 1
    You cannot delete the intent, it needs to be there for the first time info filling. – San Feb 11 '14 at 13:59
  • Lol.That is True @San – Ojonugwa Jude Ochalifu Feb 11 '14 at 13:59
  • @MysticMagic One last question, how do i get userDetailsPrefEditor in my LoginActivity since it is declared in the UserDetailsCaptureActivity? I tried to declare sharedPreferences as static but it won't work. – Ojonugwa Jude Ochalifu Feb 11 '14 at 14:47
  • 1
    @OjonugwaOchalifu Check [this](http://stackoverflow.com/a/12433138) answer to check an example of shared preference across multiple activities. :) – MysticMagicϡ Feb 11 '14 at 15:12
0

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.

Ojonugwa Jude Ochalifu
  • 26,627
  • 26
  • 120
  • 132