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.
Related
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
On the first activity I have a spinner and a button. I want to make it so that when a spinner option is chosen and then the button is clicked, a new activity is opened and the spinner option is displayed (along with additional information about it) in a TextView. In short, I'm trying to use intent to send and retrieve data, and then add additional information afterwards, which I think I would use putExtra() method.
Here I have the button directing to the second page and I am trying to store the spinner's object via intent.
MainActivity
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button yourButton = (Button) findViewById(R.id.done);
yourButton.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
Intent intent = new Intent(MainActivity.this, Selected.class);
intent.putExtra("new_variable_name","value");
startActivity(intent);
}
});
Here is Selected.class (second activity)
I want to retrieve the spinner info and put it into a textview.
Bundle extras = getIntent().getExtras();
if (extras != null) {
String value = extras.getString("new_variable_name");
}
}
Also, here is the spinner array named "person_array":
<string-array name="person_array">
<item>Nick</item>
<item>Isaac</item>
<item>Sally</item>
<item>Matt</item>
<item>Tim</item>
</string-array>
problem in your code is that you are creating two different intent object. The former filled up with the string:
Intent i = new Intent(getApplicationContext(), Spinner.class);
i.putExtra("new_variable_name","value");
and you use the latter to star the Selected activity.
startActivity(new Intent(MainActivity.this, Selected.class));
You should use only one
Intent intent = new Intent(MainActivity.this, Selected.class);
intent.putExtra("new_variable_name","value");
startActivity(intent);
In your first code snippet, you create an Intent (i), pointing to something that is not an Android application component (Spinner), put an extra on it, and then throw it away. This will not be useful.
Instead, put your extra on the Intent that you are using with startActivity():
startActivity(new Intent(MainActivity.this, Selected.class)
.putExtra("new_variable_name","value"));
i have to activities:
activity 1 with twoo edit text and
activity 2 with a list view.
Everytime i fill the forms in activity1 and press the button "send" i want that all i have wrote in the two edit texts go in one row of the list view of activity 2.
I have tried and it is the result but i don't know how i have to continue:
this is the code of activity1 when the button is pressed:
buttonSend.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.i(TAG, "Button Send Report Clicked......");
object=object.getText().toString();
description = editDescription.getText().toString();
getCoordinates();
Intent i=new Intent(FirstReporting.this, MyList.class);
i.putExtra("object", object);
i.putExtra("description", description);
startActivity(i);
}
});
this is the code of activity2:
arrayAdapterListReports arrayAdapter = new ArrayAdapterListReports(this, R.layout.item_list_reports, listReports);
listView=(ListView)findViewById(R.id.listView1);
listView.setAdapter(arrayAdapter);
and then?
In that other activity you can do this to get the values:
Bundle extras = getIntent().getExtras();
if (extras != null) {
String objStr = extras.getString("object");
String descStr = extras.getString("description");
// Do what you want with these now ...
}
Check out the Receiving Simple Data from Other Apps guide.
In your second Activity, you can retrieve the Intent used to start Activity2 using getIntent().
You can then use that Intent object to get your data like so:
Intent i = getIntent();
Bundle extras = i.getExtras();
String obj = extras.getString("object");
String desc = extras.getString("description");
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
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.