I have an activity, which is a singleTask:
public class MyActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); System.out.printf("### MyAcitvity.onCreate: " + getIntent().getExtras()); } @Override protected void onResume() { super.onResume(); System.out.println("### MyActivity.onResume: " + getIntent().getExtras()); } public void toNext(View v) { startActivity(new Intent(this, AaaActivity.class)); } }
and:
<activity android:name=".MyActivity" android:launchMode="singleTask" />
No it has been started, and it started another activity called AaaActivity
.
public class AaaActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.aaa); } public void jumpToFirst(View v) { Intent intent = new Intent(this, MyActivity.class); intent.putExtra("aaa","bbb"); startActivity(intent); } }
In the later one, there is a button which will trigger the jumpToFirst
method to start the first one, and pass a data pair aaa=bbb
. Since MyActivity
is singleTask
, it won't create a new one, instead, it will just back to the existing MyActivity
instance.
My question is: how to get the passing extra data from AaaActivity
in MyActivity
?
You can see I have logged onCreate
and onResume
methods, but it prints null extra data.
11-16 08:44:17.833: INFO/System.out(5171): ### MyAcitvity.onCreate: null 11-16 08:44:19.417: INFO/System.out(5171): ### MyActivity.onResume: null 11-16 08:44:26.041: INFO/System.out(5171): ### MyActivity.onResume: null 11-16 08:44:28.897: INFO/System.out(5171): ### MyActivity.onResume: null
Using putExtra() and getExtras() in android putExtra() adds extended data to the intent. It has two parameters, first one specifies the name which of the extra data,and the second parameter is the data itself. getExtra() fetches data which was added using putExtra() in the following way: Bundle extras= getIntent().
Try setting the launchMode type of your MyActivity
as singleTop and then override the following method to look for the new intent:
@Override protected void onNewIntent(Intent intent) { super.onNewIntent(intent); setIntent(intent); //now getIntent() should always return the last received intent }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With