简体   繁体   中英

Intent putExtra(String,String) inside of AsyncTask

I'm simply trying to carry a string onto the next activity without having to define an entire object for the task. I've seen similar solutions and gotten them to work BUT without using AsyncTask to create the intent.

protected void onPostExecute(Boolean result) {
        if (loggedIn && hasPin) {

            Intent intent = new Intent(UniteActivity.this,
                    WebViewActivity.class);
            intent.putExtra(PASSED_USERNAME, passUser);
            startActivity(intent);
        }
        if (loggedIn && !hasPin) {

            Intent intent = new Intent(UniteActivity.this,
                    CreatePinActivity.class);
            intent.putExtra(PASSED_USERNAME, passUser);
            startActivity(intent);

PASSED_USERNAME is a public static constant to hold the package name, just as the putExtra() method requires. I then try to pull the value out in the next activity.

Intent extras = getIntent();

            String username = extras.getStringExtra(UniteActivity.PASSED_USERNAME);

            // carry username to next activity
            Intent intent = new Intent(CreatePinActivity.this,WebViewActivity.class);
            intent.putExtra(PASSED_USERNAME, username);
            startActivity(intent);

There is never a String to pull out, the value of username is always null. I've gone through the debugger and found that the Eclipse IDE debugger shows different intent ID's between the activities, they are never consistant. Is it possible that AsyncTask is interfereing somehow because it splits into a seperate thread?

Not sure of the exact answer for you solution. You calling startActivity on the UI since it's in postExecute().

If all else fails you can just save that value to a sharedpreference.

The way you have handled the variable PASSED_USERNAME seems incorrect. You have used it in some palaces as simple PASSED_USERNAME whereas in some other places you have used it with the class named prefixed UniteActivity.PASSED_USERNAME. Since it is a Public Static Constant always use it prefixed with the class name.

I don't know if this applies to your problem, because I can't see from your code snippet if the intermediary activity is freshly created or not.

BUT : the getIntent() -method always returns the first Intent that started the activity. If the activity remains in the background and receives a new Intent this value does not get updated automatically. You have to override onNewIntent(...) and manually call setIntent(...) for this to work (or do all your stuff directly there).

So just for the case that you do not run your posted code in the onCreate() method please check if you did not miss to fetch the real intent you are interested in.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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