Bundle is null after setting it in Intent - android

I know there are questions like:
android-intent-bundle-always-null and intent-bundle-returns-null-every-time but there is no correct answer.
In my Activity 1:
public void goToMapView(Info info) {
Intent intent = new Intent(getApplicationContext(), MapViewActivity.class);
//intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
intent.putExtra("asdf", true);
info.write(intent);
startActivity(intent);
}
In Info:
public void write(Intent intent) {
Bundle b = new Bundle();
b.putInt(AppConstants.ID_KEY, id);
... //many other attributes
intent.putExtra(AppConstants.BUNDLE_NAME, b);
}
public static Info read(Bundle bundle) {
Info info = new Info();
info.setId(bundle.getInt(AppConstants.ID_KEY));
... //many other attributes
return info;
}
In MapViewActivity (Activity 2):
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.map_view);
Bundle extras = getIntent().getBundleExtra(AppConstants.BUNDLE_NAME);
info = Info.read(extras);
...
}
Problem is that extras bundle is always null. I have debugged it and Intent (intent = getIntent()) has all fields set to null except one indicating what class this is (MapViewActivity).
I've also tried putting the bundle via intent.putExtras(b) with the same effect.
The intent.putExtra("asdf", true) is for debugging reasons only - I cannot get this data too (because getIntent() has almost all fields set to null).
EDIT
The answers below are correct and working. This was my fault. I didn't correctly passed my bundle to new intent.

I am not sure what "Info" is for, but I suggest making the most basic passing of data from one activity to another first before involving other data objects.
Activity1
Intent intent = new Intent(Activity1.this, Activity2.class);
intent.putExtra("asdf", true);
info.write(intent);
startActivity(intent);
Activity2
Bundle bundle = getIntent.getExtras();
if (bundle!=null) {
if(bundle.containsKey("asdf") {
boolean asdf = bundle.getBooleanExtra("asdf");
Log.i("Activity2 Log", "asdf:"+String.valueOf(asdf));
}
} else {
Log.i("Activity2 Log", "asdf is null");
}

Activity 1
Intent intent = new Intent(getApplicationContext(), MapViewActivity.class);
Bundle b = new Bundle();
b.putBoolean("asdf", true);
b.putInt(AppConstants.ID_KEY, id);
intent.putExtras(b);
startActivity(intent);
Activity 2
Bundle extras = getIntent().getExtras();
boolean bool = extras.getBoolean("asdf");
int m_int = extras.getInt(AppConstants.ID_KEY,-1);

Related

How to decide which Activity will start after the next in android

Hi there I want to call an Activity an decide which is the next activity to start after the Dialog finished like:
Intent dialog_intent = new Intent(_parent, MyActivityA.class);
dialog_intent.putExtra(MyActivityA.EXTRA_PARENT, MyActivityB.class);
I am getting the extra like this in the Oncreate of MyActivityA:
Type _parent = null; // this is a class variable
Intent intent = getIntent();
Bundle bundle = intent.getExtras();
if (bundle != null) {
Set<String> keys = bundle.keySet();
Iterator<String> it = keys.iterator();
while (it.hasNext())
{
String key = it.next();
if(key.equals(EXTRA_PARENT))
_parent = (Type)bundle.get(EXTRA_PARENT);
}
}
in the finishDialog method I do this :
public void finishDialog(View v)
{
try
{
Intent intent = null;
if(_parent != null && _parent instanceof Activity)
{
intent = new Intent(this, _parent.getClass());
}
else
{
intent = new Intent(this, DefaultActivity.class);
}
if(intent != null)
{
intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
startActivity(intent);
}
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
finish();
}
2 Questions:
Why does _parent instaneof Activity not work. I think MyActivity is a Type which inherits of Activity
Trying to start the activity does not work. But if i put in
new Intent(this,MyActivityB.class);
it works! What am I doing wrong. Is there any other way to do this
You can simply pass integer value and map it to corresponding activity.
Intent dialog_intent = new Intent(_parent, MyActivityA.class);
dialog_intent.putExtra("EXTRA_PARENT", 1);
Now in your activity get this EXTRA_PARENT and call activity
Bundle extras = getIntent().getExtras();
int extraParent = extras.getInt("EXTRA_PARENT");
if(extraParent == 1)
{
Intent dialog_intent = new Intent(_parent, MyActivityB.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
startActivity(intent);
}

Show Selected value from spinner in one activity to the textview in another activity

I know there are few questions similar to mine and I have tried all the suggested solutions mentioned in the existing questions however it's still not working. Pretty sure I'm doing something wrong logically but unable to figure out where. Please point me to right direction.
Spinner activity code method
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3)
{
// TODO Auto-generated method stub
Intent main = new Intent (this, MainActivity.class);
Bundle myBundle = new Bundle();
String chosenAppType = appliancestypespinner.getSelectedItem().toString();
myBundle.putString("appliance type spinner",chosenAppType);
main.putExtra("chose appliance type", myBundle);
startActivity(main);
}
TextView Activity (getting the spinner value as string in textview (tvApplianceType = textview )
Bundle myBundle = this.getIntent().getExtras();
if(myBundle == null)
{
return;
}
String str_recieved_appType = myBundle.getString("appliance type spinner");
if (str_recieved_appType != null)
{
tvApplianceType.setText(str_recieved_appType);
}
}
Just to add that I'm also passing the value of Edittext present from the Spinner activity to the same textview that i'm passing the spinner value to that EditText to TextView is working fine. Is it not working because i'm using the same textview for both operations however either operation need to work at one time :/. So either edittext to textView OR spinner to textview.
Edittext Code
String str_appliance_type = et_input_Appliance_Type.getText().toString().trim();
if (!str_appliance_type.equals(""))
{
data.add(str_appliance_type );
aa = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, data );
appliancestypespinner.setAdapter(aa);
//passing edittext value to MainActivity class
Bundle carrier = new Bundle();
Intent i = new Intent (this, MainActivity.class);
i.putExtra("appliance type",str_appliance_type);
startActivity(i);
}
Passing Value fro Editext to textview
//getting the edittext value from inputAppliance class
Bundle carrier = getIntent().getExtras();
if(carrier == null)
{
return;
}
String str_recieved = carrier.getString("appliance type");
if (str_recieved != null)
{
tvApplianceType.setText(str_recieved);
}
String str = appliancestypespinner.getSelectedItem().toString(); // I assume this line is proper.
Intent i = new Intent (this, MainActivity.class);
Bundle b = new Bundle();
b.putString("your_key", str);
i.putExtras(b);
startActivity(i);
In MainActivity
Bundle b = getIntent().getExtras();
String s = b.getString("your_key");
your_textView.setText(s);
Intent main = new Intent (this, MainActivity.class);
main.putExtra("SELECTED_DATA", appliancestypespinner.getSelectedItem().toString());
startActivity(main);
In Another Activity:
String data=this.getIntent.getStringExtra("SELECTED_DATA");
Try using Intent:
Spinner Activity:
Intent calculate = new Intent(getApplicationContext(),Second.class);
calculate.putExtra("carry_name", entername.getText().toString());
On Second Activity:
Intent intent = getIntent();
String name = intent.getStringExtra("carry_name");
congrates.setText("Congratulation " + name);
Change this
main.putExtra("chose appliance type", myBundle);
startActivity(main);
to
main.putExtras(myBundle);
startActivity(main);

How to send datafrom one activity to another in Android? [duplicate]

This question already has answers here:
How do I pass data between Activities in Android application?
(53 answers)
Closed 8 years ago.
My app has 2 activites.
The first activity is just a simple form where a user enters course information(class title, professor..etc.) the first activity passes the data which is supposed to be stored in a list.
In the second activity. The problem is that only the first course gets stored in the list, after the first time nothing new gets added to the second activity.
How can i do this ?
In your first activity :
Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
intent.putExtra("login", jObj.getString(KEY_LOGIN));
intent.putExtra("mdp", jObj.getString(KEY_MDP));
intent.putExtra("prenom", jObj.getString(KEY_PRENOM));
intent.putExtra("nom", jObj.getString(KEY_NOM));
intent.putExtra("mail", jObj.getString(KEY_MAIL));
intent.putExtra("tel", jObj.getString(KEY_TEL));
startActivity(intent);
In your second activity :
Intent intent = getIntent();
if (intent != null) {
login = intent.getStringExtra("login");
mdp = intent.getStringExtra("mdp");
items.add(intent.getStringExtra("login"));
items.add(intent.getStringExtra("prenom"));
items.add(intent.getStringExtra("nom"));
items.add(intent.getStringExtra("mail"));
items.add(intent.getStringExtra("tel"));
}
Generally you can pass data from one Activity to another with an Intent like this:
In your first Activity:
// Create your Intent
Intent intent = new Intent(context, TargetActivity.class);
// Now you can add extras to the intent, you identify extras with a String key
intent.putExtra("text", someString);
intent.putExtra("amount", someInteger);
// Then you start your Activity with this Intent
startActivity(intent);
In your second Activity:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Only get data from Intent when the Activity is new
if(savedInstanceState == null) {
Intent intent = getIntent();
// Now you read the values from the Intent
String someString = intent.getStringExtra("text");
int someInteger = intent.getIntExtra("amount", 0);
}
}
hey its simple look at the code
for sending
Intent i = new Intent(getApplicationContext(), Confirmation.class)
i.putExtra("name",etName.getText().toString()));
i.putExtra("pass",etPass.getText().toString());
startActivity(i);
for recieving in next activity
Bundle extras = getIntent().getExtras();
String strEmployeeID="";
if (extras != null)
{
String value = extras.getString("name");
String value1 = extras.getString("pass");
//
Toast.makeText(getBaseContext(), value, Toast.LENGTH_LONG).show();
strEmployeeID = value;
strEmployeePass = value1;
}
just simply do it like that:
passing data from 1st activity to 2nd activity:
Intent intent = new Intent(yourafirstctivity.this,Yoursecondactivity.class);
intent.putExtra("yourkey", yourvalue);
intent.putExtra("yourkey", yourvalue);
intent.putExtra("yourkey", yourvalue);
startActivity(intent);
and get the data from 1st activity to 2nd activity:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.yourlayout);
String yourstring = getIntent().getExtras().getString("yourkey");
String yourstring = getIntent().getExtras().getString("yourkey");
String yourstring = getIntent().getExtras().getString("yourkey");
}
if you want to send data between two Activities .You must call following one,
startActivityForResult(getcontext(), SecondActivity.class);
On calling this,Activity starts the second.In turn, second response to this intent and reply back with necessary data.
public void startActivityForResult(Intent intent, int requestCode, Bundle options) {
if (mParent == null) {
Instrumentation.ActivityResult ar =
mInstrumentation.execStartActivity(
this, mMainThread.getApplicationThread(), mToken, this,
intent, requestCode, options);
if (ar != null) {
mMainThread.sendActivityResult(
mToken, mEmbeddedID, requestCode, ar.getResultCode(),
ar.getResultData());
}
if (requestCode >= 0) {
mStartedActivity = true;
}
} else {
if (options != null) {
mParent.startActivityFromChild(this, intent, requestCode, options);
} else {
// Note we want to go through this method for compatibility with
// existing applications that may have overridden it.
mParent.startActivityFromChild(this, intent, requestCode);
}
}
}
Following one you must call this on the First Activity (from where the intent called).It helps to recieves the reply data from second Activity.
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if(!isFeatureInstalled(getContext()))
return;
if(requestCode == GET_REMINDER_DETAILS && resultCode == Activity.RESULT_OK)
{
mFilled = true;
fillDataFromIntent(data);
checkALertRequired.setChecked(true);
}
}

Reload same activity but pass bundle

I am trying to reload my activity and pass a bundle, but I'm getting an empty (null) bundle.
Reload activity:
Intent intent = new Intent(MyActivity.this, MyActivity.class);
Bundle bundle = new Bundle();
bundle.putInt("key", 1);
intent.putExtras(bundle);
MyActivity.this.finish();
startActivity(intent);
onCreate activity and I should get the bundle:
#Override
public void onCreate(Bundle savedInstance)
{
if (savedInstance != null)
{
}
else
{
Log.i("d", "IS NULL !");
}
}
I'm getting null.
In OnCreate() you should do like this :
if(getIntent().getExtras() != null) {
Bundle extras = getIntent().getExtras();
Log.i("Value", extras.getString("key"));
}
Instead of this
if (savedInstance != null){
}
First start the activity and then call finish() as follows:
Intent intent = new Intent(MyActivity.this, MyActivity.class);
Bundle bundle = new Bundle();
bundle.putInt("key", 1);
intent.putExtras(bundle);
startActivity(intent);
MyActivity.this.finish();
Then receive the bundle extras like this:
Bundle bundle = getIntent().getExtras();
Finally, you can put conditions to check if it's correct like:
if(bundle != null)
{
}
else
{
Log.i("d", "IS NULL !");
}

Transfer data from one screen to another on android eclipse

Hi am newbie to java and android.
Assume function demo() from the screenone is going to display some values on Textview which is on the same screen(screenone).
But i need to display that resultant value to the next screen ie.(screen two)
public void demo(){
{
.....
.....
}
So i have included these line to
screenoneActivity.java
Intent nextScreen = new Intent(getApplicationContext(), SecondtwoActivity.class);
nextScreen.putExtra("","");
startActivity(nextScreen);
demo();
ScreentwoActivity.java
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main1);
TextView txtName = (TextView) findViewById(R.id.textView1);
Intent i = getIntent();
txtName.setText(name);
I did these things so far.I don't know how to do transfer data from demo() function to the next screen.
Can anyone give me clues or ideas to achieve this.
Thanks a lot!..
You need to send some value into the putExtra methods parameters to be able to get something out of it.
In your first activity(A):
Intent i = new Intent(A.this, B.class);
i.putExtra("someName", variableThatYouNeedToPass);
startActivity(i);
In your second activity(B):
Bundle extras = getIntent().getExtras();
int fetchedVariable = extras.getInt("someName");
Write the below code in demo() function:
Intent nextScreen = new Intent(getApplicationContext(), SecondtwoActivity.class);
nextScreen.putExtra("","");
startActivity(nextScreen);
In nextScreen.putExtra("",""); provide some key and value like:
nextScreen.putExtra("name","ABC");
Now in SecondActivity, write:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main1);
TextView txtName = (TextView) findViewById(R.id.textView1);
Intent i = getIntent();
Bundle bundle = i.getExtras();
txtName.setText(bundle.getString("name"));
In ScreenoneActivity
Intent act2=new Intent(this,Activity2.class);
act2.putExtra("A",a);
startActivity(act2);
In ScreentwoActivity class
Intent i = getIntent();
Bundle extras = getIntent().getExtras();
int a = extras.getInt("A");
txtName.setText(a);
in onCreate :
Bundle extras = getIntent().getExtras();
String value;
if (extras != null)
{
value= extras.getString("key");
}
https://stackoverflow.com/questions/10752501/how-can-we-go-to-next-page-in-android/10752516#10752516
google it is very basic .....
android using intent....
Vogella Ariticle
in activity 1-
Intent i = new Intent(this, ActivityTwo.class);
i.putExtra("Value1", "This value one for ActivityTwo ");
i.putExtra("Value2", "This value two ActivityTwo");
startActivity(i);
In activity 2 - in onCreate finction
Bundle extras = getIntent().getExtras();
if (extras == null) {
return;
}
// Get data via the key
String value1 = extras.getString(Intent.EXTRA_TEXT);
if (value1 != null) {
// Do something with the data
}

Categories

Resources