Android. The intent only the first time send what I want. Why? - android

I use intent to send data from activity to other, when I call the 2nd activity repeatedly nothing happens,
The 1st intent:
`
Intent intent = new Intent(this, EditActivity.class);
intent.putExtra("1stNameIntent", firstName);
intent.putExtra("2ndNameIntent", lastName);
intent.putExtra("mailIntent", mail);
intent.putExtra("mobileIntent", mobile);
intent.putExtra("idIntent", intId);
startActivity(intent);`
//There is no problem with values
...
The 2nd activity:
...
`
public String firstNameHint = null;
public String lastNameHint = null;
public String mailHint = null;
public String mobileHint = null;
public int id;
public String idStr = null;`
`protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_edit);
idStr = getIntent().getExtras().getString("idIntent");
id = Integer.parseInt(idStr);
firstNameHint = getIntent().getExtras().getString("1stNameIntent");
firstName = (EditText) findViewById(R.id.firstName_ID);
firstName.setText(firstNameHint);
firstName.setHint(firstNameHint);
lastNameHint = getIntent().getExtras().getString("2ndNameIntent");
lastName = (EditText) findViewById(R.id.lastName_ID);
lastName.setText(lastNameHint);
lastName.setHint(lastNameHint);`
...
I cant find out the problem. After the first use of 2nd activity only the default values appears.

Maybe your onCreate Activity is not being called twice, depending on the 'launchMode' of the activity.
Try setting text in your onStart method.
Hope this helps.

implement onNewIntent(Intent i) in your second activity.
If the second activity has already been created (as it has), any future new intents will go to this onNewIntentMethod(...)
So, pull out your intent reading code and the code to set views into another method, and have both onCreate and onNewIntent call this new method that actually does the work.

use
intent i = getIntent();
stringvariable = i.getStringExtra("1stNameIntent");
see this Using putExtra to pass values to intent service

you should make sure that you are calling putExtra() every time you are starting the activity. because I think it only works with one intent.
also try adding Log.d() on the onCreate() of the second activity to know when it is called.

Related

how to link a new activity in android studio using a clickable text

Please how do I link a new activity from my main activity using a clickable text. I have set the text to clickable from my main.xml but I don't know how to call the new activity from my MainActivity.java class. I know I have to use this code "textView.setOnClickListener(new View.OnClickListener());" I found in a similar question, but I don't know how and where to place it on my MainActivity.java class so that it calls a the next activity I named display
Check out Intent. You use these to start new activities or services within your application.
You're correct in that you have to assign an OnClickListener interface to your text, after you made it clickable. In the interface's onClick() method you would need to do something like this.
For example:
#Override
public void onClick(View v) {
// Create the intent which will start your new activity.
Intent newActivityIntent = new Intent(MainActivity.this, NewActivity.class);
// Pass any info you need in the next activity in your
// intent object.
newActivityIntent.putExtra("aString", "some_string_value");
newActivityIntent.putExtra("anInteger", some_integer_value);
// Start the new activity.
startActivity(newActivityIntent);
}
In the next activity, you can retrieve the intent used to start it, so that you'll have access to the data you passed from the first activity, like so:
#Override
public void onCreate(Bundle savedInstanceState) {
// Get the intent that started this activity.
Intent startingIntent = getIntent();
// Retrieve the values.
String aString = startingIntent.getStringExtra("aString");
Integer anInteger = startingIntent.getIntExtra("anInteger", 0); // 2nd param is the default value, should "anInteger" not exist in the bundle.
// Use the values to your hearts content.
}
Hope that helps.

Passing data from two activities to a third activity in android

have total 3 activites. I pass the data from the first activity like this:
Intent intent = new Intent(getActivity(), Movie_rev_fulldis_activity.class);
intent.putExtra("mov_pos", position + "");
startActivity(intent);
this working fine all data visible to my second activity but i want to display
one filed item to third activity when i click second activity image
here my second activity
youtube_image.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent youtube= new Intent(getApplicationContext(),PlayYouTube1st.class);
youtube.putExtra("youtubeLink",youtubeLink);
startActivity(youtube);
// Toast.makeText(Movie_rev_fulldis_activity.this,
// youtubeLink, Toast.LENGTH_LONG).show();
}
});
youtubeLink=Reviews_update.revData.get(mov_pos).getYoutubeLink();
Toast.makeText(Movie_rev_fulldis_activity.this,
Reviews_update.revData.get(mov_pos).getYoutubeLink(), Toast.LENGTH_LONG).show();
mov_pos=Integer.parseInt((getIntent().getExtras()).getString("mov_pos"));
in second activity i am getting values but when i send one filed to third activity that value passing null any one please help me i stuck in their,
I want to show YoutubeLink field sencnd activity to third activity how to parse that any one please help
In FastActivity:
Intent in = new Intent(FastActivity.this, SecondActivity.class);
in.putExtra("a", yourdata);
startActivity(in);
In SecondActivity:
Bundle extras = getIntent().getExtras();
if (extras != null) {
String a = extras.getString("a");
}
You can also use any static variable and use it in any class
static int a = 5;
And in any other class
int b = classname.a;
classname is the class where static variable is declared.
Make that variable static and then pass
Your variable object reference get change in the THIRD Activity so, it returns null

Passing a String value across Activities?

I am currently attempting to pass a string across activities. The string is used to set an image in the second activity. So the user is clicking a button in the first activity and then the appropriate image is loaded in the second activity based upon what was selected. There are three buttons in the first activity all which should send a String value to the next activity.
This is what I have in my first activity:
Button archerButton = (Button) findViewById(R.id.Button_Archer);
archerButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String selection = "archer";
Bundle basket = new Bundle();
basket.putString("key", selection);
Intent i = new Intent(SelectCharacterActivity.this, LevelOneActivity.class);
i.putExtras(basket);
startActivity(i);
finish();
}
});
}
The receiving activity has the following code:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
setContentView(R.layout.level_one);
ImageView img = (ImageView) findViewById(R.id.Character_Chosen);
Bundle extras = getIntent().getExtras();
String Toon = extras.getString("key");
if(Toon=="archer"){
img.setImageResource(R.drawable.archer);
}
if(Toon=="mage"){
img.setImageResource(R.drawable.mage);
}
if(Toon=="warrior"){
img.setImageResource(R.drawable.warrior);
}
}
I know that the receive if statements section works because if I manually set the value Toon the image will load. So somewhere the information is not being sent or read properly. I followed a tutorial online and but am stumped on this.
issue is Java 101. string equality test is done using the equals method.
Toon.equals("archer");
Extra tip : inverse your test to avoid null pointer exception :
"archer".equals(Toon);
Extra extra tip : considering your cases are mutually exclusive, use if else if to avoid testing all cases.
You are comparing your strings wrong,
Toon=="Anything"
will never be true, you need to use
Toon.equals("Anything")
for it to work.
EDIT:
This is true because "==" will just compare the memory addresses of the Strings, and "Anything" will just have been allocated and definitely won't match Toon. .equals however actually compares the chars in the string.
Also to test things like this try putting in a
Log.i("YourAppName", "Toon value is: " + Toon);
after
String Toon = extras.getString("key");
try to use SharedPreferences, Set it after/with the button-click and get it on 2. Activity OnCreate
If you just want to pass a simple String, you can use the following:
On sending:
Intent i = new Intent(SelectCharacterActivity.this, LevelOneActivity.class);
i.putExtra("key", selection);
startActivity(i);
On receiving:
String selection = getIntent().getStringExtra("key");
If there is no parameter with this key, selectionwill be null.
Try this
Intent i = new Intent(FirstActivity.this, SecondActivity.class);
i.putExtra("email", "lol#hotmail.com");
startActivity(i);
and
Intent i = getIntent();
String myemail = i.getStringExtra("email");
Furthermore, to compare String, use .equals() not ==

Android import intent contents within another activity

am trying to send a variable from one activity to another i have set up an intent to send to the second activity. what i want to know is in the second activity what do i need to do in order to be able to use that variable in an if statement?
heres my code
Intent mainIntent = new Intent(TheLeagueActivity.this,IntroActivity.class);
mainIntent.putExtra("leagueCount", leagueCount);
TheLeagueActivity.this.startActivity(mainIntent);
TheLeagueActivity.this.finish();
String strExtra = getIntent().getExtras().getString("leagueCount");
...that's it! ;)
(Depending on what dataType u put in, u have to use "getInt()" or sth..)
in onCreate() method of IntroActivity write the following code
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Intent intent = getIntent();
// -1 is default value if no value associated with key "leagueCount"
int leagueCount = intent.getIntExtra("leagueCount", -1);
/*
if leagueCount is not equal to -1
use leagueCount here
*/
}
In the target activty, call getIntent to retrieve the intent, then use getStringExtra, getIntExtra, etc. to retrieve the intent parameters.
Depends what is your variable.
It seems to be an int.
So you will call like this:
int myVar = getIntent().getExtras().getInt("leagueCount");
if (myVar == 2) {
//do the stuff
}

Retrieving the intent values from another class

My question is a bit basic. I've been learning to code on JAVA and android. Here I am a bit confused on how to call the values that I have sent via an intent.
In my first activity this is the intent that I am using.
Intent intent = new Intent(MainActivity.this, Secondactivity.class);
String regName1 = regName;
intent.putExtra(regName1, regNameSplit[0]);
startActivity(intent);
Here regName1 will contain three values. SessionID,URL,Name split by "-".
In my SecondActivity
public class Secondactivity extends Activity {
public final String TAG = "###---Secondactivity---###";
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.secondactivity);
Log.i(TAG,"before if statement");
if (getIntent().getStringExtra("regName1") != null){
getIntent().getStringExtra("regName1").split("-");
String[] str = "regName";
Log.i(TAG, ""+str[0]+str[1]+str[2])
}
}
}
The value if regName1 always comes as null.
This line
intent.putExtra(regName1, regNameSplit[0]);
Needs to be like this instead
intent.putExtra("regName1", regNameSplit[0]); // note the quotes
BUT you are using regName1 as a variable... how do you expect the second class to know that variable?
Use a string resource instead.
and you are sure that the content of the variable regName is actually "regName"?
cause you set the value using
intent.putExtra(regName, ... )
and you get the value using
intent.getStringExtra("regName")
use firstactivity
dont use
String regname1=regname;
just:
intent.putExtra("regName1", regNameSplit[0]);
in second Activity
if (getIntent().getStringExtra("regName1") != null){
//
}

Categories

Resources