views:

231

answers:

2

Hi,

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.

+1  A: 

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?

Sephy
+1  A: 

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.extras.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.

Pentium10
I think I'll use the third option (putExtra() method). If I understood everything I must implements "Serializable" in my XMPPReception class, isn't it?Thanks again.
Alex R.
Probably yeah `reception` must be Serializable, but Eclipse will tell for you.
Pentium10
Important on SO, you have to mark accepted answers by using the tick on the left of the posted answer, below the voting. This will increase your rate.
Pentium10