Here is source code of the Program to Send Data From Service to Activity in Android using Java. The program is successfully compiled and run on a Windows system using Eclipse Ide. The program output is also shown below.
The following code demonstrates to send an object from one activity to another , we can’t do that directly an approach to solve this problem has been demonstrated by using Parcable class in android to send object data from one activity to another.
Send data by
intent.putExtra("value", object);
and similarly retreive by –
Intent i = getIntent(); Myobject myObject = (Myobject) i.getParcelableExtra("value");
Main Activity
advertisement
advertisement
package com.example.javaandroidprogramtosenddatafromoneactivitytoanotherinandroid; import android.os.Bundle; import android.os.Parcel; import android.os.Parcelable; import android.app.Activity; import android.content.Intent; import android.view.Menu; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button mybuttton = (Button) findViewById(R.id.button1); final EditText inp = (EditText) findViewById(R.id.inp); mybuttton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub int num = Integer.parseInt(inp.getText().toString()); Intent intent = new Intent(getApplicationContext(), Second.class); Myobject objMyobject = new Myobject(); objMyobject.mData = num; intent.putExtra("value", objMyobject); startActivity(intent); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }
Myobject
package com.example.javaandroidprogramtosenddatafromoneactivitytoanotherinandroid; import android.os.Parcel; import android.os.Parcelable; public class Myobject implements Parcelable { int mData; public int describeContents() { return 0; } // write your object's data to the passed-in Parcel public void writeToParcel(Parcel out, int flags) { out.writeInt(mData); } public static final Parcelable.Creator<Myobject> CREATOR = new Parcelable.Creator<Myobject>() { public Myobject createFromParcel(Parcel in) { return new Myobject(in); } public Myobject[] newArray(int size) { return new Myobject[size]; } }; Myobject(Parcel in) { mData = in.readInt(); } Myobject() { } }
Second
Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
package com.example.javaandroidprogramtosenddatafromoneactivitytoanotherinandroid; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.Menu; import android.widget.TextView; public class Second extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_second); TextView out = (TextView)findViewById(R.id.out); Intent i = getIntent(); Myobject myObject = (Myobject) i.getParcelableExtra("value"); out.setText("Received : " + "\n"+myObject.mData); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.second, menu); return true; } }
advertisement
Sanfoundry Global Education & Learning Series – 100+ Java Android Tutorials.
If you wish to look at all Tutorials, go to Java Android Tutorials.
Related Posts:
- Check Java Books
- Apply for Java Internship
- Apply for Computer Science Internship
- Practice Programming MCQs
- Practice BCA MCQs