how to transfer data from one child activity to another using startChildActivity? - android

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

Related

App crashing on starting another activity with intent and .putExtra

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.

Passing a reference of an object to activity

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);

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 pass an array list from one activity to another without starting it

I have an activity which has an array list
ArrayList<String> array = new ArrayList<String>();
i want this array list to be passed to another activity when a Save button is clicked, but i don't want that activity to start...
Usually this code helps in starting an activity
public void onClick(View v) {
if (v==Save)
{
Bundle bundle = new Bundle();
bundle.putStringArrayList("DONE", activeURL);
Intent myIntent = new Intent(Reader2.this, Aggregator.class);
myIntent.putExtra("reader2", activeURL);
startActivity(intent);
}
}
but i just want to pass the array and start another activity.
Can you please help me ?
Thanks in advance.
You can declare you ArrayList as a static one like this,
public static ArrayList<String> array = new ArrayList<String>();
By doing this you can access your ArrayList from anywhere by
activity_name.array;
where activity_name is the activity or class in which you declare the static ArrayList
you can pass an intent to already running activity.. follow this http://www.helloandroid.com/tutorials/communicating-between-running-activities for that
in the intent you can add an extra like this
Intent contactsIntent = new Intent(getApplicationContext(),
ContactCards.class);
contactsIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
appWidgetId);
//Bundle containing the serialized list
Bundle extraContacts = new Bundle();
//Putting the array list templist is the array list here
extraContacts.putSerializable("CONTACT_KEY", tempList);
extraContacts.putString("CALL_STRING", CALL_STRING);
contactsIntent.putExtras(extraContacts);
contactsIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(contactsIntent);
Based on the fact that you mention a 'Save' button, I think you would rather save this data to SharedPreferences or an SQLiteDatabase.
I am unsure of what it would mean to 'save' some data to another Activity and not start it.
With your data in a persisted state, you should be able to access it from any one of your other Activity's, which is what is sounds like you are after.
use 1st activity
Intent i=new Intent(ArraylistpassActivity.this,second.class);
i.putStringArrayListExtra("key",arl);startActivity(i);
2nd activity:
arl=bundle.getStringArrayList("key");

Accessing instance of the parent activity?

Suppose I have a class first.java (activity class) and I start another activity in this class (second.java - activity class).
How can I access the instance of first.java from second.java?
Can someone give me a good explanation on this... An example would be great...
If you need your second activity to return some data to your first activity I recommend you use startActivityForResult() to start your second activity. Then in onResult() in your first activity you can do the work needed.
In First.java where you start Second.java:
Intent intent = new Intent(this, Second.class);
int requestCode = 1; // Or some number you choose
startActivityForResult(intent, requestCode);
The result method:
protected void onActivityResult (int requestCode, int resultCode, Intent data) {
// Collect data from the intent and use it
String value = data.getString("someValue");
}
In Second.java:
Intent intent = new Intent();
intent.putExtra("someValue", "data");
setResult(RESULT_OK, intent);
finish();
If you do not wish to wait for the Second activity to end before you do some work in the First activity, you could instead send a broadcast which the First activity reacts to.
You can simply call getParent() from the child activity.
I have no clue why other answers are so complicated.
Only this should work
class first
{
public static first instance;
oncreate()
{
instance = this;
}
}
first.instance is the required thing that is accessible from the second class
try this if this work 4 u.........
something like this.....
class first
{
public static first instance;
oncreate()
{
instance=this;
}
public static getInstance()
{
return instance;
}
}
now from second class call first.getInstance();
you can also directly acess instance in static way like this first.instance.......
Thanks...
You can't create an activity directly.
In the first activity take a static activity variable like this,
public static Activity activity;
In the onCreate do this.
activity = this;
Then in the second activity do this,
Activity activity = (your activity name).activity;
Edit:
For passing data from one activity to other activity this is not the way.
Above answer was to get activity instance from other activity which was initially asked.
To pass data from one activity to other activty generally use bundle. But if the data is not primitive data type, then use object class which should implement parcelable or serializable interface. Then through bundle only parcelable list of objects we can pass.

Categories

Resources