studentknight
studentknight

Reputation: 163

how to decide which activity we came from?

Hello Old sports !

To the point, I have 3 activity as follows :

-Activity A

-Activity B

-Activity C

in activity A I create an intent to go to activity C:

Intent intent=new Intent(getActivity(),C.class);
startActivity(intent);

in activity B I create intent to go to activity C too:

Intent intent=new Intent(getActivity(),C.class);
startActivity(intent);

in activity C I am going to do something different if it's from A and B.

The question is what is the best practice on 'how to let activity C know whether activity A or B that is calling ?

-sorry lack English, greeting from bali..

Upvotes: 7

Views: 3191

Answers (7)

The Accepted Answer and other answers works well for 4-5 classes. But, if you have more than 20 or 30 activities then you can use the below code:

In your A or B class:

Intent intent = new Intent(this, C.class);
intent.putExtra("flag", A.class); // or intent.putExtra("flag", B.class);
startActivity(intent);

Now, In your C class declare a global object as:

public Class classPreviousActivity;

Then in onCreate():

try {
    classPreviousActivity = (Class<Activity>) getIntent().getExtras().getSerializable("flag");
} catch (Exception e) {
    e.printStackTrace();
}

At the End:

startActivity(new Intent(C.this, classPreviousActivity));

Using the above code, you can go back to the activity you came from.

Upvotes: 0

Santhosh
Santhosh

Reputation: 1867

I would like to create static variable boolean in your C activity and then pass the value of static variable with C as true for A activity and false as B activity . Before staring the C activity i wll update the static variable in C activity

Ex

C.fromActivity=true;
Intent intent=new Intent(getActivity(),C.class);
startActivity(intent);

Upvotes: 1

Looking Forward
Looking Forward

Reputation: 3585

What you can do here is, you can pass one value say flag = "A" when it is from Activity A and flag = B when it is from Activity B via Intent and get that value in Activity C ...

In Activity A

Intent intent = new Intent(this, C.class);
intent.putExtra("flag", "A");
startActivity(intent);

In Activity B

Intent intent = new Intent(this, C.class);
intent.putExtra("flag", "B");
startActivity(intent);

In Activity C

    Intent intent = getIntent();
    String checkFlag= intent.getStringExtra("flag");

if(checkFlag.equals("A");
// It is from A

else
// It is from B

Upvotes: 5

Bhargav Jhaveri
Bhargav Jhaveri

Reputation: 2183

From Class A to Class C:

Intent intent=new Intent(getActivity(),C.class);
intent.putExtras("ActivityName","A")
startActivity(intent);

From Class B to Class C:

Intent intent=new Intent(getActivity(),C.class);
intent.putExtras("ActivityName","B")
startActivity(intent);

And in Class C you write,

 String strActivityName = getIntent().getStringExtra("ActivityName");

Based on the value you receive, you can decide the calling activity and accroding perform your customization over there.

Upvotes: 1

sergej shafarenka
sergej shafarenka

Reputation: 20426

You have two options.

1) Use startActivityForResult() when calling activity C. In this case you can use getCallingActivity() method to find this out.

2) Add an extra to intent when calling activity C.

// in activity A
Intent intent = new Intent(this, C.class);
intent.putExtra("calling", "A");
startActivity(intent);

// in activity B
String callingActivity = getIntent().getStringExtra("calling"); 

Upvotes: 1

InnocentKiller
InnocentKiller

Reputation: 5234

Also you can take static variable in your both activity and then pass the value of static variable with intent.

Like ex.

public static int a = 1;

and

public static int b = 2;

and then pass this with intent

and in your last activity get the value of static variable and done. You will be able to know that from which activity you came from.

Upvotes: 2

Nathua
Nathua

Reputation: 8836

You can pass a parameter with intent to another activity, in that way you can know which activity started.

Intent intent=new Intent(getActivity(),C.class);
intent.putString("activity","A");  // and same goes for B
startActivity(intent);

and in Activity C,

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

Upvotes: 6

Related Questions