Skip to main content

Featured

Android studio “SDK tools directory is missing”

Following 2 possible solutions will resolve this problem :  Solution1 : To fix the problem, it was required that I list the path to my corporate PAC file by using  Configure -> "Appearance and Behavior" -> System Settings -> HTTP Proxy . I selected "Automatic proxy configuration url:" Delete your  ~/.Android*  folders (losing all of your settings :/). Run Android Studio. It will show you a welcome wizard where it tries to download the SDK again (and fails due to my rubbish internet). Click the X on the wizard window. That will enable you to get to the normal welcome dialog. Go to Settings->Project Defaults->Project Structure and change the Android SDK location to the correct one. Solution 2 : To fix the problem, it was required that I list the path to my corporate PAC file by using  Configure -> "Appearance and Behavior" -> System Settings -> HTTP Proxy . I selected "Automatic proxy configuration url:&quo

Code to pass Custom Objects in between Activities,Android.



Passing custom objects between android activities

Passing primitive datatypes between activities is straight froward, you can use intent.putExtra() and put anything like boolean,strings and integers etc. However you can’t pass custom objects between activities in this way.
To pass custom objects between activities/services, we must implement parcelable or serializable interface to custom class. However parcelable is specifically designed for android and is advised for best performance.
Lets start with simple class:
?
1
2
3
4
5
6
7
8
9
10
11
12
public class User {
String UserName;
String Password;
int Action;
public User(String name,String pass,int ac){
UserName=name;
Password=pass;
Action=ac;
}
}
above is a simple User class with three private fields and getters and setters.
We can't pass objects of this class between activities/services. To do this we need to make this class parcelable. Lets see:
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
public class User implements Parcelable{
String UserName;
String Password;
int Action;
public User(String name,String pass,int ac){
UserName=name;
Password=pass;
Action=ac;
}
//parcel part
public User(Parcel in){
String[] data= new String[3];
in.readStringArray(data);
this.UserName= data[0];
this.Password= data[1];
this.Action= Integer.parseInt(data[2]);
}
@Override
public int describeContents() {
// TODO Auto-generated method stub
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
// TODO Auto-generated method stub
dest.writeStringArray(new String[]{this.UserName,this.Password,String.valueOf(this.Action)});
}
public static final Parcelable.Creator<User> CREATOR= new Parcelable.Creator<User>() {
@Override
public User createFromParcel(Parcel source) {
// TODO Auto-generated method stub
return new User(source); //using parcelable constructor
}
@Override
public User[] newArray(int size) {
// TODO Auto-generated method stub
return new User[size];
}
};
}
Note that order of variable writing and reading is important, you should read and write variable in same order. One thing more pay attention to CREATOR its capital( It wasted my entire day :( ).
Now you can send and receive User objects by putting as parcelableExtra. For example in sender activity
?
1
2
3
4
5
6
7
User obj= new User("sohail","1234",1);
Intent i=new Intent(this,receiverActivity.class);
i.putExtra("userTag",obj);
startActivity(i);

Comments