Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extras not getting passed when using Intent.FLAG_ACTIVITY_SINGLE_TOP and CLEAR_TOP

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);
}
like image 234
jhashane Avatar asked Oct 23 '25 19:10

jhashane


1 Answers

With getIntent() you will get the old Intent you need to process the intent argument of onNewIntent(). You can use setIntent(intent) it will Change the intent returned by getIntent(). To make sure any further call to getIntent() will return the latest intent use setIntent() .

 @Override
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    setIntent(intent);
   // Do further stuff here
}
like image 162
ADM Avatar answered Oct 26 '25 09:10

ADM