Skip to content Skip to sidebar Skip to footer

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 tw

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 = newIntent(this, ActivityC.class);
i.putExtra(ActivityC.MESSAGE_KEY, "How are you?");
startActivity(i);

or

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

And in ActivityC:

publicfinalstaticStringMESSAGE_KEY="com.package.name.ActivityC.message";

@OverrideprotectedvoidonCreate() {
    ...
    Stringmessage= 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

StringcalledFrom= 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 = newIntent(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?")
elseif (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 = newIntent(this , C.class);
i.putExras("from" , "a");
startActivity(i);

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

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

onCreate(){

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

if(from.equals("a")){
//came from a
} elseif(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?"