0

I am currently working on an android app that has 2 pages, the first page has a slider whose input/value I am unsuccessfully trying to transfer to a canvas on page 2. the general framework of the code in question is as follows:

public class MainActivity extends AppCompatActivity {
    int sliderVariable = 1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //... SliderVariable updated using slider's ontouchListener//
    }
}

This code works fine, the slidervariables are set properly, my main struggle is finding a way to transfer these variables to the MyCanvas class on the second page. This is how the canvas is set up:

public class MyCanvas extends View{
//how do i get the sliderVariable in here?
    public MyCanvas(Context context, AttributeSet attributeSet){
        super(context,attributeSet);
   }
}

It seems like something that should be really simple, but I dont know how to transfer these variables between eachother.

I have tried to use get and set methods, these havent worked.

I thought maybe I could use sharedpreference session storage, but since these two classes are unrelated, I am unable to get them to 'sync' up (i am relatively new to java so ignore the weird lingo). I feel that this option had a good possibility of working but it just isnt. I believe one class needs to extend the other? i am not sure.

The last thing I tried was using Intent to send variables to MyCanvas, but this doesnt work. The second page is actually called Sheetpage, and MyCanvas is a view which I use in the layout of the Sheetpage, so the intent is directed towards SheetPage. I have also tried having MyCanvas being the 'receiver' of the intent but I get an error saying MyCanvas must extend android.app.Activity. Considering MyCanvas already extends view, I cannot have it also extend activity.

Are there any other ways to transfer these variables that I am missing? In the full app I have 3 sliders which only end up needing to transfer 3 two digit numbers, so if there are other ways that are usually frowned upon for being slow or resource intensive, that is not a problem here. thanks for reading and hopefully you can offer me some insight to this problem.

  • General sequence should be a) send Intent to `Sheetpage` activity with parameters b) in `Sheetpage` activity do the usual `setContentView` which creates the instance of `MyCanvas` c) `findViewById` to get instance of `MyCanvas` d) set parameters per your custom `MyCanvas` methods _which must invalidate the view_ to cause an `onDraw` presumably with the modified parameters. Include relevant portion (`onCreate`) of `Sheetpage` and how you are modifying parameters in instance of `MyCanvas`. – Computable Jun 13 '22 at 00:28

1 Answers1

0

I think that you are looking for the concept of intent.

Intent is an object to pass info between views.

On the first view you create an intent object just before creating the second view. There are lot of options for intent, you can pass many types of info. Here is an example from https://www.javatpoint.com/android-intent-tutorial

Intent intent=new Intent(Intent.ACTION_VIEW, Uri.parse(url));  
startActivity(intent);

And on the other side, where the new activity is created you receive this intent and read it. Here is an example from How do I get extra data from intent on Android?

String subName = getIntent().getStringExtra("subjectName");
int insId = getIntent().getIntExtra("instituteId", 0);

Hope you find it useful.

LexFerrinson
  • 163
  • 7