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.