Android Studio defaultValue/ passing Variables [duplicate] - java

This question already has answers here:
How do I get extra data from intent on Android?
(16 answers)
Closed 4 years ago.
I'm new to Android Studio and java, so hopefully you can help me.
I want to pass a double variable from on activity to the next.
But I'm unsure what needs so go in the defaultValue in the receiving activity.
Here is the code from activity one:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button weiter = (Button)findViewById(R.id.weiter);
weiter.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
EditText EingabeBreite = (EditText)findViewById(R.id.breite);
double breite = Double.parseDouble(EingabeBreite.getText().toString());
Intent rüber = new Intent(getApplicationContext(), Main2Activity.class);
getIntent().putExtra("next", breite);
startActivity(rüber);
Here is the code from the second activity:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
TextView ergebnis = (TextView)findViewById(R.id.textView2);
Bundle extras = getIntent().getExtras();
double breite = extras.getDouble("next");
ergebnis.setText(Double.toString(breite));

add the code to second activity:
double breite=getIntent().getDoubleExtra("next",0d);
So easy

Intent weiter = new Intent(MainActivity.this,Main2Activity.class);
startActivity(weiter);
EditText EingabeBreite = (EditText)findViewById(R.id.breite);
double breite = Double.parseDouble(EingabeBreite.getText().toString());
Intent rüber = new Intent(getApplicationContext(),MainActivity.class);
getIntent().putExtra("next","breite");
startActivity(rüber);
First off- you're starting 2 intents. That's not what you want. Only one activity can be in the forefront, you want to do one of these. Not both.
Secondly- you don't want getIntent().putExtra(). You want ruber.putExtra(). You need to put the extra on the intent you send to the other activity. Calling getIntent will get the intent that started the current Activity, which isn't what you're sending to the next one.

use this code :
Intent mIntent = new Intent(HomeActivity.this, CenterActivity.class);
mIntent.putExtra("thevalue ", 0.0d);
startActivity(mIntent);
Intent intent = getIntent();
double d = Double.parseDouble(intent.getExtras().getString("thevalue "));

Related

OnClick open Activity1 but send EXTRA_TEXT to Activity10...Is a String necessary or can I simply use intent? Can post XML if helpful

Multiple Activity App...Activity1 (SiteData) collects data and sends to Activity10 (DataReview)...but before going straight to Activity10 user must go through Activities2-9 (SanDiegoArea, etc...) collecting data and also passing EXTRA_TEXT to the other activity until Activity10...
This way:
(Activity1 -> Activity2 -> ... -> Activity10)
Activity10 will be able to review Activity1-9 EXTRA_TEXT for verification...Code is as follows but I'm getting no EXTRA_TEXT at Activity10 to display in it's TextViews for review? How can I solve the problem?
Activity1 - SITEDATA - data info input
public class SiteData extends AppCompatActivity {
public static final String EXTRA_TEXT = "MultipleActivities.EXTRA_TEXT";
public static final String EXTRA_NUMBER = "MultipleActivities.EXTRA_NUMBER";
public void clickFunction(View view) {
Intent intent = new Intent(getApplicationContext(), SanDiegoArea.class);
startActivity(intent);
Log.i("Info", "Login Details Entered");
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sitedata);
//Possible coding needed for Autocomplete...see more
Button button = (Button) findViewById(R.id.buttonSend);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
openSanDiegoArea();
}
});
}
public void openSanDiegoArea() {
EditText editText1 = (EditText) findViewById(R.id.editTextName);
String text = editText1.getText().toString();
EditText editText2 = (EditText) findViewById(R.id.editTextPhone);
int number = Integer.parseInt(editText2.getText().toString());
Intent intent = new Intent(this, DataReview.class);
intent.putExtra(EXTRA_TEXT, text);
intent.putExtra(EXTRA_NUMBER, number);
startActivity(intent);
}
}
ACTIVITY10 - DATAREVIEW Section getting info
public class DataReview extends AppCompatActivity {
public void clickFunction(View view) {
Intent intent = new Intent(getApplicationContext(), ThankYou.class);
startActivity(intent);
Log.i("Info", "Sample Data Sent to BWTF");
}
#Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.datareview);
Intent intent = getIntent();
String text = intent.getStringExtra(SiteData.EXTRA_TEXT);
int number = intent.getIntExtra(SiteData.EXTRA_NUMBER, 0);
TextView textView1 = (TextView) findViewById(R.id.editTextName);
TextView textView2 = (TextView) findViewById(R.id.editTextPhone);
TextView textView3 = (TextView) findViewById(R.id.editTextEmail);
TextView textView4 = (TextView) findViewById(R.id.editTextDate);
TextView textView5 = (TextView) findViewById(R.id.editTextTime);
textView1.setText(text);
textView2.setText("" + number);
}
};
If Activity1 calls Activity2, it will have to pass data to it the way you are doing. ( I'm guessing here that your Activity2 is DataReview.class)
But Activity2 will call Activity3 and, in order for it not to loose the data received from activity 2 you will have to send it too. In other words, Activity1 sends Activity1's data to Activity2, Activity2 sends Activity1's and Activity2's data to Activity3 and so on.
In this way, Activity9 will receive (1, 2, 3, 4, 5, 6, 7, 8) data from it's caller Activity8 and will have to pass all this accumulated data to Activity10.
Considering that Activity3 is called by Activity2 it can't see the data sent from Activity1 because it don't know it unless Activity2 pass it too. Am I been clear?
If you are just learning Android it is ok to do it this way in order to learn but it is not the better way to solve this problem. I would use a same ViewModel on all activities in order to represent this data in order to always have just one place for holding it as it would make my code more clear. In reality I wouldn't even use all those Activities, I would use only one activity and work with 10 fragments, one for each page (it is just to provide navigation between then). So those are Ideas for you to improve your app if you feel It would be good.

Sending double from one activity to another Android Studio

Hi,
I am trying to send data from one activity to another activity using this method: how to retrieve a Double value from one activity to another?
Yet every time I open up my application it crashes as its because my code is in the onCreate:
double lat, longi;
Intent getLocation;
Bundle extras;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getLocation = this.getIntent();
extras = getLocation.getExtras();
lat = extras.getDouble("lat");
longi = extras.getDouble("longi");
but when I put it in a button instead It can't resolve this.getIntent();
public void getCoordinates(View view){
Button coordinates = (Button) findViewById(R.id.getCoordinates);
coordinates.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view){
getLocation = this.getIntent();
extras = getLocation.getExtras();
lat = extras.getDouble("lat");
longi = extras.getDouble("longi");
}
});
}
I would like to receive data either automatically or using a button.
I am quite new in mobile computing so please don't roast me.
this meant isView.OnClickListener() in your code .
And we need YourActivity.this as Context
You should use
public void getCoordinates(View view){
Button coordinates = (Button) findViewById(R.id.getCoordinates);
coordinates.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view){
getLocation = YourActivity.this.getIntent();
extras = getLocation.getExtras();
lat = extras.getDouble("lat");
longi = extras.getDouble("longi");
}
});
}

How to pass variable from onClick to another class [duplicate]

This question already has answers here:
How do I pass data between Activities in Android application?
(53 answers)
Closed 7 years ago.
I was trying to pass a variable named choice to another class but I do not know how I should go along doing this because it is inside the onClick. Here is an example of what I am trying to do.
public int choice;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//intent for first button
Button button1= (Button) findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
choice = 1;
startActivity(new Intent(MainActivity.this,Celeb1.class));
}
});
//intent for second button
Button button2= (Button) findViewById(R.id.button2);
button2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
choice = 2;
startActivity(new Intent(MainActivity.this,Celeb1.class));
}
});
}
In a different class I would like to check if choice is equal to 1 or 0 and execute some code.
You can pass variable by this way
Intent intent = new Intent(MainActivity.this,Celeb1.class)
intent.putExtra("choice", 1);
startActivity(intent);
And in Celeb1 class you can get variable by using
Bundle extras = getIntent().getExtras();
int choice= extras.getInt("choice");
inside onCreate()
//in main activity
Intent i=new Intent(getApplicationContext(),Celeb1.class);
i.putExtra("name","stack");//name is key stack is value
startActivity(i);
//In Celeb1 class
Intent i=getIntent();
String name=i.getStringExtra("name");
Do not make instance of Intent instead make object of Intent and pass extra values to your class
choice = 2;
Intent intent = new Intent(MainActivity.this, Celeb1.class);
intent.putExtra("choice",choice);
startActivity(intent);
That's It
I am android beginner, Hope It will help you.

How do you pass a string from one activity to another? [duplicate]

This question already has answers here:
How do I pass data between Activities in Android application?
(53 answers)
Closed 9 years ago.
I'm wondering how to pass and read a string that is in one activity from another activity. I have two activities. I'll call them Activity1 and Activity2. I have a string in Activity1 called course. I want to read that string in Activity2.
I've tried doing this but the string came out empty.
public class Activity2 extends Activity1 {
I've seen people use the Intent function but I couldn't figure out how to use it.
Any suggestions? Thanks!
Pass values using intents.
In your first activity
Intent i= new Intent("com.example.secondActivity");
i.putExtra("key",mystring);
// for explicit intents
// Intent i= new Intent(ActivityName.this,SecondActivity.class);
// parameter 1 is the key
// parameter 2 is the value
// your value
startActivity(i);
In your second activity retrieve it.
Bundle extras = getIntent().getExtras();
if (extras != null) {
String value = extras.getString("key");
//get the value based on the key
}
To pass custom objects you can have a look at this link
http://www.technotalkative.com/android-send-object-from-one-activity-to-another-activity/
your first activity, Activity1
public class Activity1 extends Activity {
Button btn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity1);
btn=(Button) findViewById(R.id.payBtn);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent=new Intent(Activity1.this,Activity2.class);
intent.putExtra("course", "courseValue");
startActivity(intent);
}
});
}
}
Activity2
public class Activity2 extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity2);
String course=getIntent().getExtras().getString("course").toString();
Log.d("course",course);
}
}
Hope this will help you.
You're on the right track - you're using an intent to launch the second activity. All you have to do is add intent.putExtra("title", stringObject); where stringObject is the string you want to pass, and title is the name you want to give that object. You use that name to refer the object passed in the second activity as follows:
String s = (String)getIntent().getExtras().getSerializable("title");
From Activity 1 call something like this :
Intent intent= new Intent("path.secondActivity");
intent.putExtra("keyString",sampleString);
startActiivty(intent);
and in activity 2 try something like this :
Bundle values = getIntent().getExtras();
if (values != null) {
String keyString = values.getString("keyString");
}
In your MainActivity
Intent i= new Intent(MainActivity.this,SecondActivity.class);
i.putExtra("key",yourstring);
startActiivty(i);
In your second activity onCreate()
Bundle extras = getIntent().getExtras();
if (extras != null) {
String value = extras.getString("key");
}
Try this
public class Activity2 extends Activity1

Make my class take an argument

Well I have a main Screen with 5 buttons. Each time a press a button I want to go a new screen. So far I have other 5 classes (each for each screen) and 5 xmls. But Iam sure that there will be a better way beacuse this screen and the xmls are the same, and what I want to do is change some texts and some data I fetch from a database. I am sure that I can ony another class and only one xml and then pass the values that I want as arguments. (Imagine that in its final state my app must have 15 buttons, so I think it is too mych waste of space and unnecessary to have 15 .java files and 15 xml files that look the same and only some values of images and textviews change). My code for main activity is:
public class MyActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
main2Theme();}
private void main2Theme() {
setContentView(R.layout.main_new);
Button Button100 = (Button)findViewById(R.id.Button100);
Button113.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
Intent i = new Intent(this, OtherScreenName.class);
startActivity(i);
}
}); //and so on for 15 buttons
My OtherScreenName.class is:
public class OtherScreenName extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Theme();
}
#SuppressWarnings("null")
private void Theme() {
Log.d("SnowReportApp","Do first thing");
setContentView(R.layout.otherscreenname); //THIS CHANGES IN EVERY CLASS DEPENDING ON THE ID OF THE BUTTON
String result = "";
InputStream is = null;
StringBuilder sb=null;
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();//() before
nameValuePairs.add(new BasicNameValuePair("ski_id","OtherScreenName"));
TextView text1 = (TextView) findViewById(R.id.otherscreenname_1);
//THESE 2 CHANGE DEPERNDING ON THE BUTTON PRESSED
//perform action.....
//AND ALSO HERE I NEED THE ID OF THE BUTTON
b1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent i = new Intent(OtherScreenName.this,MyActivity.class);
startActivity(i);
}
});
Can anyone suggest how to give arguments to my class and what it should be the type of them?
If I understand your question correctly, you want to pass arguments to the activity. Normally it would be done through the class constructor, however, activities can't have user defined constructors, only the default one; they can be instantiated only indirectly via intent creation. If you need to pass data between activities, do it by putting extras to bundles, for example:
bundle.putInt("intName",intValue);
then you can extract the data from bundle by
int intValue = bundle.getInt("intName");
Put extras before starting the activity:
Intent i = new Intent(this, YourActivity.class);
Bundle b = new Bundle();
b.putInt("intName",intValue);
i.putExtras(b);
startActivity(i);
and then read the extras in the onCreate method:
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
Bundle b = getIntent().getExtras();
int intValue;
if (b != null)
{
intValue= b.getInt("intName");
}
}
The same way you can pass other data types as String boolean etc. If this is not sufficient and you need to pass some more complex data, then use Parcelable interface.
You can pass parameters with an Intent by adding extra's to it, something like the following:
Intent i = new Intent(this, MyActivity.class);
i.putExtra("paramName", "value");
startActivity(i);
In your activity you can use the getIntent() method to retrieve the Intent and extract your parameter(s) from it:
Intent i = getIntent();
Bundle extras = i.getExtras();
String param = extras.getString("paramName", "default value");
You can place all the different text and data in your Intent, but you can also decide based on the value of an Intent parameter which data and text to retrieve. If it is a lot of data or text you are probably better off using the second approach.
If you want to pass object instead of simple data, you must use parcelable objects as it´s explained in: [http://developer.android.com/reference/android/os/Parcelable.html][1]
Reading and writing with parcelable objects is very similar to the way with a simple data
Intent intent = new Intent(this ,newthing.class);
Bundle b = new Bundle();
b.putParcelable("results", listOfResults);
intent.putExtras(b);
startActivityForResult(intent,0);
Intent i = getIntent();
Bundle extras = i.getExtras();
String param = extras.getParcelable("results");

Categories

Resources