I am trying to open another activity by intent and putExtra.
It used to run fine previously, but now it crashes.
startActivity(new Intent(ForgotPassword.this, OtpVerification.class)
.putExtra("user", user)
.putExtra("otp", verificationId));
finish();
the user here is a class object and am receiving it in another activity
user = getIntent().getParcelableExtra("user");
the app is not even going to another activty and crashes without any error message in logcat.
It only happens if i add .putExtra code
Try adding the putExtra in the following way
Intent intent = new Intent(ForgotPassword.this, OtpVerification.class);
intent.putExtra("user", user);
intent.putExtra("otp", verificationId);
startActivity(intent);
finish();
and then get the intent extras
Intent intent= getIntent();
Bundle extras = intent.getExtras();
if(extras != null)
String data = extras.getString("keyName");
Your user is an Object that you're trying to pass from one activity to another.
So you can create a custom class that implements the Serializable interface.(you must be able to use Parcelable too but i don't have experience with it )
//To pass:
intent.putExtra("User", user);
// To retrieve object in second Activity
getIntent().getSerializableExtra("User");
Point to note: each nested class in your main custom class must have implemented the Serializable interface
eg:
class MainClass implements Serializable {
public MainClass() {}
public static class ChildClass implements Serializable {
public ChildClass() {}
}
}
Ref:How to pass an object from one activity to another on Android
Thank you for your suggestions, the actual problem I found was that my user object had a variable called image (String type) and had a value of length about 1,000,000 (I was storing string encoded image using base64 in it). Once i decreased the size to thousands , it worked. I don't know why the app cannot send such large data across activities.
Related
I am creating an Android app. One of the functions is to collect some data (item name, item ID and the barcode string) from the user .
Activity1 is a form. User enters the item name and item number manually. For the barcode string, user clicks on the "scan" button then Activity2 (Scanner) is started in order to scan and read the barcode. Once the barcode is read, Activity1 (the form) starts again and all data should appear on the form.
When Activity2 starts by Intent, Activity1 is killed. So, I have to get the item name and item number and store them temporarily before staring the Intent. Then when Activity1 starts again, those data will be rendered on the form again.
Now I am thinking to use Intent Extra to keep the item name and number, and pass them to Activity2 and back to Activity1. Given that Activity2 doesn't need those data, I wonder if that is the right way to do in this scenario. Is there any better way? Should I use Shared Preferences instead?
In Your first activity use put extra argument to intent like this:
// Assuming Activity2.class is second activity
Intent intent = new Intent(this, Activity2.class);
intent.putExtra("variable_name", var); // here you are passing var to second activity
startActivity(intent);
Then in second activity retrieve argument like this:
String var2 = getIntent().getStringExtra(variable_name);
You could create a singleton class and expose setter(for saving) and getter (for retrieving) methods for the model objects (here two private string variables).This class will be alive with your application:
public class MyClass{
private static MyClass instance=null;
public static getInstance(){
if(instance==null){
instance=new MyClass();
}
return instance;
}
private String itemName;
private String itemNumber;
//setter and getter methods here
}
why need to kill the Activity 1, try to call
on Activity 1
declare private int SCAN_BARCODE_REQUEST = 101;
and then
//finish(); dont use this to destroy activity 1
startActivityForResult(new intent(this,Activity2.class), SCAN_BARCODE_REQUEST);
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == SCAN_BARCODE_REQUEST) {
if (resultCode == RESULT_OK) {
String barcode = data.getStringExtra("BARCODE");
//handle your barcode string here
}
}
}
on your Activity 2,
change your start Activity1 with
Intent intent = new Intent();
intent.putExtra("BARCODE", barcodeString);
setResult(RESULT_OK, intent);
finish();
You can use SharedPreferences.
You can learn how to use them here:
https://www.tutorialspoint.com/android/android_shared_preferences.htm
https://developer.android.com/training/basics/data-storage/shared-preferences.html
SharedPreferences is a really good solution for such applications. It is very simple and easy to use and implement.
In Xamarin, how can I pass a user defined object (a Class that I have written) to a different activity?
The object I wish to pass is an item in a List called:
_mapLocationList[0]
The item is of type:
MapLocation
Here is my current code:
intent.PutExtra ("MapLocation", _mapLocationList[0]);
Can the above method be used to pass an object? If not, how can I do this?
Thanks in advance
The simplest way of passing objects to an Activity is using Intents. For this, you'll need your class to implement Serializable or Parcelable.
This way, you would put your object in the Intent via the putExtra("myobject", object) method, and then in the other Activity recover it with getSerializableExtra("myobject").
For example:
In the first Activity:
final Intent intent = new Intent(this, SecondActivity.class);
intent.putExtra("my_class", your_maplocation_object);
startActivity(intent);
Then in your second Activity, you would do:
final Intent passedIntent = getIntent();
final MapLocation my_class = (MapLocation) passedIntent.getSerializableExtra("my_class");
There is a more simple and very pleasant way : use Googe GSON library.
Serialization in Activity A:
intent.putExtra("mapLocExtra", new Gson().toJson(myMapLocationObject,MapLocation.class);
startActivity(intent);
***Deserialisation in Activity B: *
#Oncreate(...){
String mapLocJson= getIntent().getExtra("mapLocExtra").toString();
MapLocation mylocationObject= new Gson().fromJson(maplocJson,MapLocation.Class);
//do Stuff With mylocationObject
}
More infos : https://code.google.com/p/google-gson/
I am wondering about the best way to design / program this:
If I have a Boolean value (let's say whether the user has extra power or not) and I need to pass it from Activity A to B to C. Should I add it to an intent from each activity to another OR should I just store it in a static variable and access it every time?
Its is safer to pass it in the intent. sometimes android kills apps without warning when it needs memory and your static values will not be retained on the other hand intent extras are kept. if you want to push it a little further, use shared preference. its designed using Map data struct so speed will not be a problem.
Android Intents have a putExtra method that you can use to pass variables from one activity to another.
public class ActivityA extends Activity {
...
private void startActivityB() {
Intent intent = new Intent(this, ActivityB.class);
intent.putExtra("HAS EXTRA POWER", false);
startActivity(intent);
}
}
public class ActivityB exntends Activity {
Bundle bundle;
private void hasExtraPower() {
bundle = getIntent().getExtras();
if(!bundle.getBoolean("HAS EXTRA POWER")
// do something
else
// do something else
}
}
Passing data through Intent
If you use that only in that activity that's fine but
When u need to pass to other layer like viewmodel that will make your operation's speed slower
I want to transfer a String value from one class to another class in same package. The second class is called using intent in onclick of a menu item. I used the code
Intent i = new Intent(TouristGuideActivity.this, PointOfInterest.class);
i.putExtra("videoId", videoId);
startActivity(i);
in first class and then in second class,
String address=getIntent().getExtras().getString("videoId");`
But when I click on the menu item, I get a force close. If I remove that put Extra part, it works ine. But in that case I can't send the string. Please help!
Intent intent=new Intent(this,secondclass.class);
intent.putExtra("videoId",videoId);
startActivity(intent);
and in your second class try 2 get these items by doing
Bundle extras = getIntent().getExtras();
if(extras!=null){
String videoId=extras.getString("videoId");
}
just after u define your class i hope dis would help u out 2 get the values on other page
Make sure that you are getting the same type, for example, if you put a String, get a String.
Then try with:
getIntent().getStringExtra("videoId");
this is my class name
public class Register extends TabGroupActivity
I am calling 2nd activity through
startChildActivity("Register", new Intent(Register.this,RegisterForm.class));
can any one help me how to transfer some data through this method
Intent i = new Intent(Register.this,RegisterForm.class);
i.putExtra("name", yourdata);//i assume you are adding some string data
startChildActivity("Register", i);
//in RegisterForm.class
Intent i = RegisterForm.this.getIntent();
i.getStringExtra("name", "default Value you want");
You have to implement an interface called Parecelable and write your object inside a Parcel so that you can transport it through the Intent
This tutorial will teach you how to do that
http://www.codexperience.co.za/post/passing-an-object-between-activities-using-an-intent