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
Related
I have an app that for right now, consists of 2 buttons (will later consists of 20+). When I click on a button, it takes me to a new activity that has a list of items I can select. After selecting something and clicking the Back button, it starts a new activity that passes on the item's information (in this case, "orange") and then it assigns the word "orange" to the button that was clicked.
Now when I click on the other button to assign it's information, I lose all of my first button information. What are my options for saving the previous information? Would I have to create an intent for it and keep passing it back and forth between actvities?
At the end, I need to collect all the information that was assigned to both buttons and pass that onto another activity, as this is just the customizing page. Is there a way I can just have the Strings set such that leaving the activity won't delete the String information?
Here's my MainActivity
Bundle extras = getIntent().getExtras();
if (extras != null) {
btnValue = extras.getString("btnValue");
itemValue = extras.getString("itemValue");
}
if (btnValue.equals("btn1")){
btn1.setText(itemValue);
} else if (btnValue.equals("btn2")) {
btn2.setText(itemValue);
}
}
public void onClickBtn1(View v) {
Intent myIntent = new Intent(this, Main2Activity.class);
myIntent.putExtra("btn", "btn1");
startActivity(myIntent);
}
public void onClickBtn2(View v) {
Intent myIntent = new Intent(this, Main2Activity.class);
myIntent.putExtra("btn", "btn2");
startActivity(myIntent);
}
and my 2nd activity
Bundle extras = getIntent().getExtras();
if (extras != null) {
btnValue = extras.getString("btn");
}
listView = (ListView) findViewById(R.id.list);
String[] values = new String[] { "apple", "banana", "orange", "cherry"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, android.R.id.text1, values);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
itemPosition = position;
itemPositionString = String.valueOf(itemPosition);
}
});
}
public void onClickBack (View v) {
Intent intent = new Intent(this, MainActivity.class);
intent.putExtra("btnValue", btnValue);
intent.putExtra("itemValue", itemValue);
startActivity(intent);
}
You can use the Android lifecycle to manage the activity's state.
To save the activity state you need to do your work on the method onSaveInstanceState.
onSaveInstanceState(Bundle bundle)
On restoration you either check the bundle the following methods
onRestoreInstanceState(Bundle bundle)
onCreate(Bundle bundle)
You can find more details here:
https://developer.android.com/training/basics/activity-lifecycle/recreating.html
When you use startActivity it creates new activity. So you lose old information. You need to use startActivityForResult from MainActivity while starting second activity and from the second activity you should use setResult
Intent returnIntent = new Intent();
returnIntent.putExtra("result",result);
setResult(RESULT_OK, returnIntent);
finish();
And you handle the result in MainActivity with
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
..
..
}
Here, there is an example How to manage `startActivityForResult` on Android?
You basically have 3 choices:
Pass all data in the Intent and return any new/changed data in another Intent using startActivityForResult().
Save the data in a static variable somewhere (globals). All activities can then reference the current data and make changes to it that are then seen by all other activities. This is the quick-and-dirty solution which is suitable for small, trivial or "homework" solutions.
Save the data in a persistent storage (a file or an SQLite database). All activities can read all the current data, display it and make changes. After the BACK button is pressed, the underlying Activity should read the current data from the persistent storage to refresh the views.
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
This question already has answers here:
Passing data between activities in Android
(3 answers)
Closed 7 years ago.
I have two buttons.
Button1 go to A then C
Button2 go to A then B and finally C.
There are values pass between these activities. The problem I faced now is how do I check whether they are data pass from B to C or it is from A to C only so I can set different condition to them.
Activity A
btnNext.setOnClickListener(new View.OnClickListener() { //if button1 is clicked
#Override
public void onClick(View v) {
Intent intent=new Intent(getApplicationContext(),C.class); //pass spinner value and date to next class
Name = name.getSelectedItem().toString();
Weather = weather.getSelectedItem().toString();
Status=status.getSelectedItem().toString();
intent.putExtra("Name",Name);
intent.putExtra("Weather",Weather);
intent.putExtra("Status",Status);
intent.putExtra("date2",date2);
startActivity(intent);
}
});
btnForce.setOnClickListener(new View.OnClickListener() { // if button2 clicked
#Override
public void onClick(View v) {
Intent intent=new Intent(getApplicationContext(),B.class);
Name = name.getSelectedItem().toString();
Weather = weather.getSelectedItem().toString();
Status=status.getSelectedItem().toString();
intent.putExtra("Name",Name);
intent.putExtra("Weather",Weather);
intent.putExtra("Status",Status);
intent.putExtra("date2",date2);
startActivity(intent);
}
});
Activity B
goDetails.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) { // if next button is clicked
Intent intent = new Intent(getApplicationContext(), C.class);
sub = SubContractors.getText().toString();
noP = NoPerson.getText().toString();
noH=NoHours.getText().toString();
intent.putExtra("sub",sub);
intent.putExtra("noP",noP);
intent.putExtra("noH",noH);
intent.putExtra("name",name);
intent.putExtra("weather",weather);
intent.putExtra("status",status);
intent.putExtra("date",date);
startActivity(intent);
}
});
}
Activity c
name = getIntent().getExtras().getString("Name"); // receive name from Information
weather = getIntent().getExtras().getString("Weather"); //receive weather
date = getIntent().getExtras().getString("date2"); //receive date
status = getIntent().getExtras().getString("Status"); // receive status
If has data passes from B, what should I write ?
You need to pass some value to know from where the call came from.
You can uses example ρяσѕρєя K gave you. Or better jet try
startActivityForResult(_new, REQUEST_CODE);
And then
getIntent().getAction()
Take a look at this post to see more examples:
Using startActivityForResult, how to get requestCode in child activity?
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 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.