简体   繁体   English

onResume调用了noHistory活动

[英]onResume called on noHistory activity

I have two activities, LoginActivity and MainActivity . 我有两个活动, LoginActivityMainActivity

LoginActiviy is the launcher Activity, its purpose is to check whether the user is signed in or not if he's signed in; LoginActiviy是启动器活动,其目的是检查用户是否已登录; go to MainActivity . 转到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. 尽管我将android:noHistory="true"LoginActivity但是当用户退出(意味着onPause被调用)程序并再次启动时,活动的onResume(LoginActivity)被再次调用。

Did I misunderstood what noHistory means ? 我是否误解了noHistory意思? if so what can I do to make the OS forget about the existence of LoginActivity ? 如果是这样,我该怎么做才能使操作系统忘记LoginActivity的存在?

EDIT : I tried to put this on LoginActivity 's onResume , but it calls MainActivity 's onCreate , which I don't want 编辑:我试图把它放在LoginActivityonResume ,但它调用MainActivityonCreate ,我不想要

if(!firstTime) {
    goToMainActivity();
}

LoginActivity : 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);
}

} }

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. android:noHistory当用户离开活动并且在屏幕上不再可见时,是否应该从活动堆栈中删除该活动并完成(调用了finish()方法);如果应该结束,则为“ true”;以及如果不是,则为“ false”。 The default value is "false". 默认值为“ false”。 A value of "true" means that the activity will not leave a historical trace. 值为“ true”表示该活动将不会留下历史痕迹。 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? 引用文档“调用了finish()方法”,您是否尝试过自己完成活动?

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. noHistory = true表示,一旦该活动在该用户会话中完成(),用户将再也看不到它,但是,如果只是暂停该活动而未完成,则将在返回时重新启动它。 Before you go to the main activity, just finish() it, if thats your desired behavior. 如果您想要的是行为,那么在进入主要活动之前,只需完成()它即可。

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) 主要活动:(在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: 这是活动生命周期,希望对您有帮助:

活动生命周期

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM