android - 使用 Intent.FLAG_ACTIVITY_SINGLE_TOP 和 CLEAR_TOP 时没有通过额外功能

标签 android android-intent android-activity

doneButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            if(!imgUriList.isEmpty())
            {
                    Intent intent = new Intent(getBaseContext(), createAdActivity.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP|Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, imgUriList);
                    startActivity(intent);


            }
        }
    });

This is the code I have used in my second activity to send extras to the first activity.

I want to keep the first activity as it is when coming back from the second activity. Therefore I have used _SINGLE_TOP,CLEAR_TOP FLAGS.

Without the Flags it works well. But After using the extras extras become null. This is the Code I have used in my first activity.

 @Override
    protected void onResume() {
        super.onResume();
        Toast.makeText(this,"IN RESUME ",Toast.LENGTH_LONG).show();
        imageUris = getIntent().getParcelableArrayListExtra(Intent.EXTRA_STREAM);
        if(getIntent().hasExtra(Intent.EXTRA_STREAM)) {
            if (!imageUris.isEmpty()) {
                createAdViewPagerAdapter viewpager = new createAdViewPagerAdapter(this,imageUris);
                photoViewer.setAdapter(viewpager);
            }
        }
    }

Since the first activity also uses some extras sent from MainActivity I have overrides the onNewIntent()method as well.

@Override
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
}

最佳答案

使用 getIntent(),您将获得旧的 Intent,您需要处理 onNewIntent() 的 intent 参数。您可以使用 setIntent(intent) 它将更改 getIntent() 返回的 Intent 。要确保对 getIntent() 的任何进一步调用将返回最新的 Intent ,请使用 setIntent() .

 @Override
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    setIntent(intent);
   // Do further stuff here
}

关于android - 使用 Intent.FLAG_ACTIVITY_SINGLE_TOP 和 CLEAR_TOP 时没有通过额外功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50317095/

相关文章:

java - 尝试将文本设置为 fragment 中 Activity 中的 TextView 时出现 NullPointerException

android - Activity 从 MapActivity 切换到 Activity

java - 如何使用贝塞尔曲线近似椭圆以填充给定的矩形?

database - 如何将静态数据放入 Android 中的 SQLite 数据库?

android - 如何检查 WiFi 连接是自动的还是在 android 中手动建立的?

java - 如何在android中的字符串中隐藏应用程序网址

java - 如何让我的应用程序可以从其他应用程序调用

android - 无法获取用户使用我的 Android 应用程序的国家/地区

android - 如何从 OnActivityResult 更新 RecyclerView 元素

java - 尽管 Eclipse 中的一切都正确,但 Android ActivityNotFoundException