Passing a reference of an object to activity - android

I have a reference to an anonymous class object that I want to pass to an activity. It is a kind of reference to a callback fn which gets called based on an action on activity i.e. click of button. But, I dont know a way to do so as activities can not be instantiated directly (done only through startActivity) and using intents will pass my object by value not by reference. So, I need tips on a good solution. I want to avoid statics.

Short Answer: No You cannot pass objects as references to activities.

Try this
CustomListing currentListing = new CustomListing();
Intent i = new Intent();
Bundle b = new Bundle();
b.putParcelable(Constants.CUSTOM_LISTING, currentListing);
i.putExtras(b);
i.setClass(this, SearchDetailsActivity.class);
startActivity(i);
Afterwards to call
Bundle b = this.getIntent().getExtras();
if (b != null)
mCurrentListing = b.getParcelable(Constants.CUSTOM_LISTING);

Here is the answer :
Create a public static method in the Activity to which you want to pass the reference of an object.
public static void openActivity(Activity activity, Class object){
YourNextActivity.object = object;
Intent intent = new Intent(activity, YourNextActivity.class);
activity.startActivity(intent);
}
Call this method from current Activity :
YourNextActivity.openActivity(this, classObject);

Related

Passing data from two activities to a third activity in android

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

Android: How to pass MULTIPLE ArrayList<String> values and one ArrayList<LatLng> from one activity to another in a bundle? Not working

I have several List<String> variables to pass from Splash to the Main activity:
1) I read somewhere that I can pass them as ArrayList<String> from Splash to Main, and it works... i.e.
I can receive only the first ArrayList<String> variable. In my bundle below, I am not able to receive the second ArrayList<String>. (array_list2) Why?
2) How to pass ArrayList<LatLng> from one activity to another
First Activity:
Intent intent = new Intent(SplashActivity.this, MainActivity.class);
Bundle b=new Bundle();
b.putStringArrayList("array_list1",(ArrayList<String>)Names);
b.putStringArrayList("array_list2",(ArrayList<String>)City);
b.putStringArrayList("array_list3",(ArrayList<String>)Country);
b.putStringArrayList("array_list4",(ArrayList<String>)Code);
b.putStringArrayList("array_list5",(ArrayList<LatLng>)coordinates); //ERROR in this line, type mismatch!
intent.putExtras(b);
startActivity(intent);
Second Activity:
Bundle b = getIntent().getExtras();
if (b != null) {
testList1 = b.getStringArrayList("array_list1");
testList2 = b.getStringArrayList("array_list2"); //THIS gives the same arraylist as testList1 and it is incorrect!
Log.e("TESTLIST1",testList1.toString()); //just using Log.e to view o/p as test
Log.e("TESTLIST2",testList1.toString());
Please answer both my questions. None of the other topics helped me, and I spent over 2 hours on this.
Thank you.
putStringArrayList() will not support for ArrayList .
use
putParcelableArrayList();
instead of
putStingArrayList();
or else you can use direct method of intent.
Change your code somethng like this:
Intent intent = new Intent(SplashActivity.this, MainActivity.class);
Bundle b=new Bundle();
b.putStringArrayList("array_list1",(ArrayList<String>)Names);
b.putStringArrayList("array_list2",(ArrayList<String>)City);
b.putStringArrayList("array_list3",(ArrayList<String>)Country);
b.putStringArrayList("array_list4",(ArrayList<String>)Code);
If you want to pass through bundle object use below line of code.
b.putParcelableArrayList("array_list5",(ArrayList<LatLng>)coordinates);
Otherwise pass through intent object.
intent.putParcelableArrayListExtra("array_list5",coordinates);
intent.putExtras(b);
startActivity(intent);
Because LatLng class implements Parcelable interface, so instead of using Bundle. putStringArrayList use Bundle.putParcelableArrayList to send ArrayList which contains class object which is implementing Parcelable interface. Use
b.putParcelableArrayList("array_list5",coordinates);
How to use parcelable with List or ArrayList - What is
the syntax?
Do it as:
ArrayList<String> arrCoordinates = new ArrayList<>(coordinates.size());
arrCoordinates.addAll(coordinates);
b.putParcelableArrayList("array_list5",arrCoordinates);
and get array_list5 as from Bundle
ArrayList<LatLng> coordinates = b.getParcelableArrayList("array_list5");

How to pass Multiple information from one screen to another screen in android

I have a sign-up form, and I have to pass all info filled in that form to another screen. I know how to display if there is one field, but in sign-up form there are multiple fields. so I want to know how to display all the info.
If you are launching a new activity, just create a Bundle, add your values, and pass it into the new activity by attaching it to the Intent you are using:
/*
* In your first Activity:
*/
String value = "something you want to pass along";
String anotherValue = "another something you would like to pass along";
Bundle bundle = new Bundle();
bundle.putString("value", value);
bundle.putString("another value", anotherValue);
// create your intent
intent.putExtra(bundle);
startActivity(intent);
/*
* Then in your second activity:
*/
Bundle bundle = this.getIntent().getExtras();
String value = bundle.getString("value");
String anotherValue = bundle.getString("another value");
To pass User data(multiple info) from one screen to another screen :
Create a model for user with setter and getter method.
make this class Serializable or Parcelable (Prefer) .
Create object of user class and set all data using setter method.
Pass this object from one activity to another by using putSerializable.
Person mPerson = new Person();
mPerson.setAge(25);
Intent mIntent = new Intent(Activity1.this, Activity2.class);
Bundle mBundle = new Bundle();
mBundle.putSerializable(SER_KEY,mPerson);
mIntent.putExtras(mBundle);
startActivity(mIntent);
And get this object from activity 2 in on create methode.
Person mPerson = (Person)getIntent().getSerializableExtra(SER_KEY);
and SER_KEY will be same.
for more detail please go to this link:
http://www.easyinfogeek.com/2014/01/android-tutorial-two-methods-of-passing.html
I hope it will work for you.
You can make use of bundle for passing values from one screen to other
Passing a Bundle on startActivity()?

Pass an object to a new activity

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/

how to know the calling activity in android

I have an activity which is called by few other activities. For example: I have Activity1,Activity2,Activity3.
Activity1 calls Activity2 and pass parameter.
Activity3 also calls Activity2 and pass parameter.
Now based on the calling activity, Activity2 performs some task.
But how do I know which activity is calling Activity2??
can anybody plz help me??
If you start the activity with startActivityForResult(Intent, int), then you can get calling activity by getCallingActivity().getClassName().
A. If you can use startActivityForResult
As per Zain Ali's answer below: If you can start Activity with startActivityForResult() then you can get name of calling Activity class by this.getCallingActivity().getClassName();
B. If you can not use startActivityForResult
If you can not use startActivityForResult(), then you can use following method:
You can pass additional parameter in intent, check the value in activity and act accordingly.
1) Define an interface or constants class to define integer constants to indicate calling activity
public interface ActivityConstants {
public static final int ACTIVITY_1 = 1001;
public static final int ACTIVITY_2 = 1002;
public static final int ACTIVITY_3 = 1003;
}
2) Add extra parameter in intent while calling Activity2.
Intent act2 = new Intent(context, Activity2.class);
act2.putExtra("calling-activity", ActivityConstants.ACTIVITY_1);
// or ActivityConstants.ACTIVITY_3 if called form Activity3
startActivity(act2);
3) Check the value of this extra parameter in Activity2 and act accordingly..
int callingActivity = getIntent().getIntExtra("calling-activity", 0);
switch (callingActivity) {
case ActivityConstants.ACTIVITY_1:
// Activity2 is started from Activity1
break;
case ActivityConstants.ACTIVITY_3:
// Activity2 is started from Activity3
break;
}
In your calling activity (FirstActivity):
Intent i = new Intent(FirstActivity.this, SecondActivity.class);
i.putExtra("classFrom", FirstActivity.class.toString());
startActivity(i);
And add the following code in the onCreate of the called activity (SecondActivity):
Bundle bundle = getIntent().getExtras();
if (bundle.getString("classFrom").equals(FirstActivity.class.toString())) {
//Do some task
}
Notice that you should be carefully because the bundle object can't be null when you perform "b.getString("classFrom")".
You could pass an additional parameter that specifies the calling Activity.
I successfully use: (Activity).getLocalClassName()
Pass anything(String/ int etc.) to putExtra and base on that do the your work like
Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
intent.putExtra("PARENT_ACTIVITY_REF", "ParentActivityIsA");
startActivity(intent);
And then receive in child like
String parentActivityRef = intent.getStringExtra("PARENT_ACTIVITY_REF");
then
if (parentActivityRef.equals("ParentActivityIsA"){
// do your work here
}else if ...{
// ...
}else{
//...
}
I'm Using This line
if (((((ActivityManager) getSystemService(Context.ACTIVITY_SERVICE)).getRunningTasks(1).get(0).baseActivity)).compareTo(new ComponentName(getPackageName()
, AnyActivityWantToCheck.class.getName())) == 0){
// do somthing .....
}
i hope it work with you
Another solution, using companion object as in the following example:
class MainActivity: BaseActivity {
...
LoginActivity.callerActivity = this
it.context.startActivity(Intent(getActivity(), LoginActivity::class.java))
...
}
class LoginActivity : BaseActivity() {
companion object {
var callerActivity: AppCompatActivity? = null
}
...
callerActivity?.let {
it.callSomething()
}
}

Categories

Resources