How To Change Value Of TextView Of An Activity Depending On The Different Activites Calling The First Activity?
Solution 1:
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);
}
...
}
Solution 2:
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.
Solution 3:
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.
Solution 4:
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")
Solution 5:
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
}
}
Post a Comment for "How To Change Value Of TextView Of An Activity Depending On The Different Activites Calling The First Activity?"