Failed to get additional intentions.

I have activity that I use in animated modes, so I need to do things like this:

Intent i = new Intent(MainListActivity.this,MainActivity.class); extras.putInt("id", c.getId()); extras.putInt("mode", AREA_MODE); i.putExtra("extras", extras); startActivity(i); 

and in onCreate :

  Intent i = this.getIntent(); extras = i.getBundleExtra("extras"); if(extras!=null){ id = extras.getInt("id", -1); mode = extras.getInt("mode", COUNTRY_MODE); } 

But preemptive actions are always zero. Am I missing something? Is there any way to do this?

EDIT: For some reason, the getIntent() method returns the previous Intent , which in my case has no additional (main intent). I am trying to understand why.

+4
source share
6 answers

I did not find the cause of this problem, so I refused to use one action. I create other actions that expand the scope of the issue, and it started working (with both approaches). Thank you all for your help.

0
source

Try it like this:

In one action:

 Intent i = new Intent(); Bundle bundle = new Bundle(); bundle.putInt("id", c.getId()); bundle.putInt("mode", AREA_MODE); i.putExtras(bundle); 

In another action:

 Bundle extras = getIntent().getExtras(); if (extras != null) { id = extras.getInt("id", -1); mode = extras.getInt("mode", COUNTRY_MODE); } 
+3
source

It should be.

 Intent i = new Intent(MainListActivity.this ,MainActivity.class); i.putExtra("id", c.getId()); i.putExtra("mode", AREA_MODE); startActivity(i); 

and

 Intent i = this.getIntent(); id = i.getIntExtra("id", -1); mode = i.getIntExtra("mode", COUNTRY_MODE); 
+2
source

Yeah! I just debugged this problem. It turns out that this is due to the fine detail recorded in PendingIntent .

  • A PendingIntent is a reference to a system token. Therefore, even if the sending process restarts, restoring a similar intent will restore the original PendingIntent. Then the sender can cancel or change it.
  • Similar intention == same operation, action, data, categories, components and flags. Extra not counted!
  • Skills that differ only in their Extras all extract the same token.

A common mistake people make is to create several PendingIntent objects with Intents that change only in their "superfluous" content, expecting to receive a different PendingIntent each time. This is not happening.

Fix 1: Make PendingIntent distinct. It must have different operations, actions, data, categories, components or flags - or request integers of code.

Fix 2: Use FLAG_CANCEL_CURRENT or FLAG_UPDATE_CURRENT to cancel or modify any existing PendingIntent.

These two corrections give slightly different results. In any case, your new features should go through.

+2
source

You do not add any additions to the intention in this way:

  i.putExtra("extras", extras); 

This adds additional intentions in addition. Your β€œadd-ons” are in a non-standard place in the intent. If your onCreate() code was supposed to return an object associated with "extra" and look at it, you will find your "extra".

The standard way is to populate the bundle and add it to the intent via putExtras :

 i.putExtras(bundle); 

or use convenient methods like putExtra :

 i.putExtra("mode", AREA_MODE); 
0
source

Source: https://habr.com/ru/post/1338425/


All Articles