Activity在onResume里调用getIntent()拿不到数据

有时候MainActivity一般都是设置启动模式为:singleTop,也就是说如果MainActivity处于栈顶位置的话就不会从新创建实例,也就是不会调用Activity的onCreate方法,会调用onResume方法,所以从通知栏直接打开MainActivity就会在onResume里面拿不到intent携带的数据,处理方法如下:
 /**
     *
     * 重写此方法,加上setIntent(intent);否则在onResume里面得不到intent
     * @param intent intent
     */
    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
    }
然后在onResume里面就可以拿到intent的数据了
 @Override
    protected void onResume() {
        super.onResume();
        Intent intent = getIntent();
        String message = intent.getStringExtra("message");
        //操作。。。。。。
    }


你可能感兴趣的:(Android,随笔,随笔)