0

I have two activities, LoginActivity and MainActivity.

LoginActiviy is the launcher Activity, its purpose is to check whether the user is signed in or not if he's signed in; go to MainActivity.

Although I set android:noHistory="true" to LoginActivity the activity's onResume(LoginActivity) is called again when user exits(means onPause called) the program and launch it again.

Did I misunderstood what noHistory means ? if so what can I do to make the OS forget about the existence of LoginActivity?

EDIT : I tried to put this on LoginActivity's onResume , but it calls MainActivity's onCreate, which I don't want

if(!firstTime) {
    goToMainActivity();
}

LoginActivity :

public class LoginActivity extends Activity {
protected static final String PASSED_TWITTER =    "mosaed.thukair.alsafytooth.LoginActivity";
private static final String TAG = "mosaed.thukair.alsafytooth.LoginActivity";
protected static final int RESULT_BROWSER = 0;
private SharedPreferences prefs;

private Twitter twitter;
private RequestToken requestToken;
private AccessToken accessToken;
private String authUrl;

private Button login;
private boolean firstTime;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.prefs = PreferenceManager.getDefaultSharedPreferences(this);
    firstTime = true;
    if(isAuthenticated()) {
        Log.i(TAG, "splash screen");
        setContentView(R.layout.splash_screen);
        String token = prefs.getString(Constants.OAUTH_TOKEN, "");
        String tokenSecret = prefs.getString(Constants.OAUTH_TOKEN_SECRET, "");
        Log.i(TAG, "oauth login");
        OAuthLogin(token, tokenSecret);
    } else {
        setContentView(R.layout.activity_login);

        login = (Button) findViewById(R.id.connect_button);
        login.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                Log.i(TAG, "clicked");
                LoginActivity.this.setContentView(R.layout.splash_screen);
                OAuthLogin();
            }

        }); 
    }
}

private boolean isAuthenticated() {

    String token = prefs.getString(Constants.OAUTH_TOKEN, "");
    if(token.equals(""))
        return false;
    String secret = prefs.getString(Constants.OAUTH_TOKEN_SECRET, "");
    if(secret.equals(""))
        return false;

    return true;

}

private void OAuthLogin() {
    twitter = new TwitterFactory().getInstance();
    twitter.setOAuthConsumer(Constants.CONSUMER_KEY, Constants.CONSUMER_SECRET);

    new AsyncTask<Void,Void,Void>() {

        @Override
        protected Void doInBackground(Void... params) {
            try {
                requestToken = twitter.getOAuthRequestToken(Constants.CALLBACK_URL);
                authUrl = requestToken.getAuthenticationURL();
                Intent myIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(authUrl));
                myIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | 
                        Intent.FLAG_ACTIVITY_NO_HISTORY | Intent.FLAG_FROM_BACKGROUND);
                Log.i(TAG, "open browser");
                LoginActivity.this.startActivity(myIntent);
            } catch (TwitterException e) {
                e.printStackTrace();
            }
            return null;
        }

    }.execute();
}

private void OAuthLogin(final String token, final String tokenSecret) {
    twitter = new TwitterFactory().getInstance();
    twitter.setOAuthConsumer(Constants.CONSUMER_KEY, Constants.CONSUMER_SECRET);

    new AsyncTask<Void,Void,Void>() {

        @Override
        protected Void doInBackground(Void... params) {
            AccessToken accessToken = new AccessToken(token, tokenSecret);
            twitter.setOAuthAccessToken(accessToken);
            return null;
        }

        @Override
        protected void onPostExecute(Void param) {
            goToMainActivity(twitter);
        }

    }.execute();
}

@Override
protected void onResume() {
    super.onResume();
    Log.i(TAG, "onResume");
    if ((this.getIntent() != null) && (this.getIntent().getData() != null)) {
        setContentView(R.layout.splash_screen);

        new AsyncTask<Void,Void,Void>() {

            @Override
            protected Void doInBackground(Void... params) {
                Uri uri = LoginActivity.this.getIntent().getData();
                afterBrowser(uri);
                return null;
            }

            @Override
            protected void onPostExecute(Void uri) {
                storeAccessToken();
                goToMainActivity(twitter);
            }

        }.execute();
    } else if(!firstTime) {
        goToMainActivity(twitter);
    }
}

private void afterBrowser(Uri uri) {
    String verifier = uri.getQueryParameter("oauth_verifier");
    String token = uri.getQueryParameter("oauth_token");
    try {
        twitter = new TwitterFactory().getInstance();
        twitter.setOAuthConsumer(Constants.CONSUMER_KEY, Constants.CONSUMER_SECRET);
        requestToken = new RequestToken(token, Constants.CONSUMER_SECRET);
        accessToken = twitter.getOAuthAccessToken(requestToken,
                        verifier);
        twitter.setOAuthAccessToken(accessToken);
    } catch (TwitterException ex) {
        Log.e(TAG, "" + ex.getMessage());
    }
}

private void storeAccessToken() {
    prefs.edit()
        .putString(Constants.OAUTH_TOKEN, accessToken.getToken())
        .putString(Constants.OAUTH_TOKEN_SECRET, accessToken.getTokenSecret())
        .commit();
}

private void goToMainActivity(Twitter twitter) {
    firstTime = false;
    Intent myIntent = new Intent(this, MainActivity.class);
    MyApplication.getInstance().setTwitter(twitter);
    startActivity(myIntent);
}

}

  • possible duplicate of [How does android:noHistory="true" work?](http://stackoverflow.com/questions/11836080/how-does-androidnohistory-true-work) – eikooc Jun 15 '14 at 19:35
  • @eikooc no, he's asking about the stack of activities , in my situation i don't care about the stack as i care of calling First Acitiy's onResume –  Jun 15 '14 at 19:40
  • Woah the question also seems to have changed a lot since I marked that – eikooc Jun 15 '14 at 19:43
  • post your `LoginActivity` codes – frogatto Jun 15 '14 at 19:49

2 Answers2

0

android:noHistory Whether or not the activity should be removed from the activity stack and finished (its finish() method called) when the user navigates away from it and it's no longer visible on screen — "true" if it should be finished, and "false" if not. The default value is "false". A value of "true" means that the activity will not leave a historical trace. It will not remain in the activity stack for the task, so the user will not be able to return to it.

This attribute was introduced in API Level 3.

Quoting the documentation, "it's finish() method called", have you tried finishing the activity yourself?

noHistory = true means once the activity is finish() for that user session, the user will never see it again, however, if the activity is just being paused without finishing, then it will be restarted when going back to it. Before you go to the main activity, just finish() it, if thats your desired behavior.

Ryhan
  • 1,815
  • 1
  • 18
  • 22
  • i tried it , i called finish() myself , but didn't solve anything, it made the firstTime variable also re-initialized which won't let me use my half-solution. –  Jun 15 '14 at 19:45
  • add the finish() in the onPause() method also – Ryhan Jun 15 '14 at 19:48
  • i can't . LoginActivity will open another activity (web browser) . –  Jun 16 '14 at 20:09
0
if(!firstTime) {
    goToMainActivity();
    finish();
}

What no history does is that it doesn't let that certain activity register in the stack of past activities, it doesn't allow it to skip parts of the Activity lifecycle.

If you don't want certain code not to execute then you should do something like:

Login Activity:

if(!firstTime) {
    Intent intent = new Intent(LoginActivity.this, MainActivity.class);
    intent. putExtra("skip", true);
    finish();
}

Main Activity: (inside onCreate)

if(!getIntent().getBundle().getBoolean("skip", false)) {
    //You code that you don't want
}

This is the activity lifecycle I hope it's beneficial to you:

Activity Life Cycle

Shereef Marzouk
  • 3,282
  • 7
  • 41
  • 64
  • nothing happened .. the same. also it will call the MainActivity's onCreate .. i don't want that , i want to call onResume . –  Jun 15 '14 at 19:45
  • Marouk ... i tried that, but MainActiviy's variables and objects are re-initialized .. i don't want that .. i want to call MainActivity's onResume –  Jun 16 '14 at 22:33