Reputation: 70
Lets say I have multiple running Activities; A, B, and C. Each share a similar optionsmenu with some differences in their execution ("start" option might be slightly different in Activity A than B). I also have a static class called "values" that is tied to activity A. It also has the context of the current running activity.
At times values may call an item in the optionsmenu of the current running activity. My code is messy(see below) so I would like to organize it to a more readable form.
My goal is to set up values so that it can call just a function of the current running activity instead of an optionmenu item of that activity. Inside the activity an item of the optionsmenu would just call a function instead of code inside of it (for organization reasons).
Here a sample of values.class calling the item of an optionsmenu of the current running activity.
public void startExample() {
Runnable startRun = new Runnable() {
@Override
public void run() {
handler.post(new Runnable() { // This thread runs in the
// UI
@Override
public void run() {
((Activity) getCurrentContext()).openOptionsMenu();
((Activity) getCurrentContext()).closeOptionsMenu();
((Activity) getCurrentContext()).onOptionsItemSelected(theMenu.findItem(R.id.start));
}
});
}
};
new Thread(startRun).start();
}
As you can see values.startExample() calles the start item of the options menu of the current running activity. I would like to change this so that it calls a function based off of the current running activity instead. So I was thinking that I could do something like this instead.
In values.class
ActivityB b = new ActivityB
public void startExample() {
Runnable startRun = new Runnable() {
@Override
public void run() {
handler.post(new Runnable() { // This thread runs in the
// UI
@Override
public void run() {
if( ((Activity) getCurrentContext()).getClass().getName().equals("package.ActivityB") ) {
b.start();
}
}
});
}
};
new Thread(startRun).start();
}
And Activity B might look like.
public class ActivityB extends Activity {
//class code here
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()) {
case start:
this.start();
break;
}
}
public void start() {
//code here
}
}
Is this possible? I know that this question might sound confusing so please ask questions and I might be able to simplify it again.
Upvotes: 0
Views: 177
Reputation: 33741
No no no no no. This is like pulling a crowbar to Android and just smashing it to pieces. Activities are not ever meant to be instantiated via new
.
If all you want is for there to be some shared behavior between the methods that the different Activities call in their onOptionsItemSelected(), then either create a super activity and call the super before you add whatever behavior you want in the particular subclasses or else create a function accessible to all activities that contains what doesn't change and then add what does change inside of the subclass. Either way, Activities are meant to be ran via providing a reference to the class in startActivity, etc. You can't treat an Activity like an ordinary java object (even though it ultimately is) because it has a lifecycle that is carefully managed by the system.
Upvotes: 1