Thursday, July 29, 2010

Passing information between Activities

Hi

There are serveral methods for passing information between Activities. But its not only the method that takes us to better results but also it depends on our ability to choose the optimized method out of available options.

1. Shared Prefs
2. Internal Storage
3. External Storage
4. SQLite Databases
5. Network Connection
6. Stuffing intent Object

So main point of concern here is Stuffing intent object.

Let's say we 've an Activity A that starts Activity B. In activity A before starting Activity B using intent object , first we will stuff the intent object which desired information that activity A needs to send to activity B.

public class ActivityA extends Activity{
....

Intent i = new Intent(A.this,B.class);

i.put("[name of variable]",[default value for variable]);

// eg. i.put("type","1");


startActivity(i);

}

Now in Activity B

public class ActivityB extends Activity {

....

Intent i = getIntent();

i.get..... methods can be used to get the desired values keeping the types of values in mind before storing them in variables.



}

No comments:

Post a Comment