Search code examples
androidandroid-intentandroid-activityonclickonbackpressed

How can i return to specific previous activity on button pressed


I have a next problem. Let's assume i have three Activities : A, B and C. I can come from A to C, and from B to C. Then when i pressed button in Activity C i want to go back to the previous Activity, or A or B. I don't know which one was previous.

in A Activity :

Intent intent=new Intent(this, C.class);
Intent.PutExtra("FROM_A", "A")
StartActivity;

in B Activity :

Intent intent=new Intent(this, C.class);
Intent.PutExtra("FROM_B", "B")
StartActivity;

in Activity C:

public void onClick(View v) {
    onBackPressed();    // automatic know which Activity i came from(A or B)
} 

Please Help Me. What's Wrong?

Thank you for your answers.


Solution

  • in Activity A :

        Intent intent=new Intent(this, C.class);
        intent.PutExtra("FROM_A", "A")
        startActivity(intent);
        finish();  
    

    in Activity B :

        Intent intent=new Intent(this, C.class);
        intent.PutExtra("FROM_B", "B")
        startActivity(intent);
        finish();  
    

    in Activity C:

        public void onClick(View v) {
          Intent intent=new Intent(this, YourActivity.class);
          startActivity(intent);
          finish();  
        }