5

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 !

GAMA
  • 5,958
  • 14
  • 79
  • 126

2 Answers2

1

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

Pratik
  • 30,639
  • 18
  • 84
  • 159
  • in 2nd activity layout is main or different layout you used please update your question what you have tried it upto – Pratik Dec 20 '11 at 10:37
  • let me explain. I've created the whole layout in main.xml. I'm setting the content to this layout in 1st activity. Now I want to plot points on graph with two arrays. on buttonClick, I pass these two arrays to 2nd activity and try to display that on graph. But doing so, whole layout is getting disturbed. I want to **PASS** mySimpleXYPlot created in _1st activity_ and not **CREATE** it again in _2nd activity_ – GAMA Dec 20 '11 at 10:49
  • I have done one example but it's too much complex check edit2 – Pratik Dec 20 '11 at 12:14
  • When I click on button and navigate to another activity, graph is displayed but all the onClickListeners and similar things can not be used. i.e. If I have certain functionality on some button click in activity 1 and then I'm navigating to 2nd activity, then functionality of that button is lost as onClickListener for that button is present in activity1 and not in activity2. – GAMA Dec 20 '11 at 12:42
0

Simply add the same xml in the second content view. If you have to use a specific part of your layout in more then one view put it in an separate xml and include it.

schlingel
  • 8,560
  • 7
  • 34
  • 62
  • put the view elements you want to use in a separate xml called plotviews.xml or something similar and then put in your content views where you want to have the plot view. This way you can get the view by findViewById in both activities. – schlingel Dec 20 '11 at 10:36
  • what do you mean by "rest of the layout is not working" . remember that all clickListner and other handlers need to be set again after call setContentView() . – Shailendra Singh Rajawat Dec 20 '11 at 10:45
  • sorry but I didn't got that very clearly. Do I need to use `setContentView(R.layout.main);` in both activities or just in 1st? If I don't write `setContentView(R.layout.main);` in 2nd activity I'm getting `nullPointerException` – GAMA Dec 20 '11 at 10:51
  • Sorry, I didn't get the question at first. I thought you want to have a second plotview in the second activity. You should work with startActivityForResult http://developer.android.com/reference/android/app/Activity.html, pack the data you want to plot as parcable or serializable in the bundle and process it in the activity which holds the plot view. – schlingel Dec 20 '11 at 11:14
  • I can't get an example where parcel is used to pack the view. Is it like that ,It can pack int,string,etc. but not view? – GAMA Dec 20 '11 at 11:46