I'm trying to pass a view from one activity to another activity.
In my 1st activity, onButtonClick
, I'm navigating to another activity by using Intent
.
I've written this line setContentView(R.layout.main);
in 1st activity and also declared a graphView
.
Now the problem is, I want to populate the graphView
in the second activity but it's reference i.e. mySimpleXYPlot = (XYPlot) findViewById(R.id.mySimpleXYPlot);
is present in 1st activity.
So how can I acess mySimpleXYPlot
in 2nd activity?
if I use
setContentView(R.layout.main);
mySimpleXYPlot = (XYPlot) findViewById(R.id.mySimpleXYPlot);
in 2nd activity, whole layout gets restarted which I don't want to happen :(
ANY HELP WILL BE APPRICIATED !
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With