if you want to use same instance that declare as static with public so you can used in another class or activity also
Edit...
In Activity 1st do like this way
public class MyActivity1 extends Activity{
public static XYPlot mySimpleXYPlot;
public onCreated(Bundle b){
setContentView(R.layout.main);
mySimpleXYPlot = (XYPlot) findViewById(R.id.mySimpleXYPlot);
// start you 2nd activity from button
}
}
In Activity2 use this way
public class MyActivity2 extends Activity{
private XYPlot mySimpleXYPlot;
public onCreated(Bundle b){
setContentView(R.layout.main);
mySimpleXYPlot = MyActivity2.mySimpleXYPlot;
// use mySimpleXYPlot as per your requirement
}
}
I am not sure this work perfectly or not but try this way and say what happen with this
Edit2
don't add your component into the xml layout file add at oncreate time
my layout file looks like this
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:id="@+id/main_linear"
>
<Button android:id="@+id/btn1" android:text="Click" android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
my first activity
public static EditText edittext;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test);
//edittext = (EditText) findViewById(R.id.edittext);
final LinearLayout ll = (LinearLayout) findViewById(R.id.main_linear);
edittext = new EditText(getApplicationContext());
edittext.setId(1);
edittext.setText("text change");
((Button)findViewById(R.id.btn1)).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
edittext.setText(edittext.getText().toString());
ll.removeView(edittext);
startActivity(new Intent(TestLinear.this,TestClass.class));
}
});
ll.addView(edittext);
}
you need to remove first that component from the layout in which you have added as child
private static EditText edittext;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test);
LinearLayout ll = (LinearLayout) findViewById(R.id.main_linear);
getEdit();
((Button)findViewById(R.id.btn1)).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Log.v("test", edittext.getText().toString());
}
});
ll.addView(edittext);
}
static void getEdit(){
edittext = TestLinear.edittext;
}
I know this not perfect way. Another way is you can store it's value in custom class like setter/getter and used in activity