-1

I have been trying to find a solution for my problem for quite sometime now but to no avail. I have a customView class that I add statically into my MainActivity's layout (I add it in the XML file). I have a second Activity that needs to have access to my customView's methods as it will retrieve data from it and change some data. However, my second activity needs to only access that same object of the custom view that was already added to the MainActivity.

I want it to look something like this:

In my MainActivity.java I would do this:

public class MainActivity extends AppCompatActivity {


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Button btn = (Button) findViewById(R.id.btnID);


    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            Intent btnIntent = new Intent(MainActivity.this, SecondActivity.class);
            startActivity(btnIntent);
        }
    });


}   

And in my SecondActivity it would be something like this:

public class SecondActivity extends AppCompatActivity {


 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_second);


 CustomView viewer = (CustomView) findViewById(R.id.custom); //I want to link to the view I added in the MainActivity

 viewer.getData(); // Example of what methods I want to use here
 viewer.setData(); // It should directly set/get the data from the object in the MainActivity

So far I have tried this:

public static CustomView viewer = (CustomView) findViewById(R.id.custom);

In the MainActivity so that I can then use viewer in the SecondActivity but that didn't work because findViewById is non-static.

I have also tried adding this:

CustomView viewer = (CustomView) ((Activity)context).findViewById(R.id.custom);

In the SecondActivity but then I would get an error that I am invoking findViewById on a null reference.

I am not entirely sure what I can do, I have searched a lot online and in SO but I still can't fix the problem.

I am still a beginner in android so I would be grateful if someone manages to help me out.

Thanks!

UltimateDevil
  • 2,641
  • 2
  • 15
  • 30

4 Answers4

1

You cannot access views of firstActivity in secondActivity. The better way will be pass data to secondActivity using intent.putExtra() method. If you need result back to firstActivity, you can use startActivityForResult() method to get result back into firstActivity

UltimateDevil
  • 2,641
  • 2
  • 15
  • 30
Sangeet Suresh
  • 2,309
  • 15
  • 19
  • @MostafaOsama see [this](https://stackoverflow.com/questions/2091465/how-do-i-pass-data-between-activities-in-android-application) – FarshidABZ Oct 21 '17 at 13:20
  • see https://stackoverflow.com/questions/4114443/use-an-intent-to-send-data-to-my-activity for sending data see https://stackoverflow.com/questions/10407159/how-to-manage-startactivityforresult-on-android for receiving result – Sangeet Suresh Oct 21 '17 at 13:23
1

This way you can send your extras to another activity from ClassA:

Intent intent = new Intent(ClassA.this, ClassB.class);
            intent.putExtra("A", "A");
            startActivity(intent);

And receive it at ClassB:

Intent intent = getIntent();
Bundle extras = intent.getExtras();

if (extras != null) {
    if (extras.containsKey("A")) {
        //do your stuff
    }
}
Stanojkovic
  • 1,576
  • 1
  • 16
  • 21
1

A bad approach - But your code works

You tried using

 public static CustomView viewer = (CustomView) findViewById(R.id.custom);

in the MainActivity. You can get it working by dividing above statement in two parts assuming that you start SecondActivity after MainActivity. First define the variable and initialize it in onCreate of MainActivity after calling setContentView like following:

public class MainActivity extends AppCompatActivity {
public static CustomView viewer;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    viewer = (CustomView) findViewById(R.id.custom);

    Button btn = (Button) findViewById(R.id.btnID);


    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            Intent btnIntent = new Intent(MainActivity.this, SecondActivity.class);
            startActivity(btnIntent);
        }
    });


}   

Now in SecondActivity

public class SecondActivity extends AppCompatActivity {


 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_second);



 MainActivity.viewer.getData(); // Example of what methods I want to use here
 MainActivity.viewer.setData(); // It should directly set/get the data from the object in the MainActivity
}

Your code does not work because your CustomView of MainActivity layout is not available before setContentView.

A good Approach

Views contain Context and Context should not be put in static fields as they invite memory leaks. As the other answers suggest you should get all the data from your CustomView in the MainActivity itself and then you should pass it to SecondActivity via Intent.

Community
  • 1
  • 1
Birendra Singh
  • 822
  • 11
  • 18
0

You cannot reference a view across activities. You need to have your customView with id R.id.custom in both of your R.layout.activity_second and R.layout.activity_main layouts.

You can think of an Activity as a class that manager what is drawn on the screen.When you start SecondActivity it essentially redraws what your MainActivity created using R.layout.activity_main.

Also, if you do not want to go the Intent way as other answers suggest, one way of referencing the same Object across activities is to keep it in Application Context. So you can have something like:

  1. Make a CustomViewManager singleton class
  2. Instantiate an object of CustomViewManager to your application class.
  3. In your MainActivity, before your startActivity method call: set the values from customview to CustomViewManager object.
  4. In your SecondActivity refer CustomViewManager to get/set values of your customview.
Amit Barjatya
  • 209
  • 3
  • 12