How to decide which activity we came from?

Hello old sport!

By the time, I have 3 actions as follows:

-Activity A

-Activity B

-Activity C

in step A I create the intention to move to activity C:

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

in action B I create the intention to go to activity C too:

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

in action C I'm going to do something else if it is from A and B.

The question is, what is the best practice regarding how to let activity C know if activity A or B is active?

-sorry missing english greeting from bali ..

+4
source share
6 answers

, : flag = "A", Activity A flag = B, Activity B Intent, Activity C...

A

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

B

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

C

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

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

else
// It is from B
+4

, , , .

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

C,

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

, .

ex.

public static int a = 1;

public static int b = 2;

. , .

+2

.

1) C startActivityForResult(). getCallingActivity(), .

2) 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"); 
+1

A C:

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

B C:

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

C

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

, , accroding .

+1

boolean C C true A false B. , C, C

Ex

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

All Articles