Search code examples
androidandroid-activity

Android get previous activity


I have 2 activities: Activity1 and Activity2. In each of this activities there is a button that leads me to a third activity (MainActivity). In MainActivity I want to know from which activity page was called.


Solution

  • You can use the putExtra attribute of the Intent to pass the name of the Activity.

    Calling Activity,

    Intent intent = new Intent(this, Next.class);
    intent.putExtra("activity","first");
    startActivity(intent);
    

    Next Activity,

    Intent intent = getIntent();
    String activity = intent.getStringExtra("activity");
    

    Now in the string activity you will get the name from which Activity it has came.