String not shown from other activity used bundle - java

I wanted to get the name of pizza on bill page put it isn't displaying(blank).
Menu.java:
bundle = new Bundle();
bundle.putString("name",pizzaName);
y = new Intent(this,Bill.class);
y.putExtras(bundle);
startActivity(y);
Bill.java:
text1=(TextView)findViewById(R.id.textView6);
y= new Intent();
bundle=getIntent().getExtras();
Name=bundle.getString("name");
text1.setText();
Output:

You do not need to do y= new Intent(); in Bill.java
Change code in Bill.java as follows. Also, for better naming convention, change Bill.java to BillActivity.java
TextView text1 = (TextView) findViewById(R.id.textView6);
Bundle bundle = getIntent().getExtras();
if (bundle != null) {
String name = bundle.getString("name");
text1.setText(name);
}

You can simply pass and retrieve value like this.
// Creating and initializing an Intent object
Intent intent = new Intent(this, NextActivity.class);
// Attach the key value pair using putExtra to this intent
String pizza = "Margherita";
intent.putExtra("PIZZA_NAME", pizza);
// Start the activity
startActivity(intent);
// Get the current intent
Intent intent = getIntent();
// Get the attached extras from the intent
// We should use the same key as we used to attach the data.
String pizza = intent.getStringExtra("PIZZA_NAME");

Remove y= new Intent(); in your bill.class

No need to get y= new Intent(); when you get data from bundle
text1=(TextView)findViewById(R.id.textView6);
bundle=getIntent().getExtras();
Name=bundle.getString("name");
text1.setText();

Related

Passing string between Android Activities

I have a List Activity that when the user presses a line item, it passes the ID of the record to the next activity.
Intent intent = new Intent(CustomerListActivity.this, CustomerEditActivity.class);
intent.putExtra("CUSTOMER_ID", id);
startActivity(intent);
I can see the data in the intent when debugging on that activity; however, when I get into the next activity, the data is not coming up with the below code.
Intent i = getIntent();
Bundle b = i.getExtras();
String s = b.getString("CUSTOMER_ID");
I have debugged and poked around in the variables window, but I do not see the Customer_ID=# value as I do on the previous activity.
You should call String s = b.getStringExtra("CUSTOMER_ID");
Try this.It will work.Paste it on your next activity where you want to get data from intent.
String s= getIntent().getExtras().get("CUSTOMER_ID")+"";
This is how you can do it in a right way
//main activity
Intent intent = new Intent(getActivity(), TargetActivity.class);
intent.putExtra("ParamKey", "key_value");
getActivity().startActivity(intent);
****
//TargetActivity
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.target_activity_layout);
Bundle extras = getIntent().getExtras();
if (extras != null) {
if (extras.get("ParamKey") != null)
paramValue= extras.getString("ParamKey", "default_value");
}
You will have to call getStringExtra() method in New Activity to retrieve the value from Intent.
For example :
String custIdInNewAct= getIntent().getStringExtra("CUSTOMER_ID");
Bundle contains data sent by the calling activity so
Bundle b = getIntent().getExtras();
String s = b.getString("CUSTOMER_ID")`;

Android Studio: Passing variable through another variable doesn't work

I created a Method for my MainActivity to pass a String to my SecondActivity.
public void convertCurrency(View view)
{
intent.putExtra("NO", "My String");
startActivity(intent);
}
But in my SecondActivity in my OnCreate Method
Button back = (Button) findViewById(R.id.back);
TextView t = (TextView) findViewById(R.id.first_text);
Intent intent = new Intent(this, MainActivity.class);
String g = intent.getStringExtra("NO");
t.setText(g);
Nothing happens. But Why?
get Your variable in this way:
String yourVriable = getIntent().getStringExtra("NO")
don't new Your intent
Your are creating new intent.
In onCreate method call
String data = getIntent().getStringExtra(key);
replace Intent intent = new Intent(this, MainActivity.class);
by Intent intent = getIntent();
When you do intent.getStringExtra("NO"); your intent Object is new, so his Extra is empty.
You need to change the code in both the activities . You need to create intent and put your string into it, so your code will be.
public void convertCurrency(View view) {
Intent intent = new Intent(this, Main2Activity.class);
intent.putExtra("NO","My String");
startActivity(intent);
}
Note that in the intent constructor the first argument is the current activity and the second argument will be the activity that you are starting.So in the second activity you need to recieve the string. Code for that will be,
Button back = (Button) findViewById(R.id.back);
TextView t = (TextView) findViewById(R.id.first_text);
String g = getIntent().getStringExtra("NO")
t.setText(g);
I resolve the problem myself. There occurs an error if you have two Intent objects with the same name even if they are within separate methods.

Convert Bundle to Integer (Intents)

I am attempting to pass an int through an intent to another class and have managed to successfully pass through the integer however I am unsure as how to convert a Bundle to an Integer.
Code from Intent:
private void nextPage()
{
Intent intent = new Intent(this, Timer.class).putExtra("totalTime", totalTime);
startActivity(intent);
}
Code in Timer class:
Bundle time = getIntent().getExtras();
if(time == null)
{
timeDisp.setText("Failed.");
}
else
{
totalTimeMs = Integer.parseInt(String.valueOf(time));
timeDisp.setText(totalTimeMs);
}
Thanks in advance :)
Intent can hold directly all java primitives types and parcelable/serializable objects.
You may have a confusion that it can also hold Bundles.
Do you really need to put your integer in a Bundle? It can be true for multiple values that logically coupled.
Check Intent API.
If your totalTime is of type int which you pass through putExtra() the you can use:
int time = getIntent().getExtras().getInt("totalTime");
You are not adding your intent to a Bundle, so on the recieving activity you are trying to get data out of an empty Bundle.
You would Add the data to a bundle by:
private void nextPage()
{
Intent intent = new Intent(this, Timer.class);
Bundle b = new Bundle():
b.putString("totalTime", totaltime);
intent.putExtras(b);
startActivity(intent);
}
Then you would retrieve the String from the bundle:
Intent intent = getIntent();
Bundle extras = intent.getExtras();
String time = extras.getString("totalTime");

Can I send class as extra with intent?

I'm trying to pass class name with extra, how to do that ?
Intent p = new Intent(StartScreen.this, Setting.class);
p.putExtra(" ",StartScreen.this);
I want to get the class name in Setting class but I don't want it to be String cause I'm going to use this class name like that :
Bundle extras = getIntent().getExtras();
extras.getString("class");
Intent i = new Intent(Setting.this, class);
startActivity(i);
you can use this code
Intent p = new Intent(StartScreen.this, Setting.class);
p.putExtra("class","packagename.classname");
and in setting class
Bundle extras = getIntent().getExtras();
String classname=extras.getString("class");
Class<?> clazz = Class.forName(classname);
Intent i = new Intent(Setting.this, clazz);
startActivity(i);
A tidier way than the accepted answer would be to use Serializable or Parcelable.
Here is an example of how to do it using Serializable:
In your first activity...
Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
intent.putExtra("EXTRA_NEXT_ACTIVITY_CLASS", ThirdActivity.class);
startActivity(intent);
Then in your second activity...
Bundle extras = getIntent().getExtras();
Class nextActivityClass = (Class<Activity>)extras.getSerializable("EXTRA_NEXT_ACTIVITY_CLASS");
Intent intent = new Intent(SecondActivity.this, nextActivityClass);
startActivity(intent);
Doing it with Parcelable is pretty much the same, except you would replace extras.getSerializable("EXTRA_NEXT_ACTIVITY_CLASS") in the above code with extras.getParcelable("EXTRA_NEXT_ACTIVITY_CLASS").
The Parcelable method will be faster, but harder to set up (as you need to make your third Activity implement Parcelable - see http://developer.android.com/reference/android/os/Parcelable.html).

Action before startActivity()

I'm new in Android development and I've a problem when I create my new activity.
I want to use my activity before start it. For example, I have try it:
MyActivity menu = new MyActivity();
menu.setXmppreception(reception);
Intent intent = new Intent(Screen.this,MyActivity.class);
Screen.this.startActivity(intent);
But, my "menu" and "MyActivity.class" aren't the same instance. Consequently I have try it:
MyActivity menu = new MyActivity();
menu.setXmppreception(reception);
Intent intent = new Intent(Screen.this,menu);
Screen.this.startActivity(intent);
But it doesn't work...
Have you a solution for help me?
Thanks for help and sorry for the bad english.
You can't do that like you want, if you are looking to pass data between an activity you must use Extras, and you can pass just Serializable items.
First Context (can be Activity/Service etc)
You have a few options:
1) Use the Bundle from the Intent:
Intent mIntent = new Intent(this, Example.class);
Bundle extras = mIntent.getExtras();
extras.putString(key, value);
2) Create a new Bundle
Intent mIntent = new Intent(this, Example.class);
Bundle mBundle = new Bundle();
mBundle.putString(key, value);
mIntent.putExtras(mBundle);
3) Use the putExtra() shortcut method of the Intent
Intent mIntent = new Intent(this, Example.class);
mIntent.putExtra(key, value);
New Context (can be Activity/Service etc)
Intent myIntent = getIntent(); // this getter is just for example purpose, can differ
if (myIntent !=null && myIntent.getExtras()!=null)
String value = myIntent.getExtras().getString(key);
}
NOTE: Bundles have "get" and "put" methods for all the primitive types, Parcelables, and Serializables. I just used Strings for demonstrational purposes.
you don't have to create the new activity yourself, Android system does it for you.
If you want to go from Screen Activity to MyActivity, you can do this :
Intent intent = new Intent(Screen.this,MyActivity.class);
startActivity(intent);
and then, in you MyClass java file, in the onCreate method, you could do :
this.setXmppreception(reception);
This way, I think you get you wanted, no?

Categories