Skip to content Skip to sidebar Skip to footer

Using Flags In Android So To Start From The Same Activity From Where The App Was Closed

I am creating an app in which the registration pages are 3 in number. When the user registers on the first page and saves it, it goes to the second page and so on. If suppose the u

Solution 1:

Suppose your fist signup page contain email and password. If firstpage signup is successful then set data of first page in SharedPreferences like below.

AppTypeDetails is class for SharedPreferences.

 AppTypeDetails.getInstance(SignUpActivity.this).setEmail(<Your Email ID>);
 AppTypeDetails.getInstance(SignUpActivity.this).setPassword(<Your Password>);

AppTypeDetails.java

import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;

publicclassAppTypeDetails {

    privateSharedPreferences sh;

    privateAppTypeDetails() {

    }

    privateAppTypeDetails(Context mContext) {
        sh = PreferenceManager.getDefaultSharedPreferences(mContext);
    }

    privatestaticAppTypeDetails instance = null;

    /**
     * 
     * @parammContext
     * @return {@link AppTypeDetails}
     */public synchronized staticAppTypeDetailsgetInstance(Context mContext) {
        if (instance == null) {
            instance = newAppTypeDetails(mContext);
        }
        return instance;
    }

    // get usernamepublicStringgetEmail() {
        return sh.getString("email", "");
    }

    publicvoidsetEmail(String email) {
        sh.edit().putString("email", email).commit();
    }

    // get passwordpublicStringgetPassword() {
        return sh.getString("password", "");
    }

    publicvoidsetPassword(String password) {
        sh.edit().putString("password", password).commit();
    }

    publicvoidclear() {
        sh.edit().clear().commit();
    }

}

Now close your app and open again

Now check below code in splash screen.

Stringemail= AppTypeDetails.getInstance(SplashScreen.this).getEmail();
Stringpass= AppTypeDetails.getInstance(SplashScreen.this).getPassword();

if (email == null && pass == null) {

    // Open First SignUp page

} else {
    // Open Second SignUp page
}

And do continue.

If signup page 2 is successful then save data of 2nd page in SharedPreferences and check data of first page and second page in splash screen. If data of first page and second page is not null then directly open 3rd page.

For clear SharedPreferences :

Call clear() method on logout.

Solution 2:

If you want to have one start icon but want to show different activities depending on the current state (or shared preferences as suggested by @Chirag Savsani) you can implement an invisible StartupActivity that decides which activity should be visible

publicclassStartupActivity extends Activity {

    @Override
    protectedvoidonCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        if (!hasUserRegistered()) {
            startActivity(newIntent(this, RegisterUserActivity.class));
        } elseif (!hasUserLogedIn()) {
            startActivity(newIntent(this, LoginUserActivity.class));
        } else {
            startActivity(newIntent(this, MainActivity.class));
        }
        finish(); // else StartupActivity keeps open after started activity closes
    }
}

In the manifest.xml set this StartupActivity as the only "LAUNCHER" to make it appear in android program manager

<manifest...><application... ><activityandroid:name="StartupActivity" ><intent-filter><actionandroid:name="android.intent.action.MAIN" /><categoryandroid:name="android.intent.category.LAUNCHER" /></intent-filter></activity>
        ...
    </application></manifest>

Post a Comment for "Using Flags In Android So To Start From The Same Activity From Where The App Was Closed"