【问题标题】:How to capture the new Intent in onNewIntent()?如何在 onNewIntent() 中捕获新的 Intent?
【发布时间】:2011-01-24 21:15:19
【问题描述】:

我想将一个新的但不同的 Intent 传递给一个 Activity。您如何区分新的 Intent 和以前的 Intent?什么样的代码进入 onNewIntent()?举个例子会很有帮助。

【问题讨论】:

    标签: android android-intent


    【解决方案1】:

    新意图是onNewIntent(Intent) 的一部分。原来的Intent 仍然可以通过getIntent() 获得。

    您将所需的任何代码放入onNewIntent,以便使用新参数更新 UI;可能与您在 onCreate 中所做的类似。

    此外,您可能希望在 onNewIntent 中调用 setIntent(intent),以确保在 Activity 生命周期内将来对 getIntent() 的调用获得最新的 Intent 数据。

    【讨论】:

    • 谢谢,所以当调用活动时,控件直接转到onNewIntent()?
    • 如果你触发一个 Intent 导致现有的 Activity 被重用,是的。
    • @ChristopherOrr 我的 onNewIntent() 永远不会启动,你能和我分享一些想法吗?
    【解决方案2】:

    意图如何到达您的活动取决于启动模式(请参阅http://developer.android.com/guide/topics/manifest/activity-element.html#lmode 的启动模式文档)。

    • 对于启动模式“标准”(默认),具有新意图的 startActivity 将导致具有该意图的 onCreate 到 Activity 的新实例。

    • 对于启动模式“singleTop”和“singleTask”,具有新意图的 startActivity 将导致

    a) 具有该意图的 onCreate 到活动的新实例(如果该活动未运行)[根据上述“标准”] 或 b) 对现有活动具有该意图的 onNewIntent(如果该活动已在运行)。

    对于 b),第二个意图在 onNewIntent 参数中可用。你用它做什么取决于你的应用程序。一些应用程序会忽略它,而其他应用程序会执行 setIntent() 并开始重新初始化/更新处理新的意图。

    【讨论】:

    • 目前我正在使用 singleTask,但是根据日志,这两种方法都没有在我的代码中触发... :(
    【解决方案3】:

    你调用的 Activity-Main Activity

    public class MainActivity extends Activity
    {
        public void onCreate(Bundle SavedInstanceState)
        {
        }
    
        @Override
        protected void onNewIntent(Intent intent) 
        {
            super.onNewIntent(intent);
            if(intent.getStringExtra("methodName").equals("myMethod"))
            {
                myMethod();
            }
        }
    
        public void myMethod()
        {
        }
    }
    

    您的通话活动

    代码转到上一个意图

    public class CallingActivity extends Activity
    {
        public void onCreate(Bundle savedInstanceState)
        {
            Intent i=new Intent(this,MainActivity.class);
            i.putExtra("methodName","myMethod");//goes to previous INtent
            startActivity(i);//will trigger only myMethod in MainActivity
        }
    }
    

    您的通话活动

    代码使用这些意图启动新活动

    public class CallingActivity extends Activity
    {
        public void onCreate(Bundle savedInstanceState)
        {
            Intent i=new Intent(this,MainActivity.class);
            startActivity(i);//will trigger New Activity i.e. MainActivity
        }
    }
    

    【讨论】:

    • mainActivity 中的protected void onNewIntent(Intent intent) 根据您的解释未触发。你能根据我的代码举一些例子吗?帮助将不胜感激...
    • @Vikalp Patel 您的代码非常有趣且有用,但您在代码中使用了 3 个不同版本的“mymethod”字符串。你能纠正这个吗?或者是故意的字符串的不同大小写版本。你有“Mymethod”、“mymethod”和“myMethod”
    【解决方案4】:

    【讨论】:

      【解决方案5】:

      如果您不希望您的活动在每个后续 onResume() 中重复使用新意图,我建议将意图存储在实例字段中,而不是通过 setIntent(intent)

      这样,您可以在使用 Intent 后将该实例字段重置为 null,而不会丢弃原始启动 Intent。

      我的答案中的更多详细信息: https://stackoverflow.com/a/21261404/621690

      setIntent(Intent) 被 Android 框架工程师描述为一个错误: https://groups.google.com/forum/#!topic/android-developers/vrLdM5mKeoY

      【讨论】:

        猜你喜欢
        • 2015-07-14
        • 1970-01-01
        • 1970-01-01
        • 2012-10-19
        • 1970-01-01
        • 1970-01-01
        • 2021-05-08
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多