Tirtha
Tirtha

Reputation: 351

I want to do registration of my app at the time of installation?

Problems

  1. The activity which will do registration will be shown only once
  2. After registration the control should move to next main activity

I have used following code

Below code will not meet my requirement?

Any Help will be appreciated!!

Code in Registration Activity

    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
    SharedPreferences.Editor editor=prefs.edit();
    editor.putBoolean("registration", true);
    editor.commit();

Code in Main Activity

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
    boolean regComplete =prefs.getBoolean("registration", false);
    SharedPreferences.Editor editor =prefs.edit();
    editor.putBoolean("registration", false);

    editor.commit();

    if(regComplete)
    {
       startActivity(new Intent(this, SecureXActivity.class));
    } else 
    {
       startActivity(new Intent(this, LoginActivity.class));

   }

Upvotes: 2

Views: 108

Answers (2)

Archie.bpgc
Archie.bpgc

Reputation: 24012

The Registration Activity should be like this:

public class RegistrationActivity extends Activity {

    public static SharedPreferences pref;
    public static final String PREFS_NAME = "MyPrefsFile";

    public void onCreate(Bundle savedInstanceState) {

            pref = getSharedPreferences(PREFS_NAME, 0);
                        boolean regComplete =prefs.getBoolean("registration", false);
                        if(regComplete){
                        //go to main class
                        }else{
                        //stay in the registration class
                        }
}
}

and the Main class should be:

public class MainActivity extends Activity {

    public void onCreate(Bundle savedInstanceState) {

    RegistrationActivity.pref = getSharedPreferences(PREFS_NAME, 0);
    SharedPreferences.Editor editor = settings.edit();

    editor.putBoolean("registration", true);
        // Commit the edits!
    editor.commit();
}
}

Upvotes: 1

user370305
user370305

Reputation: 109237

Just put your Registration code in SecureXActivity.class

and Check for the Registration before setContentView(), if its not done then start LoginActivity.class

And in LoginActivity.class after registration complete put these code,

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor=prefs.edit();
editor.putBoolean("registration", true);
editor.commit();

If you use this approach then I think you don't need Main Activity class..

And keep in mind this all thing done at time of your application's first run not at the time of installation.

Upvotes: 3

Related Questions