Harsha M V
Harsha M V

Reputation: 54949

How to find out which activity has been loaded

I have 3 activities. A, B and C. From A to C and B to C. How can i from Activity C find out which activity was loaded previously and refereed to this Activity.

Upvotes: 0

Views: 355

Answers (2)

Eric Nordvik
Eric Nordvik

Reputation: 14746

If you start your activity C with startActivityForResult instead of startActivity, you have access to the calling Activity:

Start Activity C like this:

Intent intent = new Intent(this, C.class);
int requestCode = 1; // Or some other integer
startActivityForResult(intent, requestCode);

in Activity C:

onCreate(...) {
  String callingClassName = getCallingActivity().getClass().getSimpleName();
}

Upvotes: 1

Juri
Juri

Reputation: 32900

You could handle this through intent bundles. Basically in Activity A or B you launch Activity C as follows:

Intent launchIntent = new Intent(this, ActivityC.class);
launchIntent.putExtra("originActivity", this.getClass().getName());

In Activity C, you retrieve it like

public class ActivityC extends Activity{
   onCreate(...){
      Intent callingIntent = getIntent();

      String originActivity = callingIntent.getStringExtra("originActivity");

   }
}

Now I passed the activity name as string, you may include it in some more convenient way, using constants or something like that. You can look it up here.

Upvotes: 3

Related Questions