简体   繁体   English

AsyncTask中的意图putExtra(String,String)

[英]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. 我已经看到了类似的解决方案,并且使它们可以在不使用AsyncTask来创建意图的情况下工作。

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. PASSED_USERNAME是一个公共静态常量,用于保存程序包名称,就像putExtra()方法所要求的那样。 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. 永远不会有String可以提取,username的值始终为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. 我遍历了调试器,发现Eclipse IDE调试器在活动之间显示了不同的Intent ID,它们从未保持一致。 Is it possible that AsyncTask is interfereing somehow because it splits into a seperate thread? AsyncTask是否有可能因为它分裂为单独的线程而以某种方式进行了干扰?

Not sure of the exact answer for you solution. 不确定确切答案为您解决方案。 You calling startActivity on the UI since it's in postExecute(). 您在UI上调用startActivity,因为它位于postExecute()中。

If all else fails you can just save that value to a sharedpreference. 如果其他所有方法都失败,则可以将该值保存到sharedpreference。

The way you have handled the variable PASSED_USERNAME seems incorrect. 您处理变量PASSED_USERNAME的方式似乎不正确。 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. 您在某些宫殿中将其作为简单的PASSED_USERNAME使用,而在其他一些地方,将其与前缀为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. 但是getIntent()方法始终返回启动活动的第一个Intent。 If the activity remains in the background and receives a new Intent this value does not get updated automatically. 如果活动保留在后台并收到新的Intent,则该值不会自动更新。 You have to override onNewIntent(...) and manually call setIntent(...) for this to work (or do all your stuff directly there). 您必须重写onNewIntent(...)并手动调用setIntent(...)才能使其工作(或直接在此处进行所有处理)。

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. 因此,仅在您没有在onCreate()方法中运行发布的代码的情况下,请检查您是否没有错过获取感兴趣的真实意图的方法。

暂无
暂无

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

相关问题 putExtra String Intent Android - putExtra String Intent Android Intent.putExtra(String,Bundle)vs Intent.putExtra(Bundle) - Intent.putExtra(String,Bundle) vs Intent.putExtra(Bundle) Intent类型的方法putExtra(String,boolean)不适用于参数(String,CarouselDataItem) - The method putExtra(String, boolean) in the type Intent is not applicable for the arguments (String, CarouselDataItem) Intent.putExtra-如何将字符串发送到下一个活动? - Intent.putExtra - How to send a string to the next activity? 获取错误“无法对类型为Intent的非静态方法putExtra(String,String)进行静态引用” - Getting error “Cannot make a static reference to the non-static method putExtra(String, String) from the type Intent” 将putExtra意向中的URL字符串数组的选定项从活动传递到服务 - Passing selected item of string array of urls in putExtra intent from activity to service 将textView转换为Boolean以避免:Intent类型中的方法putExtra(String,boolean)不适用于参数 - Converting textView to Boolean to avoid: The method putExtra(String, boolean) in the type Intent is not applicable for the arguments 如何对字符串使用putExtra和putIntent - How to use putExtra and putIntent for string putextra和getextra不将字符串传递给下一个活动 - putextra and getextra not passing string to next activity putExtra如何发送到新的活动数组列表<STRING []> - putExtra How send to new Activity Arraylist <STRING []>
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM