thecodeseeker
thecodeseeker

Reputation: 1

How to clear history populated in application progress

  1. How to clear history which gets populated in application progress,Here m creating a simple login application,where i have two activites LoginActivity and MainActivity, I am using shared preferences to check whether user is logged in or not,I have made application class which will be called every time when application is started, i have added these lines of code in manifest

android:name=".FrontScreenApplication"

FrontScreenApplication.class is as follows

public class FrontScreenApplication extends Application {

    public static final String PREFS_NAME = "ApplicationFirstTime";
    int PRIVATE_MODE = 0;
    private static final String PREF_NAME2 = "LoginPref";
    private static final String IS_LOGIN = "IsLoggedIn";

    @Override
    public void onCreate() {
        // TODO Auto-generated method stub
        super.onCreate();

        SharedPreferences pref = getSharedPreferences(PREFS_NAME, 0);
        if (!pref.getBoolean("isFirstTime", false)) {
            SharedPreferences.Editor editor = pref.edit();
            editor.putBoolean("isFirstTime", true);
            editor.commit();
            Intent intent = new Intent(this, LoginActivity.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK);                     
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(intent);

        } else {

            SharedPreferences prefs = getSharedPreferences(PREF_NAME2,MODE_PRIVATE);
            boolean result = prefs.getBoolean(IS_LOGIN, false);
            if (result) {
                Intent intent = new Intent(this, MainActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK);                     
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);
            } else {
                Intent intent = new Intent(this, LoginActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK);                     
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);
            }

        }
    }

Main class in manifest is MainActivity so it is called first,when we go back, each activity is shown in backstack LoginAcivity and MainActivity.

Upvotes: 0

Views: 52

Answers (2)

Chandra Sharma
Chandra Sharma

Reputation: 1340

You have to set intent flag Intent.FLAG_ACTIVITY_CLEAR_TASK.This will resolve your issue.

      intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK)

OR

In AndroidManifest.xml file put android:noHistory="true" for FrontScreenApplication activity.

OR

You can do it by another way ,simply finish the FrontScreenApplication activity after starting the intent.

        Intent intent = new Intent(Login.this, ReadPhoneNo.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK);                     
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
        finish();

Upvotes: 1

MUHAMMAD WASIM
MUHAMMAD WASIM

Reputation: 191

We use FLAG_ACTIVITY_CLEAR_TASK flag in the intent exact syntax is as below:

intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);

Upvotes: 0

Related Questions