MH16
MH16

Reputation: 2356

How to change value of TextView of an activity depending on the different Activites calling the first activity?

I need to create a logic which is going to decrease my code size in an Android Project.Suppose I have an Activity C. Activity C have a TextView whose value is "Hello". There are two another Activities A & B. Now, if Activity A on button Click calls Activity C, the value of TextView must change to "How are You?" and if Activity B calls Activity C , TextView value will be "I am Fine".

So, my question is , How to detect which Activity is calling Activity C and change the text of TextView accordingly at runtime ?

Any kind of help will be appreciated.

Upvotes: 0

Views: 687

Answers (5)

Sergey Glotov
Sergey Glotov

Reputation: 20346

In addition to other replies. You can send just text of the message and get rid of conditional checks in Activity C.

Calling of Activity C:

Intent i = new Intent(this, ActivityC.class);
i.putExtra(ActivityC.MESSAGE_KEY, "How are you?");
startActivity(i);

or

Intent i = new Intent(this, ActivityC.class);
i.putExtra(ActivityC.MESSAGE_KEY, "I am fine");
startActivity(i);

And in ActivityC:

public final static String MESSAGE_KEY = "com.package.name.ActivityC.message";

@Override
protected void onCreate() {
    ...
    String message = getIntent().getStringExtra(MESSAGE_KEY);
    if (message != null) {
        textView.setText(message);
    }
    ...
}

Upvotes: 2

daigoor
daigoor

Reputation: 685

You need to send some data to activity C so you can handle who is calling this C activity :

Intent i = new Intent(this , C.class);
i.putExras("from" , "a");
startActivity(i);

Intent i = new Intent(this , C.class);
i.putExras("from" , "b");
startActivity(i);

On the activity C you need to read these values and check like this :

onCreate(){

String from = getIntent().getStringExtras("from");

if(from.equals("a")){
//came from a
} else if(from.equals("b")){
//came from b
}

}

Upvotes: 0

Muhannad A.Alhariri
Muhannad A.Alhariri

Reputation: 3912

You can pass data between Activities

such as

Intent intent = new Intent(A.this,C.class);

since Intent takes two params Context and Class refreing to the intended started Activity befor classing startActivity(); method just add an integer refering to the class

intent.putExtra("src",1);

in C activity

    Intent intent = getIntent();
if (intent.getExtra("src").equals("1"))
textView.setText("how are you?")
else if (intent.getExtra("src").equals("2"))
textView.setText("fine thanks")

Upvotes: 0

AAnkit
AAnkit

Reputation: 27549

You can send extra with your Intent on startactivity.

Like when you calling it from B

  add intent.PutExtra("VARIABLE NAME","called from B"); 

if called from A

 add intent.PutExtra("VARIABLE NAME","called from A");

and can get this variable value in your Activity C by

 String calledFrom = getIntent().getStringExtra("VARIABLE NAME");

you can check calledFrom string value from where it called.

Upvotes: 0

Anurag Ramdasan
Anurag Ramdasan

Reputation: 4340

u can make the calling activity send a bundle along with the intent with the name of the calling activity in it.

the called activity can then read the content of the bundle to know which activity has called it and display data accordingly.

Upvotes: 0

Related Questions