How to Find Intent Source in Android

How to find Intent source in Android?

A better way to do this would be to use Intent extras to pass parameters to the receiver.

how to check which Intent started the activity?

try as:

Intent intent = new Intent();  
intent.setClass(A.this,Receiveractivity.class);
intent.putExtra("Uniqid","From_Activity_A");
A.this.startActivity(intent);

Intent intent = new Intent();
intent.setClass(B.this,Receiveractivity.class);
intent.putExtra("Uniqid","From_Activity_B");
B.this.startActivity(intent);

Intent intent = new Intent();
intent.setClass(C.this,Receiveractivity.class);
intent.putExtra("Uniqid","From_Activity_C");
C.this.startActivity(intent);

and in onCreate of main Activity:

//obtain  Intent Object send  from SenderActivity
Intent intent = this.getIntent();

/* Obtain String from Intent */
if(intent !=null)
{
String strdata = intent.getExtras().getString("Uniqid");
if(strdata.equals("From_Activity_A"))
{
//Do Something here...
}
if(strdata.equals("From_Activity_B"))
{
//Do Something here...
}
if(strdata.equals("From_Activity_C"))
{
//Do Something here...
}
........
}
else
{
//do something here
}

use putExtra for sending Unique key from Each Activity to identify from which Activity intent is Received

How to get the sender of an Intent?

There may be another way, but the only solution I know of is having Activity A invoke Activity B via startActivityForResult(). Then Activity B can use getCallingActivity() to retrieve Activity A's identity.

How do I get extra data from intent on Android?

First, get the intent which has started your activity using the getIntent() method:

Intent intent = getIntent();

If your extra data is represented as strings, then you can use intent.getStringExtra(String name) method. In your case:

String id = intent.getStringExtra("id");
String name = intent.getStringExtra("name");


Related Topics



Leave a reply



Submit