what is the proper way to start ioioActivity?

90 views
Skip to first unread message

Ilan Tal

unread,
Mar 16, 2016, 6:30:22 AM3/16/16
to ioio-users
In order that my connection to ioio will not die upon the phone screen shutting down, I put most of the ioio activity into a separate class.
I have a text view and a button defined on the main activity.

I defined a constructor in IoioTerbiumActivity which takes as an input the MainActivity object. That way I can control the text view and button from inside IoioTerbiumActivity.

When I tried the project, it showed the text view and the button but clearly did not start my ioio class.
It clearly doesn't make sense to create the class and then call startActivity with a new intent. If I just call startActivity, how do I bring across the parent class?
If I use the my1 from the constructor, what is the proper way to start the activity?

public class
MainActivity extends AppCompatActivity {
TextView timerView;
int runState = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
timerView = (TextView) findViewById(R.id.timerView);
Button b = (Button) findViewById(R.id.startBut);
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String txtTmp = getString(R.string.start_lab);
Button b = (Button) v;
switch (runState) {
case 0:
case 1:
txtTmp = getString(R.string.stop_lab);
runState++;
break;

case 2:
runState++;
break;

default:
runState = 0;
break;
}
b.setText(txtTmp);
}
});
// IoioTerbiumActivity my1 = new IoioTerbiumActivity(this);
// startActivity(new Intent(this,IoioTerbiumActivity.class));
}
}

Ytai Ben-Tsvi

unread,
Mar 20, 2016, 4:33:14 PM3/20/16
to ioio-...@googlegroups.com
You don't need two activities. It doesn't make sense, since two activities are not typically alive at the same time. Merge them into one activity that does both the GUI and the IOIO stuff (from inside a IOIOLooper, which can be an inner class or a regular one, define in a separate file).

--
You received this message because you are subscribed to the Google Groups "ioio-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ioio-users+...@googlegroups.com.
To post to this group, send email to ioio-...@googlegroups.com.
Visit this group at https://groups.google.com/group/ioio-users.
For more options, visit https://groups.google.com/d/optout.

Ilan Tal

unread,
Mar 22, 2016, 12:07:34 PM3/22/16
to ioio-users
Hi Ytai,
I almost gave up on getting a reply from you, since my problem isn't connected to the IOIO board as such but is an android problem.

I have written tons of code in regular Java but android is a different mind set. In regular Java I make a constructor to a dialog or whatever else I want to use and then I call functions in the instantiated class. Here things just come alive by themselves.

The problem with everything in a single file was that when the android screen turned off, it would disconnect the IOIO board, but my timer would keep counting. You can see my initial bias was to use a constructor and then somehow start the IOIO - and stop the IOIO when I am finished. I had no idea what exactly to call in "my1", assuming there is something in the IOIOActivity which will start the board. The only thing I could find was startActivity but that used new intent.

It is totally unclear to me why having the code in a separate file would make any real difference. If I understand you correctly you are saying have the loop going always, but perhaps have the code which it operates in a separate object. Then I would never "start" or "stop" the IOIO itself but only control what it is doing in a separate object. I can't see how that would make any difference, since it is the card itself which is losing connection. I thought that if I could somehow start and stop the card itself, then it would no longer loose connection when the android screen goes off. It isn't clear to me who is telling the IOIO to drop the connection, and why.

Once I see what the answer is, it will probably look very simple but at the moment it is all black magic. Somehow I need to get control and not have the card drop the connection whenever the screen goes blank or some other event. I looked at the source code for IOIOActivity and it didn't give me much. That code is the interface and I need something which calls it - something that does start and stop on the board itself. At least this is what I think I need.

Thanks,
Ilan

Ytai Ben-Tsvi

unread,
Mar 22, 2016, 11:51:43 PM3/22/16
to ioio-...@googlegroups.com
On Tue, Mar 22, 2016 at 9:07 AM, Ilan Tal <ilan...@gmail.com> wrote:
Hi Ytai,
I almost gave up on getting a reply from you, since my problem isn't connected to the IOIO board as such but is an android problem.

I have written tons of code in regular Java but android is a different mind set. In regular Java I make a constructor to a dialog or whatever else I want to use and then I call functions in the instantiated class. Here things just come alive by themselves.

Well, not really. You're calling API methods to make those things happen. Whether you're constructing the objects explicitly or requesting them to be created from the framework is not such a big difference. Of course, like anything else, it is a learning curve, but luckily there's really good documentation on the Android developer website explaining all those things. I'd recommend you to take some time to go over those things, so you don't have to do much trial and error to get your app going.
 

The problem with everything in a single file was that when the android screen turned off, it would disconnect the IOIO board, but my timer would keep counting. You can see my initial bias was to use a constructor and then somehow start the IOIO - and stop the IOIO when I am finished. I had no idea what exactly to call in "my1", assuming there is something in the IOIOActivity which will start the board. The only thing I could find was startActivity but that used new intent.

This has nothing to do with single file or not. Your activity typically gets stopped by the framework when the phone goes to sleep. That's just the way it is. The framework notifies your activity that it should stop by calling its onStop() method. What your activity does (by virtue of inheriting from IOIOActivity) is disconnect from the IOIO when that happens. It doesn't have to do that, though. As I said, if you make a copy of IOIOActivity and inherit from it instead, you can change its behavior, for example, to not disconnect from the IOIO in onStop(), but rather some other time, for example, if the user explicitly presses a disconnect button. This isn't a very good practice, strictly speaking, since Android really does want you to stop the application in onStop() and if you want to leave stuff running in the background, you should really use a service (e.g. IOIOService). But in practice it would probably work fine. BTW, I might be mis-remembering, but I feel like on my phone the app only gets paused (onPause()) when the screen turns off, hence keeping the IOIO connection alive.
 

It is totally unclear to me why having the code in a separate file would make any real difference. If I understand you correctly you are saying have the loop going always, but perhaps have the code which it operates in a separate object. Then I would never "start" or "stop" the IOIO itself but only control what it is doing in a separate object. I can't see how that would make any difference, since it is the card itself which is losing connection. I thought that if I could somehow start and stop the card itself, then it would no longer loose connection when the android screen goes off. It isn't clear to me who is telling the IOIO to drop the connection, and why.

No. It is not about a separate file or a separate object. IOIOActivity is explicitly telling the IOIO to connect at onStart() and disconnect at onStop(), and what I proposed is for you to change that.
All I meant with my comment regarding a separate file is that if you want, you can define your IOIOLooper sub-class as a regular class (in a separate file) rather than an inner class of your IOIOActivity sub-class (just to make the code more readable, not a fundamental difference in functionality).
 

Once I see what the answer is, it will probably look very simple but at the moment it is all black magic. Somehow I need to get control and not have the card drop the connection whenever the screen goes blank or some other event. I looked at the source code for IOIOActivity and it didn't give me much. That code is the interface and I need something which calls it - something that does start and stop on the board itself. At least this is what I think I need.

What calls it is the Android framework. You don't call onStart() and onStop() etc. directly. What these methods to (e.g. call the IOIOAndroidApplicationHelper.start(), etc.) is what makes the IOIO connect and disconnect etc.
 

Thanks,
Ilan

Ilan Tal

unread,
Mar 29, 2016, 1:48:25 AM3/29/16
to ioio-users
Ytai,
I just wanted to let you know that I very much appreciated your detailed answer.
There is a course from Udacity which goes through the development process but I can't stop everything while I go through the course.
So, like it or not, I have to feel my way around and make mistakes.
The most important part of your letter, from my point of view, was you pointing out I was doing nonsense by creating a new activity.
I could have wasted endless hours on that, going in a totally wrong direction.

I sat on your letter and turned it around in my mind for 24 hours before I decided that at this stage, simplicity was the best method.
It is always nice to try to learn new things but at this stage I need "a bird in hand", which gets the job done.
So I have returned to the original code, with some additions from what I learned from my mistakes, and can continue to work again.
We just have to insure that the android doesn't go to sleep during the measurements.
The really interesting point was the timer in the application continues on, while the connection to the IOIO board goes away.
Up front I would have expected "all or nothing", but it doesn't work that way.

When I have more experience I can return to see if I can implement your suggestion. For the moment, I have something which works.

Thanks,
Ilan

Eddie Latuta

unread,
Mar 31, 2016, 4:20:41 PM3/31/16
to ioio-users

Why don't you use ioioservice and communicate to the activities throughout intents or use something like eventbus


Reply all
Reply to author
Forward
0 new messages