Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to check which Intent started the activity?

Tags:

android

I have many activities. Each one of them has an intent which refers to the same activity. Is there a way to find out which intent started the activity?

like image 358
Fawzinov Avatar asked Jul 17 '12 19:07

Fawzinov


People also ask

Which type of intent should we use to start an activity?

To start an activity, use the method startActivity(intent) . This method is defined on the Context object which Activity extends. The following code demonstrates how you can start another activity via an intent. # Start the activity connect to the # specified class Intent i = new Intent(this, ActivityTwo.

Which is intent type is used to navigate between activities?

An intent is to perform an action on the screen. It is mostly used to start activity, send broadcast receiver,start services and send message between two activities. There are two intents available in android as Implicit Intents and Explicit Intents. Here is a sample example to start new activity with old activity.


2 Answers

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

like image 143
ρяσѕρєя K Avatar answered Sep 28 '22 09:09

ρяσѕρєя K


You didn't provide any context, so here's one general approach.

Put an extra into each Intent type, like a unique int or String:

intent.putExtra("Source", "from BroadcastReceiver");

and use:

String source = getIntent().getStringExtra("Source");
like image 24
Sam Avatar answered Sep 28 '22 10:09

Sam