Passing data from two activities to a third activity in android - 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

Related

Intent Extras.getString() not comparing correctly -- Android

I have an Activity called searchProcedures which allows a user to select from a listview of medical procedures. I navigate to this activity from two other activities called searchHome and describeVisit. I needed a way for searchProcedures to know which activity it should navigate back to onClick. So I pass an intent.extra from searchHome or describeVisit (key:"sentFrom" value""). Then in searchProcedures I use the following code to determine which class to navigate to.
Intent intent = getIntent();
Bundle extras = intent.getExtras();
if(!extras.isEmpty()){
if(extras.containsKey("sentFrom")){
if(extras.getString("sentFrom") == "searchHome"){
returnIntent = new Intent(searchProcedures.this, searchHome.class);
}
else if(extras.getString("sentFrom") == "describeVisit"){
returnIntent = new Intent(searchProcedures.this, describeVisit.class);
}
else{
Log.d("failed", "the value of getString is " + extras.getString("sentFrom"));
}
}
}
Checking the Log values, the correct values are being passed to and from activity, but when I check extras.getString("sentFrom") == "searchHome/describeVisit" it comes back as false, and returnIntent remains un-initialized. I have tried putting .toString after the .getString to no avail.
1.
== compares the object references, not the content
You should use:
"searchHome".equals(extras.getString("sentFrom"))
Remeber to check blank space,...
2.
You can use a static variable in your SearchProceduresActivity to check where it comes from
SearchProceduresActivity
public static int sFrom = SEARCHHOME;
SearchHomeActivity:
Intent myIntent = new Intent(SearchHomeActivity.this, SearchProceduresActivity.class);
SearchProceduresActivity.sFrom = SEARCHHOME;
startActivity(myIntent);
DescribeVisitActivity:
Intent myIntent = new Intent(DescribeVisitActivity.this, SearchProceduresActivity.class);
SearchProceduresActivity.sFrom = DESCRIBEVISIT;
startActivity(myIntent);
SEARCHHOME, DESCRIBEVISIT value is up to you
Hope this help!
String compare should use equal not ===

Activity That has Several Extras from Other Activities?

I have an Activity that uses the following code to retrieve information from another activity:
Bundle extras = getIntent().getExtras();
if (extras != null) {
int tok = extras.getInt("Token");
tempToken += tok;
}
This is the Code inside the first other class that sends this information:
final Button mainMen = (Button) findViewById(R.id.toMainMenu);
mainMen.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent i = new Intent(getApplicationContext(),
Menu.class);
i.putExtra("Token", tok + teTok);
startActivity(i);
}
});
Now i have another Activity that also wants to sen information to the Main Activity like so:
maMenu.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(Campaign.this, Menu.class);
intent.putExtra("Token", player.tokens);
intent.putExtra("Round", player.round);
intent.putExtra("Rank", player.rank);
intent.putExtra("Score", player.score);
intent.putExtra("Sec", player.secondsTapped);
intent.putExtra("Min", player.minutesTapped);
intent.putExtra("Hour", player.hoursTapped);
intent.putExtra("Day", player.daysTapped);
intent.putExtra("LifeTap", player.tapsInLife);
intent.putExtra("SecTap", player.tapsPerSec);
intent.putExtra("TapRo", player.tapps);
startActivity(intent);
}
});
Now my question is, how do i handle these different extras from multiple Activities inside the one Main Activity?
Thank You for your time :)
There are two ways to solve your problem..
1)
You can pass one boolean value to or and int variable with some value.. And retrieve this in your new Activity and check with boolean value or int value and get correct data correspond to Activity.
2) You can save your all Data in Shared Preference. And get your all Data in any Activity.
you can send one boolean value that data is in first class or second class and in MainActivity check the value and get the correct data

Android clear values on Activity start

I'm stuck in one problem from yesterday and I can't find a solution. I'm trying to set one variable in a child activity in my tabhost to 0. Basically I have this situation :
Activity A -- sends extra "something" to activity B (B is getting it)
Activity B -- starts Activity C
Activity C -- onButton click sends extra "something" to activity B and finish()
This all happens in First tab.
The problem is when I change the tab and return back to Activity A, than B, that extra "something" is still saving the value which Activity C sends, no matter if I send it from Activity A. Here is an example code :
Activity A -- starts B :
if (ownedCards != 0) {
ownedStampii.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent previewMessage = new Intent(getParent(), OwnedStampii.class);
TabGroupActivity parentActivity = (TabGroupActivity)getParent();
previewMessage.putExtra("collection_id", collId);
previewMessage.putExtra("SOMETHING", 0); // this is what I'm trying to se to 0
parentActivity.startChildActivity("OwnedStampii", previewMessage);
}
});
}
This is how I read extra in Activity B :
final int extra = getParent().getIntent().getIntExtra("SOMETHING", 0);
Log.e("", "extra : " + extra);
This is how I send extra from Activity C :
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = getParent().getIntent();
intent.putExtra("SOMETHING", objectID); // objectId of category which will sort the data
getParent().setResult(RESULT_OK, intent);
finish();
}
});
I tried to set int extra =0 on onResume(), onRestart(), onStop().. and etc. it's still not working.
Any suggestions how can I set this variable to 0?
I think you are reusing an existing intent in activity C and putting another SOMETHING into the same intent. Try calling intent.removeExtra("SOMETHING"); before putting it in again. Or send a new Intent from C to B.
Intent intent = getParent().getIntent();
intent.removeExtra("SOMETHING"); // Hox! Add this.
intent.putExtra("SOMETHING", objectID);
getParent().setResult(RESULT_OK, intent);
finish();
You should pass data between Activities through Intent and startActivityResult() and onActivityResult();
This may be tricky while using TabGroupActivity.
Here is a solution which I posted to another question having somehow same scanario.
You are declaring extra as a final variable which means once you create and assign it to something you cant change it afterwards.
So when you are trying to reassign and change the value it's basically not possible cause your variable is final.

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){
//
}

Android Intent issue

I want to show the details of a calculation in another activity when click on a button.
How to achieve this.
my first activity java code is
public void onClick(View v)
{
if(v.getId()==R.id.Button07)
{
Intent intent=new Intent(AdvancedCalculator.this,calculatedData.class);
startActivity(intent);
}
The calculation i want to do is
String a,b;
Integer vis;
a = txtbox3.getText().toString();
b = textview1.getText().toString();
vis = (Integer.parseInt(a)*Integer.parseInt(b))/100;
tv.setText(vis.toString());
i want the result 'tv' to be shown in next activity when i press the submit button.
Where I need to include this calculation.and what are the further steps
Any help is greatly appreciated
Thanks
In your first activity:
public void onClick(View v) {
//Put your calculation code here
Bundle b = new Bundle();
b.putString("answer", youranswer);
//You could also use putInteger, whichever you prefer.
Intent intent=new Intent(AdvancedCalculator.this,calculatedData.class);
intent.putExtras(b);
startActivity(intent);
}
In your second activity, in the onCreate put this:
Bundle b = getIntent().getExtras();
String answer = b.getString("answer");
Answer is your key. it is used to identify what you want to get from the bundle. Using unique keys means you can pass more than one value to the next activity, if you want.

Categories

Resources