Why putextra isn't passing value? - java

I'm trying to send a result from my MainActivity to SecondActivity, but it always returns a default value, not the result. I'm a begginer, if there is someone that could help me it would be nice.
I've tried everything that came to my mind, but nothing worked.
btnPlus.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String prvi=etPrviBroj.getText().toString().trim();
String drugi=etDrugiBroj.getText().toString().trim();
prviBroj=Integer.parseInt(prvi);
drugiBroj=Integer.parseInt(drugi);
rez=prviBroj+drugiBroj; //declared as an int and set to 0
Intent intent=new
Intent(MainActivity.this,SecondActivity.class);
intent.putExtra("rez",rez);
}
});
//and in the second activity
rezultat=getIntent().getIntExtra("rez",0);

in first activity :
btnSum.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (!edt1.getText().toString().isEmpty() && !edt2.getText().toString().isEmpty()) {
sum = Integer.parseInt(edt1.getText().toString()) + Integer.parseInt(edt2.getText().toString());
Intent intent = new Intent(Main2Activity.this, MainActivity.class);
intent.putExtra("RESULT", sum);
startActivity(intent);
txtAns.setText("" + sum);
} else if (edt1.getText().toString().isEmpty()) {
edt1.setError("Please enter no1 ");
} else if (edt2.getText().toString().isEmpty()) {
edt2.setError("Please enter no2 ");
}
}
});
second activity
int sum= getIntent().getIntExtra("RESULT",0);
You can store your sum of two variables in one int variable. When you click of your second button make instant of Intent and call startactivity at that time.

You're creating an Intent just to set the variable you want to pass to the second activity but in your code this Intent is not linked to the new Activity.
If you want to pass a value between two activities with this method, you have to start the second activity with the Intent you gave the extra value to.
Intent intent=new Intent(MainActivity.this,SecondActivity.class);
intent.putExtra("rez",rez);
startActivity(intent);
If you want to start the second activity with another Intent you have to pass the value in another way.

Intent intent=new
Intent(MainActivity.this,SecondActivity.class);
intent.putExtra("rez",rez);
startActivity(intent);
In SecondActivity
Intent intent = getIntent();
String abc = intent.getStringExtra("rez");

Related

How to get value of a TextView which is in another activity?

I want to get the value of a TextView which is defined in another activity? I am using android studio.
You should use intents for passing values to another activities.
in first activity:
Intent intent = new Intent(CurrentActivity.this, NextActivity.class);
intent.putExtra("YOURKEY", yourTextViewValue);
startActivity(intent);
Access that intent on next activity
String textViewValue= getIntent().getStringExtra("YOURKEY");
First activity (which has TextView)
SecondActivity.launchActivity(this, textView.getText().toString())
Second Activity (which need the text value)
public static void launchActivity(Context context, String textViewValue) {
Intent intent = new Intent(context, SecondActivity.class);
intent.putExtra("textValueKey", textViewValue)
context.startActivity(intent);
}
#Override
public void onCreate(Bundle savedInstanceState) {
if (getIntent() != null) {
String textViewValue = getIntent().getStringExtra("textValueKey");
}
...
}

How to send data from the 5th activity to the 1st activity?

I am using 5 activities. from activity1 I move to activity2, from act2 to act3, from act3 to act4 and from act4 to act5.
activity 2 carries data to act3 and like this act5 receives data of act2, act3, act4 and then send all data to act 1.
My First activity
I{
Intent i= new Intent(firstactivity.this, secondactivity.class);
startActivityForResult(i, 10);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 10) {
String a= data.getStringExtra("Value1");
String b= data.getStringExtra("Value2");
String c= data.getStringExtra("Value3");
String d= data.getStringExtra("Value4");
String showall = a+", "+b+", "+c+", "+d;
address.setText(showall);
}
My Second activity
Intent intent = new Intent(secondactivity.this, thirdactivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT);
intent.putExtra("Value1", firstvalue);
startActivity(intent);
My Fifth activity
Intent intent = new Intent(fourthactivity.this, fifthactivity.class);
intent.putExtra("Value1", geta);
intent.putExtra("Value2", getb);
intent.putExtra("Value3", getc);
intent.putExtra("Value4", getd);
intent.addFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT);
setResult(10);
finish();
Try below:
Intent intent = new Intent(fifthactivity.this, firstactivity.class);
intent.putExtra("Value1", geta);
intent.putExtra("Value2", getb);
intent.putExtra("Value3", getc);
intent.putExtra("Value4", getd);
intent.addFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT|Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
startActivity(intent);
And then process your passed result in onCreate (in case activity got destroyed) or onNewIntent (if your activity is still running but you bring it to front and update it with new intent) of your firstactivity
//A simple approach to solve this issue is to use sharedpreferences
//store value from activity five
getsharedpreferences('temp','MODE_PRIVATE').edit().clear().putString('values','new value').apply();
//get values from shared preferences if its available
String value 5 = getsharedpreferences('temp','MODE_PRIVATE').getString('values','nil');
getsharedpreferences('temp','MODE_PRIVATE').edit().clear();
first activity
static ArrayList<String> arlist=new ArrayList<String>();
arlist.add("value");
arlist.add("value1");
arlist.add("value2");
In second Activity
ActivityOne.arlist

App crashes when switching from activity 1 to activity 2

In my app, there are so many activities which are referred to as levels. And one activity is Reward activity. when i win level-1, reward activity opens. Now i want to replay the level-1. For this i have used getExtra(). My app crashes when i click the replay button.
Houselevel1.java
public void getReward(){
if(count == 3) {
Intent intent = new Intent("com.creatives.arfa.revealthesecretsgame.Reward");
intent.putExtra("activity", "level1");
startActivity(intent);
}
}
HouseLevel2.java
public void getReward(){
if(count == 3) {
Intent intent = new Intent("com.creatives.arfa.revealthesecretsgame.Reward");
intent.putExtra("activity", "level2");
startActivity(intent);
}
}
Reward.java
public void replayLevel() {
replay = (ImageButton) findViewById(R.id.replay);
Intent intent= getIntent();
activity = intent.getStringExtra("activity");
replay.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View paramView) {
if(activity.equals("level2")){
Intent intent = new Intent("com.creatives.arfa.revealthesecretsgame.HouseLevel2");
startActivity(intent);
}
if(activity.equals("level1")){
Intent intent = new Intent("com.creatives.arfa.revealthesecretsgame.Houselevel1");
startActivity(intent);
}
}
});
}
With the java code you've posted, in the Reward.java file, you're trying to create another Intent Object with the same name as the one declared in the scope right above it. Because of this, the build will never be successful.
Also, when you declare intents, you MUST pass on the activity_name.class file.
Something you can try:
1) HouseLevel1.java
public void getReward(){
if(count == 3) {
Intent intent = new Intent(getApplicationContext(), com.creatives.arfa.revealthesecretsgame.Reward.class);
intent.putExtra("activity", "level1");
startActivity(intent);
}
}
2) HouseLevel2.java
public void getReward(){
if(count == 3) {
Intent intent = new Intent(getApplicationContext(), com.creatives.arfa.revealthesecretsgame.Reward.class);
intent.putExtra("activity", "level2");
startActivity(intent);
}
}
3) Reward.java
public void replayLevel() {
replay = (ImageButton) findViewById(R.id.replay);
Intent intent= getIntent();
activity = intent.getStringExtra("activity");
replay.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View paramView) {
if(activity.equals("level2")){
Intent intent = new Intent(getApplicationContext(), com.creatives.arfa.revealthesecretsgame.HouseLevel2.class);
startActivity(intent);
}
else if(activity.equals("level1")){
Intent intent = new Intent(getApplicationContext(), com.creatives.arfa.revealthesecretsgame.Houselevel1.class);
startActivity(intent);
}
}
});
}
Also, if you're simply using the Reward.java file to get the previous intent's data, perform some calculation, and send some data back to the calling, or parent activity, then you can simply use the startActivityForResult() method, which takes care what what you're trying to do manually.
Here's a small article that might be able to help you with the problem
http://www.vogella.com/tutorials/AndroidIntent/article.html#retrieving-result-data-from-a-sub-activity
If all you want is go from Activity 1 or to 2 to a Reward activity grab something and send that something back to either activity.
What you do is startActivityForResult You pass an Id (constant number) do what you do on the Reward activty, pack what you need to return in a Bundle, and set ActivtyResult to OK and close your activity.
Your app will go back to the Activity1 or 2 whoever call it. On those activties you override the method onActivityResult There you check if the id on which the result is coming from is the Id you sent on the startActivityForResult and if the status is OK.
Then you have whatever was set on the Reward activity. The Reward activity don't need to know from where it came from if only will grab some data. So you can later have an Activity3 that calls the Reward activity and you do not need to modify the Reward activity.
It is explain here check the accepted answer.
How to manage `startActivityForResult` on Android?

Unwanted duplicated activity

There is a code that sends an int 'a' from the main activity to activity B. It also starts activity B with the fade animations. However, this code creates 2 of the same activity B's and I only need 1 activity B. How can I fix this so that it only makes 1.
new Handler().postDelayed(new Runnable() {
public void run() {
Handler splash = new Handler();
int a = 1;
Intent myIntent = new Intent(MainActivity.this, Differentiate.class);
startActivity(new Intent(MainActivity.this, Differentiate.class));
myIntent.putExtra("HEADER", a);
overridePendingTransition(R.anim.fade_in_switch_fast,R.anim.fade_out_switch_fast);
startActivity(myIntent);
finish();
}
}, secondsDelayed * 2000);
You are starting the second activity twice. Remove the following line from your code and move overridePendingTransition after you use the intent to start the activity:
startActivity(new Intent(MainActivity.this, Differentiate.class));
Hey just remove this line from code:
startActivity(new Intent(MainActivity.this, Differentiate.class));
Rest is fine in your code.
That's because you are starting Activity B twice!
you should remove this part of your code:
startActivity(new Intent(MainActivity.this, Differentiate.class));

Move data by intent without startActivity

I have 2 Activities. On 2nd activity i have data which i want to move by button "Back" to 1st Activity.
Usually i moving by something like this:
button12.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
pass=editText2.getText().toString();
Intent intent = new Intent(FirstActivity.this, MapsActivity.class);
intent.putExtra("pass_value", pass);
startActivity(intent);
}
});
But now i dont want to start Activity, instead of this i want close Activity, so i need to use finish()
Currently i created this, but no working:
b2.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, FirstActivity.class);
intent.putExtra("number", numberOfTrue);
finish();
}
});
I need something more in this code, but i dont know what.
Start a new activity with
Intent intent = new Intent(FirstActivity.this, MapsActivity.class);
intent.putExtra("pass_value", pass);
startActivityForResult(intent,1)
You can use setResult method to acheive your desired result
Intent output = new Intent();
output.putExtra("number", numberOfTrue);
setResult(Activity.RESULT_OK, output);
finish();
get Your result in FirstActivity
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1 && resultCode == Activity.RESULT_OK && data != null) {
int num = data.getIntExtra("pass_value");
}
}
b2.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
finish();
Intent intent = new Intent(this, FirstActivity.class);
intent.putExtra("number", numberOfTrue); }});
You need to open by function startActivityForResult
startActivityForResult(Intent intent, int requestCode)
And get the data returned in the function
onActivityResult
As an example you can see a face to the camera for a picture

Categories

Resources